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 learn to manipulate comma separated lists in Latex. To give a concreet but artificial example, I would like to have this command:

\swap{1,10}

expand to this:

{10,1}

How can I achieve it?

share|improve this question
add comment

1 Answer

I think you should ask what you want in full generality. For your concrete example,

\newcommand*\swap[1]{\doswap#1\relax}
\def\doswap#1,#2\relax{{#2,#1}}

should work, although I didn't test it.

share|improve this answer
    
I didn't know how to ask before seeing the answer. Can you elaborate on what \relax does and why do I need separate \def ? –  Łukasz Lew Sep 8 '10 at 13:39
    
Normally, \relax tells TeX to do nothing. Here, I was just using it as a delimiter for the second argument to \doswap. As for why you need the separate \def, I cannot think of a way to do this without defining a second control sequence. \swap takes a single parameter and so I defined it so that \swap{1,10} expands to \doswap 1,10\relax. Then \doswap reads 1 as its first argument and 10 as its second argument and so it expands to {10,1}, exactly as you wanted. –  TH. Sep 8 '10 at 13:50
    
This is really useful! How would one go about writing a \newcommand variant that does all this behind the scenes? –  Neil Olver Oct 27 '10 at 12:29
1  
\newcommand doesn't allow you to define a macro with delimited arguments, so you cannot. –  TH. Oct 27 '10 at 14:05
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.