In the nomencl package, how can I make the symbols print by order of appearance in the LaTeX code?
- 81
2 Answers
As far as I know, nomencl does not support this out of the bag. Here's a hacky way to do it instead.
Basically it is a redefinition of the \nomenclature[]{}{} command, to ignore the sorting argument (i.e. the optional argument in \nomenclature) and to use a counter-based sorting method instead.
Whenever \nomenclature is called, the counter @nomcount raises by one. We then take advantage of makeindex's sorting algorithm which treats numbers as per normal (8<34<111), and so the entries will be printed in order of use.
I have included a toggle for you to switch this sorting behaviour 'on' and 'off'. With
\settoggle{nomsort}{<bool>}
If <bool> is set to true, then we activate sort by use (which is your desired effect). If <bool> is set to false then we return to normal usage of nomencl (using the optional argument of \nomenclature and what not).
\documentclass[]{article}
\usepackage{etoolbox}
\usepackage[]{nomencl}
\makenomenclature
\providetoggle{nomsort}
\settoggle{nomsort}{true} % true = sort by use, false = sort as usual
\makeatletter
\iftoggle{nomsort}{%
\let\old@@@nomenclature=\@@@nomenclature
\newcounter{@nomcount} \setcounter{@nomcount}{0}%
\renewcommand\the@nomcount{\two@digits{\value{@nomcount}}}% Ensure 10>01
\def\@@@nomenclature[#1]#2#3{% Taken from package documentation
\addtocounter{@nomcount}{1}%
\def\@tempa{#2}\def\@tempb{#3}%
\protected@write\@nomenclaturefile{}%
{\string\nomenclatureentry{\the@nomcount\nom@verb\@tempa @[{\nom@verb\@tempa}]%
\begingroup\nom@verb\@tempb\protect\nomeqref{\theequation}%
|nompageref}{\thepage}}%
\endgroup
\@esphack}%
}{}
\makeatother
\begin{document}
\nomenclature{$j$}{Appears first}
\nomenclature{$i$}{Appears second}
\nomenclature{$h$}{Appears third}
\nomenclature{$g$}{Appears fourth}
\nomenclature{$f$}{Appears fifth}
\nomenclature{$e$}{Appears sixth}
\nomenclature{$d$}{Appears seventh}
\nomenclature{$c$}{Appears eighth}
\nomenclature{$b$}{Appears ninth}
\nomenclature{$a$}{Appears tenth~(last)}
\printnomenclature
Note that $d$ appears first because it is declared first.
Note also the 10th entry is sorted after the 9th.
\end{document}
To be honest, though, it's probably easier and more consistent to do it with the glossaries package, as @NicolaTalbot mentioned in the comments.
- 13,741
-
thanks! But this solution does not interact well with the
subfilespackage, which I am using... – unknownSoldier Aug 04 '17 at 12:21 -
@unknownSoldier Please provide a minimal working example (complete with documentclass and the included packages) that we can compile and that demonstrates the problem you are facing, i.e. what does "does not interact well" mean? – Troy Aug 04 '17 at 12:24
-
I don't know the reason, but this hacky gets messy when the list have more than ten items... – Renan Mezabarba Oct 20 '17 at 13:15
-
-
1
-
Me again: same problem, but now for more than one hundred items - I know, it looks like I'm writing a big text. I naively tried to replace \two@digits with \three@digits, but it didn't work. – Renan Mezabarba Feb 06 '18 at 17:26
-
1@RenanManeliMezabarba Define your own
\three@digits:\def\three@digits#1{\ifnum#1<100 0\ifnum#1<10 0\fi\fi\number#1}in the preamble. – Troy Feb 06 '18 at 19:14 -
It worked, thanks! I'll come back here when I get to the "thousand" version of the issue lol – Renan Mezabarba Feb 06 '18 at 21:32
-
I don't see the advantage of
\string\nomenclatureentry{\the@nomcount\nom@verb\@tempaover\string\nomenclatureentry{#1\the@nomcount\nom@verb\@tempa, the former breaks nomencl's approach to grouping to create multiple lists without obvious benefit. – Dai Bowen Apr 18 '23 at 14:39
Let us say that you want to print variables z,y and x using nomencl with
z coming first followed by y and x. You key in the code as below:
\nomenclature{$z$}{This is the first variable}
\nomenclature{$y$}{This is the second variable}
\nomenclature{$x$}{This is the third variable}
\printnomenclature
The nomenclature package sorts the variables in alphabetical order. The default output for an input code given above will be:
which may not be the desired one. To generate the nomenclature list in the input order (in the order as they appear) can be accomplished by the including the order number within the optional square brackets. The modified code with a longer list of variables is illustrated below:
\nomenclature[[1]]{$z$}{This is the first variable}
\nomenclature[[2]]{$y$}{This is the second variable}
\nomenclature[3]{$x$}{This is the third variable}
\nomenclature[4]{$w$}{This is the fourth variable}
\nomenclature[5]{$v$}{This is the fifth variable}
\nomenclature[6]{$u$}{This is the sixth variable}
\nomenclature[7]{$t$}{This is the seventh variable}
\nomenclature[8]{$s$}{This is the eighth variable}
\nomenclature[9]{$r$}{This is the ninth variable}
\nomenclature[a1]{$q$}{This is the tenth variable}
\nomenclature[a2]{$p$}{This is the eleventh variable}
\printnomenclature
The desired output appears below:
Note the numbering scheme used within the square brackets to extend beyond 10 variables. Hope this helps. Thank you.
- 259,911
- 34
- 706
- 1,036



nomencl. Are you committed tonomenclor would you consider changing packages? (It can easily be done withglossariesvia thesort=useoption but that package has a different interface, so it's not straight-forward to convert.) – Nicola Talbot Jul 24 '17 at 16:13nomencl, so switching toglossarieswould be quite annoying. – unknownSoldier Jul 24 '17 at 18:03nomenclcan suggest something. – Nicola Talbot Jul 25 '17 at 08:06