22

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.

5
  • 1
    i may be misinterpreting something, but this seems like a compelling reason to use \ensuremath. Commented Oct 11, 2013 at 16:42
  • 1
    By symbol directory, do you mean list of symbols?
    – jub0bs
    Commented Oct 11, 2013 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
    Commented Oct 11, 2013 at 17:08
  • @Jubobs: yes, sorry I am not a native speaker...
    – user4811
    Commented Oct 11, 2013 at 17:09
  • 1
    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. Commented Oct 11, 2013 at 17:16

2 Answers 2

20

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:

Image of document

6
  • 2
    Of course I like best the second solution. ;-)
    – egreg
    Commented Oct 12, 2013 at 11:16
  • @egreg I had a feeling you might :-) Commented Oct 12, 2013 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
    Commented Oct 12, 2013 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? Commented May 23, 2015 at 18:21
  • @NicolaTalbot Is it possible to make a separate file of glossary and use it in different source tex files?
    – Arun
    Commented Feb 15, 2016 at 16:25
12

I think you mean something like this, that defines the commands and makes a list at the same time.

enter image description here

\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}
2
  • I prefer the solution from Nicola but your solution is also nice. Thanks for your help!!!
    – user4811
    Commented Oct 12, 2013 at 13:46
  • How to make hyperlink for these \mysymbol Commented Nov 11, 2017 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .