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've got an expansion issue with the following code:

\def\sizes{huge, LARGE, Large, large, normalsize, small, footnotesize, scriptsize, tiny}
\forcsvlist\sampletext{\sizes}

My problem is that \sizes should be expanded before being passed to \forcsvlist, otherwise \sampletext will simply receive the list and not know how to parse it.

How can I make it so that \sizes is expanded?

Note: Putting the list hardcoded in the \sampletext call works, but I don't want to do that because I might override \sizes with other values.

share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

This is the standard way:

\expandafter\forcsvlist\expandafter\sampletext\expandafter{\sizes}

The first \expandafter expands the second one, which in turn expands the third and this last does the job, expanding \sizes. The primitive \expandafter expands to nothing, after having caused the expansion of the second token after it; just jump over one token, when checking the action of \expandafter. It's a bit more complicated when there are arguments, but \expandafter and \sizes don't take them. The expansion performed by \expandafter is only one step: after

\def\y{\z} \def\z{a}

saying

\expandafter\x\y

is like saying

\x\z

In your case the three \expandafter commands vanish, but TeX now has

\forcsvlist\sampletext{huge,LARGE,...}

and continues expanding from here.

Another trick is

\begingroup\edef\x{\endgroup\noexpand\forcsvlist\noexpand\sampletext{\sizes}}\x

which works because the expansion of \sizes consists of characters; it wouldn't if \sizes had been defined as

\def\sizes{\huge,\LARGE,...}
share|improve this answer
    
\expandafter is still something a bit hard for me to grasp. I don't get why I have to use 3 of them. –  ℝaphink Jul 5 '11 at 15:48
    
See edited answer –  egreg Jul 5 '11 at 15:58
    
Thank you very much :-) –  ℝaphink Jul 5 '11 at 16:02
    
Any explanation about the "Another trick", I don't quite understand them, thanks. –  ollydbg23 May 8 at 2:52
1  
@ollydbg23 This defines \x to expand to \endgroup\forcsvlist{<full expansion of \sizes>}; executing \x will close the group started with \begingroup and do \forcsvlist. –  egreg May 8 at 7:25
show 2 more comments

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.