How to create a list of symbols where symbols can be used in math mode?

I describe complex protocols with lots of variables in different files. The problem is that I might change the variable names later. My idea was to define variable names as Latex commands like \lengthtree (instead of writing e.g. $\ell_t$). Later I can easily change \lengthtree to $L_t$ by just redefine the latex command.

Now I want to create a list of symbols and I am searching for a possibility to create it automatically. I give the glossaries package a try, but I get Latex errors if I use the symbols in math environments.

What would be the "state-of-the-art" solution to define my variables (which can be used also in math mode) at one global point? A list of symbols should be created automatically.

• i may be misinterpreting something, but this seems like a compelling reason to use \ensuremath. – barbara beeton Oct 11 '13 at 16:42
• By symbol directory, do you mean list of symbols? – jub0bs Oct 11 '13 at 16:48
• @barbarabeeton: Oh thx, yes this would work but I have read that \ensuremath comes with a lot of problems. Other solutions? – user4811 Oct 11 '13 at 17:08
• @Jubobs: yes, sorry I am not a native speaker... – user4811 Oct 11 '13 at 17:09
• if you're using amsmath, they you might try \text{$...$} as the expansion of your symbol code. \text ensures the correct size in sub- and superscripts, but it's expected to be used within a math context, so if you use it in text (such as in an ordinary sentence, or even as the entry for your symbols list), you need to put it in $...$'. and if any of these symbols are supposed to be large operators (like \sum), this also won't automatically go into \displaystyle in a display equation. – barbara beeton Oct 11 '13 at 17:16

2 Answers

As barbara mentioned, you can use \ensuremath like this:

\documentclass{article}

\usepackage{glossaries}

\makeglossaries

\newglossaryentry{lengthtree}%
{%
name={\ensuremath{L_t}},
description={description here},
sort={L}
}

\begin{document}

In line: \gls{lengthtree}. In math mode:
$\gls{lengthtree}$

\printglossary[title={List of Symbols}]

\end{document}


The arguments against \ensuremath come down to two main issues (see When not to use \ensuremath for math macro?):

Firstly, you might end up with $ inside the argument of \ensuremath. This won't occur in this example, as the argument of \ensuremath is being explicitly set to L_t. Secondly, there is an issue of semantics. Following this line of argument, you should define the entry without \ensuremath and do $\gls{lengthtest}$, which is fine in the document text that you type, but this will cause a problem in the glossary where the style doesn't automatically shift to math mode when it comes to the entry name. Personally, I think \ensuremath is okay in this context, but for the purists who can't stand it, here's an alternative solution: \documentclass{article} \usepackage{glossaries} \makeglossaries \newglossaryentry{lengthtree}% {% name={$L_t$}, text={L_t}, description={description here}, sort={L} } \begin{document} In line:$\gls{lengthtree}$. In math mode: $\gls{lengthtree}$ \printglossary[title={List of Symbols}] \end{document}  Both the above examples produce: • Of course I like best the second solution. ;-) – egreg Oct 12 '13 at 11:16 • @egreg I had a feeling you might :-) – Nicola Talbot Oct 12 '13 at 13:24 • Thx Nicola! I played with both solution and I prefer this solution. No problems so far and the package puts only symbols in the list which I have used. – user4811 Oct 12 '13 at 13:42 • Whenever I try and follow a latex solution such as this, I always get the error "glossary.sty could not be found", or similar for other packages. Why is this? – Keir Simmons May 23 '15 at 18:21 • @NicolaTalbot Is it possible to make a separate file of glossary and use it in different source tex files? – Arun Feb 15 '16 at 16:25 I think you mean something like this, that defines the commands and makes a list at the same time. \documentclass{article} \newcommand\mysymbol[3]{% \protected\gdef#1{#2}% \item[$#2$]#3} \begin{document} \section*{List of symbols} \begin{description} \mysymbol{\zzz}{z_z^z}{Something about$Zzzzz$.} \mysymbol{\Txxx}{Tx_{xx}}{The xx component of T.} \mysymbol{\vvvv}{V_2}{$V$, the second.} \mysymbol{\vvvvv}{V_3}{$V\$, the third.}
\end{description}
\section{zzz}

$\zzz=\Txxx$
where
$\vvvv=3$
and
$\vvvvv=X$

\end{document}
`
• I prefer the solution from Nicola but your solution is also nice. Thanks for your help!!! – user4811 Oct 12 '13 at 13:46
• How to make hyperlink for these \mysymbol – Renjth V Ravi Nov 11 '17 at 4:42