2

I want to define a macro to add an automated symbol explanation under an equation. I have to use pre-defined glossary entries because the \glsentrycurrent does not allow to add new glossaries in the document. To add the automated legend I define the newcommand \addLegeds which should basically loop over all comma separated strings passed as an argument and check if they have been used befor. If all of them were used befor no legend should be added. The macro works exept that as soon as I use the \gls or \glsdesc macros, I get the following errors:

Argument of \@tempc has an extra }. \addLegends{test1,test2} 
File ended while scanning use of \@tempc.
Missing \endcsname inserted. \addLegends{test1,test2}
Paragraph ended before \@tempc was complete. \addLegends{test1,test2}
Undefined control sequence. \addLegends{test1,test2}

The following macro has been used:

\makeatletter
\newif\ifshowleg
\newcommand{\addLegends}[1]{%
    \showlegfalse
    \@for\@tempa:=#1\do{%
        \ifnumequal{\glsentrycurrcount{\@tempa}}{0}{\showlegtrue}{}}%
    \ifshowleg  %
        with: % 
        \begin{conditions} %
            \@for\@tempb:=#1\do{ %
                \ifnumequal{\glsentrycurrcount{\@tempb}}{0}{ %
                    \gls{\@tempb} & \glsdesc{\@tempb}\\ %
                }{}}%
        \end{conditions} %
     \else \fi %
}
\makeatother

And this is a reduced version of the latex document:

\usepackage[toc]{glossaries}
\glsenableentrycount
% MACRO
    \newglossaryentry{test1}{name={test1},description={TEST1}}
    \newglossaryentry{test2}{name={test2},description={TEST2}}

\begin{document}
\addLegends{test1,test2}
\begin{end}

Does anybody know what is causing the error?

UPDATE: By using

\csname gls{\@tempb} \endcsname  \csname glsdesc{\@tempb} \endcsname %

instead of:

\gls{\@tempb} & \glsdesc{\@tempb}\\

The code compiles, but doesn't show any output. Putting the & back in between doesn't compile. Has any ideas how to fix both problems?

  • Are you looking for something like this mini-glossary example? – Nicola Talbot Dec 4 '18 at 10:11
  • This is very similar to what I'm looking for. Since I have many equations in my thesis, I want to show the symbol explanation only on the first occurance in each chapter. Since you are a quite experienced latex developer, you might can answer why the \gls and \glsdesc aren't working in my newcomand but \glsentrycurrcount does? – mboe Dec 4 '18 at 17:33
  • EDIT: I would like to use the extra command to avoid the extra length in the equations caused by the additional \gls commands. – mboe Dec 4 '18 at 17:44
  • It's difficult to tell without a complete MWE that provides your conditions environment, but I recommend using \glsentrytext and \glsentrydesc instead of \gls and \glsdesc in your list. – Nicola Talbot Dec 4 '18 at 18:22
  • Just tried the \glsentrytext and \glsentrydesc. They show the desired output, but but it seems that \glsentrytext doesn't increase the \glsentrycurrent and all symbols are shown for all equations, which is not intended. I could manually increase the counter if there is a way. – mboe Dec 4 '18 at 19:06
0

You haven't provided a definition of the conditions environment, but I'm guessing that it's a form of tabular. Since I don't know what it is, I've used \begin{tabular}{ll} in place of \begin{conditions} and \end{tabular} in place of \end{conditions}.

Loops are tricky within tabular contexts and it's often simpler to construct the contents and do the actual typesetting outside of the loop:

\documentclass{scrartcl}
\usepackage{glossaries}

\makeatletter
\newif\ifshowleg
\newcommand{\addLegends}[1]{%
  \showlegfalse
  \def\leg@list{\begin{tabular}{ll}}%
  \@for\@tempa:=#1\do{%
    \ifnumequal{\glsentrycurrcount{\@tempa}}{0}%
    {\showlegtrue\eappto\leg@list{\noexpand\gls{\@tempa} 
      \noexpand& \noexpand\glsentrydesc{\@tempa}\noexpand\\}}{}%
  }%
  \appto\leg@list{\end{tabular}}%
  \ifshowleg    
      with: \leg@list
  \fi 
}
\makeatother

\glsenableentrycount
\newglossaryentry{test1}{name={test1},description={TEST1}}
\newglossaryentry{test2}{name={test2},description={TEST2}}

\begin{document}
\addLegends{test1,test2}
\end{document}

with: test 1 TEST1 test2 TEST2

The \eappto command provided by etoolbox (which is automatically loaded by glossaries) ensures that the label is expanded, but the awkward commands like \gls need to have their expansion prevented with \noexpand.

\csname gls{\@tempb} \endcsname

The code compiles, but doesn't show any output.

It won't show any output because it forms a command name that isn't defined. \csname gls\endcsname expands to \gls but \csname gls{\@tempb}\endcsname expands to a command with a name that includes literal braces surrounding the current value of \@tempb.

|improve this answer|||||
  • That's what I was looking for. Thank you for your help and the thorough explanation! – mboe Dec 5 '18 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.