1

I like to create a nomenclature that shows a page number (with \pageref or \hyperlink, etc) pointing to a page elsewhere that I assigned a label. Possibly, this label should be an argument to the nomenclature command.

In a similar question it is possible to add page numbers to nomenclature, but they place the number of the page where \nomenclature itself is located at. While, I need to assign the page myself.

The motivation for this question is that I write all my nomenclatures in one file, say symbols.tex, like

% In symbolds.tex
% We have groups of symbols (here S, and P)
\nomenclature[S,001]{$N$}{Integer}{\ref{numencl:N}}
\nomenclature[P,001]{$M$}{Another integer}{\ref{numencl:M}}

and include them before any chapter starts (\include{symbolds.tex}). This way I can reorder and regroup the entries better in a large document. Later in each chapter, I add anchors (\label) to places where a variable is first defined. I like the nomenclature to know where to find the page number to the labels that are placed forward in the document. Something like

% In symbolds.tex
\nomenclature[S,001]{$N$}{An integer}{\ref{numencl:N}}

% Later in a chapter
The variable $N$ \label{nomencl:N} is ...

Here is an example

% In main.tex

\documentclass{memoir}

\usepackage{lipsum}

% Nomenclature stuff
\usepackage[refpage]{nomencl}
\usepackage[backref=page,pageanchor]{hyperref}

% I am not sure I need the next line, this is from the linked stack exchange page above
\def\pagedeclaration#1{, \dotfill\hyperlink{page.#1}{\nobreakspace#1}} 
\makenomenclature

% Grouping symbols
usepackage{etoolbox}
\renewcommand\nomgroup[1]{%
  \item[\bfseries
  \ifstrequal{#1}{S}{Sets}{%
  \ifstrequal{#1}{P}{Symbols}{}}%
]}
% ------------------

\begin{document}

% Some fillers here (title page, abstract, table of content, list of figures, etc.
\frontmatter
\tableofcontents*
\clearpage
\thispagestyle{plain}
\begin{abstract}
\lipsum[1]
\end{abstract}

% Here is the nomenclature
\include{symbols}
\clearpage
\printnomenclature

% Chapters start
\chapter{Introduction}
Let $N$ be an integer \label{nomencl:N} ... and $M$ another integer \label{nomencl:M} defined by ...

\end{document}

I am wondering how I can renew \nomenclature or other related commands, so that it accept \label{nomencl:N}) as an option.

If this is not possible, what is a feasible solution to place a reference to an arbitrary page?

Marijn
  • 37,699
Sia
  • 219

1 Answers1

1

To make the \nomenclature command use a label as a page reference you can modify the command to accept an extra argument #4. The command is defined in nomencl.sty as a sequence of commands: \nomenclature calls \@nomenclature which calls \@@nomenclature which finally calls \@@@nomenclature. This last command is where the actual formatting is done. The original command contains the line

|nompageref}{\thepage}}%

which is interpreted as "put the \nompageref command in the output with \thepage as argument", where \thepage is expanded to the page number where the command is called. You can change this to call a different command with a different argument. An easy way seems to be to just call \pageref with an extra command #4. However, this does not work because the original command \nompageref contains some extra code at the end that is necessary for the processing of the nomenclature. Therefore you can define a new command (called \nomlabelref in the code below) to contain a space, the \pageref, and the extra code, and call that new command within \@@@nomenclature.

MWE:

\documentclass{article}
\usepackage[refpage]{nomencl}
\makeatletter
\def\nomlabelref#1{\ \pageref{#1}\nomentryend\endgroup}
\def\@@@nomenclature[#1]#2#3#4{%
 \def\@tempa{#2}\def\@tempb{#3}\def\@tempc{#4}%
 \protected@write\@nomenclaturefile{}%
  {\string\nomenclatureentry{#1\nom@verb\@tempa @[{\nom@verb\@tempa}]%
      \begingroup\nom@verb\@tempb\protect\nomeqref{\theequation}%
        |nomlabelref}{\@tempc}}%
 \endgroup
 \@esphack}
\makeatother
\usepackage[backref=page,pageanchor]{hyperref}
\makenomenclature
\begin{document}
\nomenclature[S,001]{$N$}{Integer}{nomencl:N}
\nomenclature[P,001]{$M$}{Another integer}{nomencl:M}
first page
\printnomenclature
\clearpage
second page

Let $N$ be an integer \label{nomencl:N} ... and $M$ another integer \label{nomencl:M} defined by ...
\end{document}

Result:

enter image description here

Note that I did not use #4 directly, but instead I defined a temporary variable \@tempc, to be consistent with the rest of the definition (which defines \@tempa and \@tempb for #2 and #3).


In response to the comment: you can check for an empty argument and remove the pageref in that case. Because makeindex requires an argument you can use a dummy value like 1. Then in \makelabelref you can check for this value and print the pageref only when the argument is an actual label. The check uses \ifnum1=0#1 which works for numbers and for strings, see https://tex.stackexchange.com/a/306500/.

MWE:

\documentclass{article}
\usepackage[refpage]{nomencl}
\makeatletter
% if argument is 1 do nothing, else print pageref
\def\nomlabelref#1{\ifnum1=0#1\relax\else\ \pageref{#1}\fi\nomentryend\endgroup}
\def\@@@nomenclature[#1]#2#3#4{%
 \def\@tempa{#2}\def\@tempb{#3}\def\@tempc{#4}%
 \ifx\@tempc\@empty\def\@tempc{1}\fi% set dummy value if argument is empty
 \protected@write\@nomenclaturefile{}%
  {\string\nomenclatureentry{#1\nom@verb\@tempa @[{\nom@verb\@tempa}]%
      \begingroup\nom@verb\@tempb\protect\nomeqref{\theequation}%
        |nomlabelref}{\@tempc}}%
 \endgroup
 \@esphack}
\makeatother
\usepackage[backref=page,pageanchor]{hyperref}
\makenomenclature
\begin{document}
\nomenclature[A,001]{$N$}{Integer}{nomencl:N}
\nomenclature[B,001]{$M$}{Another integer}{nomencl:M}
\nomenclature[C,001]{$Z$}{Yet another integer}{}
first page
\printnomenclature
\clearpage
second page

Let $N$ be an integer \label{nomencl:N} ... and $M$ another integer \label{nomencl:M} defined by ...
\end{document}

Result:

enter image description here

Marijn
  • 37,699
  • This hack is an excellent solution. Is it possible to make the fourth argument optional? There are some symbols that might not need to be linked to any specific page. I am wondering if the signature of \nomenclature can fall back to no 4th option, or perhaps accept a null input {} on the last option. – Sia Oct 18 '19 at 22:16
  • @Sia see edit for using an empty argument. – Marijn Oct 19 '19 at 18:50
  • Dear Marijn, do you know if there is a way to adapt your solution to my recent question https://tex.stackexchange.com/questions/586422 ? :-) – Watson Mar 10 '21 at 08:15
  • (OK, I just found a way to solve my problem!) – Watson Mar 10 '21 at 08:36