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 been trying to understand the notion of recursion in TeX - not easy given that my background is the high-level stuff in LaTeX. The only examples I can (just about) understand are very basic for-loop constructions, but I don't really feel like I have a picture for what expansion is or how it works.

I'd like to write a function that will take as its argument a group

{{#1}{#2}...{#n}}

where n can take any positive-integer value, and return the pair of groups

{#1}{{#2}...{#n}},

when n is at least 2, and {#1} otherwise.

This seems like it should be reasonably simple to do, but I don't know how I'd go about it.

edit: I want to use this function in beamer to progressively overlay lines inside AMSMath environments such that

  • Vertical spacing is preserved;
  • Equation numbering (where applicable) appears in-time with the displayed lines of the equation array, and is preserved (i.e. not incremented) between overlays.
share|improve this question
    
Where does the recursion come in? –  Joseph Wright Apr 20 '11 at 17:48
    
@joseph, I've edited the question to include some context. I initially had it in there, but must have edited the question after I decided what tags it should have. –  Nick Loughlin Apr 20 '11 at 19:07
add comment

1 Answer

up vote 4 down vote accepted
\catcode`@=11
\def\splitarg#1{\@splitarg#1\@nil}
\def\@splitarg#1#2\@nil{{#1}\if\relax\detokenize{#2}\relax\else{#2}\fi}
\catcode`@=12

\edef\x{\splitarg{{a}{b}{c}}}\show\x
\edef\x{\splitarg{{a}}}\show\x

Requires e-TeX, so you must process the example with pdftex, xetex, or luatex (not Knuth's tex). The test \if\relax\detokenize{#2}\relax is true when #2 is empty (that is, when there is only one element in the list you pass to \splitarg) and e-TeX finds nothing in the "true" part; otherwise e-TeX braces what remains in the original list.

share|improve this answer
    
Would be more or less my approach too: the test is doable without e-TeX, but why tie one hand behind your back :-) –  Joseph Wright Apr 20 '11 at 18:16
    
"you must process the example with pdftex" Works fine with xetex. –  morbusg Apr 20 '11 at 18:27
    
@morbusg: I meant "pdftex" as opposed to "tex", but you're right. –  egreg Apr 20 '11 at 19:22
    
@Joseph: my opinion is the same; this test is more robust and expandable, without the need of taking into account many possibilities as it would be the case with original TeX. –  egreg Apr 20 '11 at 19:25
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.