8

Using glossaries commands in align environment seems to screw up the tag alignment.

How can I use gls{...} commands in align environments without the tag position being altered?

Consider the following code:

\documentclass[a4paper,10pt]{article}

\usepackage{amsmath,amsfonts,glossaries}
\usepackage[parfill]{parskip}

\newacronym{E}{\ensuremath{E}}{Energy}
\makeglossaries

\begin{document}
    Standard gather environment
    \begin{gather}
        E = mc^2
    \end{gather}
    Glossaries Command in Align: 
    \begin{align}
        \gls{E} &= mc^2
    \end{align}
    Same align environment, with the `gls` command missing:
    \begin{align}
        E &= mc^2
    \end{align}
\end{document}

Which yields the following result, alignment differences should be clear:

Output

2 Answers 2

7

The explanation is already given in egreg's answer. Package amsmath processes the equations twice. First is measures the equation in order to know, whether it has to shift the equation number for wide equations. Then the equation is actually set.

\gls{E} changes its appearance. In the measuring step it is called the first time with the long form. Then in the second step, it generates the short form, confusing amsmath that had expected the long form.

The following example patches \glsunset that sets a flag, if the entry is set the first time. The measuring phase can be detected via switch \ifmeasuring@:

\documentclass[a4paper,10pt]{article}

\usepackage{amsmath,amsfonts,glossaries}
\usepackage[parfill]{parskip}

\newacronym{E}{\ensuremath{E}}{Energy}

\makeglossaries

\makeatletter
\let\org@glsunset\glsunset
\renewcommand*{\glsunset}[1]{%
  \ifmeasuring@
  \else
    \org@glsunset{#1}%
  \fi
}
\makeatother

\begin{document}
    Standard gather environment
    \begin{gather}
        E = mc^2
    \end{gather}
    Glossaries Command in Align:
    \begin{align}
        \text{\gls{E}} &= mc^2
    \end{align}
    Same align environment:
    \begin{align}
        \text{\gls{E}} &= mc^2
    \end{align}
\end{document}

Result

2
  • I didn't know about \ifmeasuring@. I'll add a check for it in the next version of glossaries. Thanks, Heiko. Commented Sep 23, 2013 at 7:41
  • Thanks Heiko. Although in practice I wouldn't use the long form within equations (my presumption is that the 'first use', in reality, would come beforehand in a document), that is easy to fix and your answer is perfect in the case of this question. Cheers. Commented Sep 23, 2013 at 22:10
6

The material in the align environment is processed twice, one for measuring it and the second one for actually typesetting it.

Since \gls{E} didn't appear earlier, in the first pass it is expanded to the long form and this is what LaTeX uses for deciding the column width; but in the second pass the acronym has already appeared (in a box that's discarded), so it's typeset in the short form.

It makes little sense to use \gls in an align, doesn't it?

3
  • 1
    Cheers. Why does it not make sense? What about a large book that needs to keep consistency in symbols and math terms, or that needs a glossaries table at the front listing all of the math terms and symbols used? Commented Sep 22, 2013 at 21:39
  • @ADP Do you really want that the formula reads "Energy (E)=mc^2"? I'd rule this as plainly wrong.
    – egreg
    Commented Sep 22, 2013 at 21:41
  • of course. short terms in the align environment. now that you explained the mechanism of how it works, I didn't know two passes were done. Using \glsunset{...} prior fixes the issue outlined in initial question. Commented Sep 22, 2013 at 21:47

You must log in to answer this question.

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