6

I want to generate a list of technical abbreviations and acronyms that I use. Currently, I'm defining all acronyms with a macro I have designed. Now, I want the macro to call another macro, called \addtototaa, each time it is called, which adds the abbreviation to a table. This other macro is defined as

\newrobustcmd{\addtototaa}[2]{\let\OldContent\TableoftaaBody%
    \renewrobustcmd{\TableoftaaBody}{\OldContent #1 & #2 \\}}

And then I have the two additional macros

\newrobustcmd{\TableoftaaBody}{}
\newrobustcmd{\tableoftaa}[0]{\begin{longtabu}{l X}\TableoftaaBody\end{longtabu}}

where \TableoftaaBody acts as a storage container for the table contents and \tableoftaa prints the table. But when I try to compile the document I get

TeX capacity exceeded, sorry [input stack size=5000] \tableoftaa

if I call \tableoftaa (but not otherwise). Why do I get this error? I read on this blog post that this message can appear if you have an infinite recursive loop (I guess the function calls are put on the stack and that's why it's eaten up). Have I defined a recursive function here somewhere? By the way, \addtototaa is called 31 times before \tableoftaa is called, because that's how many acronyoms and abbreviations I have defined, and the two arguments I send to \addtototaa are always just literals, and no additional macros.

4
  • 2
    In order to adequately assess "why [you] get this error", it would be best to provide a minimal example that illustrates your usage.
    – Werner
    Aug 17, 2012 at 2:30
  • 2
    Is there a reason to not just use the glossaries package for this?
    – J M
    Aug 17, 2012 at 5:31
  • @JM: I actually tried to use the glossary package, but I couldn't figure out how to use it, not even after having read this guide. The .glo file that was generated was empty, which is strange, since I had used both \makeglossaries, \newglossaryentry and \printglossaries and I also have a normal, working index for which I call makeindex. Also, I dislike the fact that one have to recompile several times just to see the changes you've made, and then all abbreviations where already defined in alphabetic order. Aug 17, 2012 at 13:23
  • @JM: So, do you have any idea of what I might have done wrong, or is it impossible to tell without a code example? Aug 17, 2012 at 15:38

2 Answers 2

9

You can use the etoolbox package to append content to an existing macro using \gappto (works similar to \g@addto@macro). I've simplified your code snippets so I can make a MWE, but it would most likely be easy to extend to your usage:

enter image description here

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\let\TableoftaaBody\relax
\newcommand{\tableoftaa}{\begin{tabularx}{\textwidth}{l X}\TableoftaaBody\end{tabularx}}

\newcommand{\addtototaa}[2]{%
  \gappto\TableoftaaBody{#1 & #2 \\}%
}
\begin{document}
\addtototaa{1}{\lipsum*[1]}
\addtototaa{2}{\lipsum*[2]}
\addtototaa{3}{\lipsum*[3]}
\noindent\tableoftaa
\end{document}
1
7

@Werner has shown how to do what you want, so I'll answer the other question: Yes you have defined an infinite recursive loop.

\newrobustcmd{\TableoftaaBody}{}

\newrobustcmd{\addtototaa}[2]{\let\OldContent\TableoftaaBody%
\renewrobustcmd{\TableoftaaBody}{\OldContent #1 & #2 \\}}

so \TableoftaaBody is eempty then you do

 \addtototaa{1}{2}

so \OldContent is \let to \TableoftaaBody so is defined empty and \TableoftaaBody is defined to be

\OldContent 1 & 2 \\

Then you go

 \addtototaa{3}{4}

so \OldContent is \let to \TableoftaaBody so is defined to be \OldContent 1 & 2 \\ (oooops!!!!) and \TableoftaaBody is defined to be

\OldContent 3 & 4 \\

so at this point if you expand \TableoftaaBody the first token you see is \OldContent so that expands but the first token of that expansion is \OldContent again and so you are in an infinite loop. Each time you expand \OldContent you see \OldContent again but push further tokens on the input stream after that, so finally you fill up all available memory.

1
  • I see. I got the idea to do that from the TeX Wikibook, but I guess the example featured there never intended you to use the same trick twice to extend a macro. Aug 17, 2012 at 13:08

You must log in to answer this question.

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