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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want a simple way to have:

\reverseit{abc defg \mycommand}


\mycommand gfed cba


(Yes, the original motivation is related to Hebrew and RTL languages, but that can sort of be solved using \beginR ... \endR/\beginL ... \endL where necessary without changing the language. So I'm asking the general question.)

Edit: The solution in this question is acceptable as a non-LaTeX-3 solution...

share|improve this question
Related: Most efficient way to reverse a token list (tail recursion). Please check if it is a duplicate. – Martin Scharrer Jan 6 '12 at 10:19
@MartinScharrer While that question discusses the problem, it was oriented in finding the "most efficient" way. For short token lists the solution I provided (courtesy of the LaTeX3 team) is the shortest. – egreg Jan 6 '12 at 10:49
up vote 9 down vote accepted
\NewDocumentCommand\reverseit { m }
  { \tl_reverse:n { #1 } }

\reverseit{abc defg \LaTeX}

This also honors spaces.

share|improve this answer
For completeness, mention \tl_reverse_items:n, which does not honor spaces, and puts braces around every item, but is much faster (100 times?). --- Sorry, didn't see that there were spaces in Eyal's example. – Bruno Le Floch Jan 6 '12 at 12:25

Your Answer


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.