nomenclature horror



In retrospective, I don't even know why I am still using [nomenclature](http://ctan.org/tex-archive/macros/latex/contrib/nomencl), but with my current fixes it seems to work just fine. I am using the nomencl package to create a list of symbols for mathematical texts. The options are as follows,
\usepackage[refpage,intoc,norefeq]{nomencl} 
% ... 
\setlength{\nomlabelwidth}{.8in} 
\setlength{\nomitemsep}{-.8\parsep}
\renewcommand*{\pagedeclaration}[1]%
{\nobreakspace(page\nobreakspace\hyperpage{#1})} 
because I think it is extremely practical to have a page reference to the definition of a mathematical symbol. However, page referencing does not seem to be a very popular feature of this *rich* package, so it is clear that more effort was put into developing other features. In fact, problems do not occur if you only use the nomenclature command once for each symbol. However, I sometimes want to add the same command in two places in order to reference two different pages. This usually happens when I define a symbol for a special case first, then extend the definition to a larger scope. For reasons I do not fully comprehend, nomencl will sometimes handle this very competently, and sometimes screw up cataclysmically. Every once in a while, it will write
\nompageref{12} \nompageref{17} 
instead of
\nompageref{12, 17} 
Now the nompageref macro contains an \endgroup command, so two of them following each other will cause the latex compilation to fail. I have no idea what causes this bug, but it is quite easy to fix. But don't worry, there's more. The nomenclature creation
makeindex %2.nlo -s nomencl.ist -o %2.nls
does write \nomeqrefs commands to the nls-file, which is completely unnecessary considering my options - but it *did* make me realize another bug: Even if two entries are completely identical, if they have a different equation preceeding them, nomencl will create one separate entry for each of them. That is quity silly considering that I explicitly forbade it to ever display equation references anyway. To make a long story short, I wrote a little script fixnls.py to fix all the horrible things that nomencl does wrong. For the time being, it might be of use to someone.
import re
from sys import argv, stdout

stdout.write('Removing nomeqrefs ...')
contents = open(argv[1],'r').read()
contents = re.sub('\\\\nomeqref\s?\{[\d.]*\}\s*','',contents)
stdout.write(' done.\n')

stdout.write('Merging nompagerefs ...')
contents = contents.replace('}, \\nompageref{',', ')
stdout.write(' done.\n')

stdout.write('Combining ...')
flip = True
count = 0
while flip:
  pattern = '\\\\item\s\\\x5B\{\$([^\$]*)\$\}\\\x5D' + \
    '(?:(?:\\\\\S*\s*)*)([^\n\r]*)\\\\nompageref\{([^\}]*)\}'
  matched_items = [ match for match in re.finditer(pattern,contents) ]
  matched_items.reverse()
  flip = False  
  for m in matched_items:
    if flip: break
    for n in matched_items:
      if n is m: continue
      elif flip: break
      if n.group(2) == m.group(2):
        flip = True
        count += 1
        number = m.group(3)
        contents = contents[:m.start()]+contents[m.end():]
        contents = contents[:n.end(3)]+', '+number+contents[n.end(3):]

stdout.write(' %d combined.\n' % count)
open(argv[1],'w').write(contents)
In [LED](http://latexeditor.org) (which is, by the way, the worst editor with the best preview function) I can now do all the jobs at once in the following user1.bat:
@echo off
%3
cd %1
makeindex %2
makeindex %2.nlo -s nomencl.ist -o %2.nls
python fixnls.py %2.nls
Here's a minimal example for those who really just want a working nomenclature:
\documentclass{article}
\usepackage{hyperref}

\usepackage[refpage,intoc,norefeq]{nomencl} 

\setlength{\nomlabelwidth}{.8in} 
\setlength{\nomitemsep}{-.8\parsep}
\renewcommand*{\pagedeclaration}[1]%
{\nobreakspace(page\nobreakspace\hyperpage{#1})}

\makenomenclature

\begin{document}

\section{First Section}
I will call $\Gamma$ something nice
\nomenclature[G]{$\Gamma$}{something nice}
and also, $\Delta$ will be something bad.
\nomenclature[D]{$\Delta$}{something bad}
consequently,
\begin{equation}
\Delta\ne\Gamma
\end{equation} 

\newpage
\section{Second Section}
In a completely different context, let $\Gamma$ again be something rather nice.
\nomenclature[G]{$\Gamma$}{something nice}
that's it!

\newpage
\printnomenclature
\end{document}

Tags: - -

Leave a Reply

Your email address will not be published.