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 want a simple way to have:

\reverseit{abc defg \mycommand}

yield

\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
add comment

1 Answer

up vote 9 down vote accepted
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand\reverseit { m }
  { \tl_reverse:n { #1 } }
\ExplSyntaxOff

\begin{document}
\reverseit{abc defg \LaTeX}
\end{document}

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
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.