# nomencl package : sort by order of appearance

In the nomencl package, how can I make the symbols print by order of appearance in the LaTeX code?

• I'm not aware of an easy way to do this with nomencl. Are you committed to nomencl or would you consider changing packages? (It can easily be done with glossaries via the sort=use option but that package has a different interface, so it's not straight-forward to convert.) – Nicola Talbot Jul 24 '17 at 16:13
• @NicolaTalbot not really, but I have already a full document using nomencl, so switching to glossaries would be quite annoying. – unknownSoldier Jul 24 '17 at 18:03
• @unknownSoldier Okay. Just thought I'd mention it on the off-chance. Maybe someone more familiar with nomencl can suggest something. – Nicola Talbot Jul 25 '17 at 8:06

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
\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.

• thanks! But this solution does not interact well with the subfiles package, which I am using... – unknownSoldier Aug 4 '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 4 '17 at 12:24
• I don't know the reason, but this hacky gets messy when the list have more than ten items... – Renan Maneli Mezabarba Oct 20 '17 at 13:15
• @RenanManeliMezabarba Thanks for letting me know. Cheers. – Troy Oct 20 '17 at 13:57
• @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 6 '18 at 19:14

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.