1

I would like to use a foreach loop to create a list of acronyms in a LaTeX document. I expect the code should look something like this but it does not work (it nearly works):

\documentclass{article}
\usepackage{pgffor}
\usepackage[acronym]{glossaries}

\makeglossaries

\foreach \x/\y in { DOC/document, ACR/acronym, VV/very very, } {\newacronym{\x}{\x}{\y}}

\begin{document}

\section{Document Body}

A \gls{DOC} with lots of unexplained \glspl{ACR} is \gls{VV} annoying.

\printglossary[type=\acronymtype]

\end{document}

For reference, here is an example of a working foreach loop and acronym glossary:

\documentclass{article}
\usepackage{pgffor}
\usepackage[acronym]{glossaries}

\makeglossaries

\newacronym{DOC}{DOC}{document} \newacronym{ACR}{ACR}{acronym} \newacronym{VV}{VV}{very very}

\begin{document}

\section{Document Body}

A \gls{DOC} with lots of unexplained \glspl{ACR} is \gls{VV} annoying.

\section{Foreach Loop}

\foreach \x/\y in { DOC/document, ACR/acronym, VV/very very, } {\textbf{\x} \y\}

\printglossary[type=\acronymtype]

\end{document}

  • Very helpful, thank you. I understand your argument about writing out the acronyms but it seems cumbersome to write out the syntax every time (particularly for standard glossary entries), especially if you have a lot of entries. – GeographyJames Dec 01 '22 at 11:34
  • I understand but many LaTeX editors allow templates to be scripted so that it automatically places \newacronym{}{}{} and maybe even CCs the first to the second argument. That said, you could just do \newcommand*\myNewAcronym[3][]{\newacronym[#1]{#2}{#2}{#3} and then you only have to use \myNewAcronym{DOC}{document} without having to repeat DOC. – Qrrbrbirlbel Dec 01 '22 at 11:41

1 Answers1

1

You would need to expand \x and \y before you hand them off to \newacronym.

In this case, it would be as simple as

\foreach \x/\y in {
  DOC/document,
  ACR/acronym,
  VV/very very%
}{%
  \edef\z{\noexpand\newacronym{\expandonce\x}{\expandonce\x}{\expandonce\y}}%
  \z
}

The name of \z doesn't matter (it even could be \x or \y) since it is forgotten after the loop.

Be careful with pgffor however, since its body is grouped, local assignments will not survive. The glossaries package evidently does them globally, though.


With LaTeX3 you can use \exp_args:Nooo which would expand each of the three arguments once before using them with \newacronym:

\ExplSyntaxOn
\NewDocumentCommand\MakeAcronym{ m m }{
  \exp_args:Nooo \newacronym#1#1#2
}
\ExplSyntaxOff

\foreach \x/\y in { DOC/document, ACR/acronym, VV/very very% }{% \MakeAcronym{\x}{\y}% }


If you don't have to use the full power of \foreach (mainly calculating values and remembering others) you can use the .list handler which would still allow the ... syntax (not that it is really applicable here).

This does expand the values before it is handed off to /GJ/new acros which is why you simply can (and must) use #1 and #2 then.

Code

\documentclass{article}
\usepackage{pgffor}
\usepackage[acronym]{glossaries}

\makeglossaries

\pgfkeys{ GJ/new acros/.code args={#1/#2}{% \newacronym{#1}{#1}{#2}% }, GJ/new acros/.list={ DOC/document, ACR/acronym, VV/very very% } }

% \ExplSyntaxOn % \NewDocumentCommand\MakeAcronym{ m m }{ % \exp_args:Nooo \newacronym#1#1#2 % } % \ExplSyntaxOff

% \foreach \x/\y in { % DOC/document, % ACR/acronym, % VV/very very% % }{% % \MakeAcronym{\x}{\y}% % }

% \foreach \x/\y in { % DOC/document, % ACR/acronym, % VV/very very% % }{% % \edef\z{\noexpand\newacronym{\expandonce\x}{\expandonce\x}{\expandonce\y}}% % \z % }

\begin{document} \section{Document Body} A \gls{DOC} with lots of unexplained \glspl{ACR} is \gls{VV} annoying. \printglossary[type=\acronymtype] \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821