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 would like to keep track of all the \AddRef commands in a document and then process them one at a time, for instance print them in the document on one line per value. It seems that this should be doable as the functionality is not that different than list of figures that, but I can' seem to get this to work. I did not even attempt the \foreach command required at the end of the file so if someone knows how to do that that would be appreciated.

\documentclass[12pt]{article}
\begin{document}

\newtoks{\Ref}
\newcommand{\AddRef}[1]{%
    \let\OldRef=\the\Ref\relax
    \let\Ref=\the\OldRef{#1}\relax
}

\AddRef{Ref 1}
\AddRef{Ref 2}
\AddRef{Ref 3}

The list of Ref are:
\the\Ref

\end{document}

At the end, I would like to process the list, say to print a line at a time:

Ref 1

Ref 2

Ref 3


The original question above required the case where this list of strings is a global macro so that it is complete at the end of the document. Another application requires that this list of strings be local to a scope so a local version would also be useful.

share|improve this question
    
Related Question: Long List of Files Names with Paths. –  Peter Grill May 2 at 18:16
add comment

3 Answers 3

up vote 8 down vote accepted
\documentclass[12pt]{article}
\newcommand\AddRefList{}
\newcommand\AddRef[1]{\edef\AddRefList{\AddRefList#1\endgraf}}
\begin{document}

\AddRef{Ref 1}
\AddRef{Ref 2}
\AddRef{Ref 3}

The list of Ref are:

\AddRefList

\end{document}
share|improve this answer
    
This worked as I needed. Thanks. –  Peter Grill Apr 7 '11 at 6:51
    
I think the \edef be an \xdef as then it works independent of where \AddRef is called from. –  Peter Grill Sep 23 '12 at 8:35
    
one important rule: use global definitions only when they are really needed ... –  Herbert Sep 23 '12 at 8:48
    
Ok, point taken. Was using this in a possible solution to Print macro output instead of executing and took me some time to figure out what was going on... –  Peter Grill Sep 23 '12 at 8:55
add comment

The \let\OldRef=\the\Ref doesn't work because \OldRef is then simply a copy of \the. Also note that \Ref here stands for \toks<number> and coping it using \let would only let both macros point to the same token register.

You can accumulate code using the \g@addto@macro macro:

\def\Ref{}
\makeatletter
\newcommand{\AddRef}[1]{%
    \g@addto@macro\Ref{{#1}}%
}
\makeatother

This redefines a macro, which is less efficient than using a token register, but this should be OK in your case.

Instead of using a for loop I would simply add the required macros as part of the definition of \Ref, so that you simply have to use the macro. The ToC and List of Figures is generated by writing such code into an external file instead.

share|improve this answer
    
Both of these work. But in which circumstances is one better than the other? I am not too concerned about speed. –  Peter Grill Mar 29 '11 at 4:26
    
And the reason I wanted to use a for loop at the end, is that I have not yet figured out exactly how I am going to do the processing that is needed, so had planned on temporarily outputing a \todo statement for now and coming back later to fix it. That way I did not need to change the collection of these, just the processing of the \Ref(s). I could of course collect the \todo as part of the list as it is created, and then later change this, but that solution somehow seems wrong. –  Peter Grill Mar 29 '11 at 5:02
add comment

If you want a local version of \g@addto@macro without using up a new token register you could use something like

\long\def\@addto@macro#1#2{%
\begingroup
\toks@\expandafter{#1#2}%
\expandafter\endgroup\expandafter
\edef\expandafter#1\expandafter{\the\toks@}%
}

this uses the scratch register \toks@ without affecting its existing contents, it is basically \g@addto@macro but using \edef rather than \xdef and with a few extra \expandafter to compensate.

share|improve this answer
    
On the other hand, this particular application seems to require global definitions, since \AddRef might be used inside an environment. –  egreg Oct 5 '12 at 21:44
1  
@egreg I was asked by the OP in chat for a local version of g@addto@macro and I always do what I'm told:-) –  David Carlisle Oct 5 '12 at 22:02
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.