Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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 '12 at 2:30
2  
Is there a reason to not just use the glossaries package for this? –  J M Aug 17 '12 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. –  StrawberryFieldsForever Aug 17 '12 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? –  StrawberryFieldsForever Aug 17 '12 at 15:38
add comment

2 Answers

up vote 7 down vote accepted

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}
share|improve this answer
    
Thank you very much, \gappto definitely did the trick. –  StrawberryFieldsForever Aug 17 '12 at 12:59
add comment

@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.

share|improve this answer
    
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. –  StrawberryFieldsForever Aug 17 '12 at 13:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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