[Tex/LaTex] Creating a simple list of symbols

glossarieslistsmacrosprogrammingsorting

Inspired by this great answer I tried to manually create a glossary-style environment for a simple list of symbols. Basically I wanted to create a command newsymbol that can be executed from anywhere in the text and adds the new symbol together with a short explanation and the page number to a list of symbols at the end of the text. This is what I tried:

 \documentclass{amsbook}
 \usepackage{lstdoc,longtable}

 \begin{document}
 \makeatletter

 \def\symbollist{}
 \let\sort\lst@BubbleSort 
 \def\addtolist#1#2{
   \lst@lAddTo\symbollist{#2}
 }
 \long\gdef\addterm#1#2{\addtolist\symbollist{#1,}}
 \def\newsymbol#1#2#3{%
 \long\expandafter\gdef\csname#1\endcsname{#2 & #3 & \thepage}
 \addterm{#1}{#2}
 \sort\symbollist
 }
 \def\PrintListOfSymbols{%
 \begin{longtable}{r p{.8\textwidth} l}
 \@for \i:=\symbollist\do{%
     \csname\i\endcsname \\ }
 \end{longtable}
 }

 \chapter{Contents}
 Here the integers $\mathbb{Z}$ appear first.\newsymbol{Z}{$\mathbb{Z}$}{integers} \newpage

 Here the other symbols appear first: $\mathbb{R},\mathbb{N}$. \newsymbol{R}{$\mathbb{R}$}{real numbers} \newsymbol{N}{$\mathbb{N}$}{natural numbers}

 \begin{center} The entry from inside an environment does not appear. \newsymbol{Q}{$\mathbb{Q}$}{rational numbers} \end{center}

 \chapter{List of Symbols}
 \PrintListOfSymbols

 \makeatother
 \end{document}

The first argument of newsymbolis just used for the sorting of the entries. Here is what the list looks like:
List of symbols

There are basically two problems yet to resolve:

  • The page numbers are not correct, the \thepage variable returns the page where the list of notations is printed. How can I give a command the page number where the command is executed as an argument?
  • The command does nothing when executed from within an environment. How to fix that?

I'd be grateful for your help!

Best Answer

After some more debugging I got everything to work. In case anyone is interested, here is a working version:

\documentclass{article}
\usepackage{lstdoc,longtable,amssymb}

\begin{document}
\makeatletter

\def\symbollist{}

\def\newsymbol#1#2#3{\label{symbol#1}#2\long\expandafter\gdef\csname createNewSymbol#1\endcsname{$#2$ & & #3 & & \pageref{symbol#1}\\}\lst@AddTo\symbollist{createNewSymbol#1,}}

\def\PrintListOfSymbols{%
\lst@BubbleSort{\symbollist}
\renewcommand*{\arraystretch}{1.1} 
\begin{longtable}{r p{.01\textwidth} p{.8\textwidth} p{.01\textwidth} l}
\@for \i:=\symbollist\do{%
    \csname\i\endcsname }
\end{longtable}
}

\section{Contents}
Here the integers $\newsymbol{Z}{\mathbb{Z}}{integers}$ appear first. Here the continuous functions $\newsymbol{CX}{C(X)}{continuous functions on $X$}$\newpage

Here the other symbols appear first: $\newsymbol{R}{\mathbb{R}}{real numbers}, \newsymbol{N}{\mathbb{N}}{natural numbers}$

\begin{center} Even entries $\newsymbol{Q}{\mathbb{Q}}{rational numbers}$ from inside an environment appear.  \end{center}

\section{List of Symbols}
\PrintListOfSymbols

\makeatother
\end{document}
Related Question