Sign up ×
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.

Is there an existing macro that "translate" tokens using some code ? For example :

\translate[\code]{caac} % => would typeset "abba"

or even better something like that :

\translate[\code]{abbcac} % => would typeset "bacbca"

The syntax for the code definition is not important.

If there is no such macro, any proposition is welcomed to obtain an efficient solution to this problem.

Thank you :)

share|improve this question
What I have seen is using etoolbox to replace certain tokens in a macro. –  canaaerus Jul 19 '12 at 16:17
This is probably doable with regular expressions. See… for a starting point. There are also regex questions and answers on stackoverflow. Regular expressions have a steep learning curve leading to a big payoff. –  Ethan Bolker Jul 19 '12 at 16:45
In fact, we need some kind of parsing strategy. Regexp can be used for such a thing but are not useful for doing the parse itself... –  Xoff Jul 20 '12 at 5:48
TikZ/PGF provides (and uses) a parser module... –  Paul Gaborit Jul 20 '12 at 5:56

2 Answers 2

up vote 5 down vote accepted

(edit: first parser of my initial answer was not using the right rules.)

Here's a TikZ/PGF solution:


\pgfparserdef{myparser}{initial}{the letter a}{b}
\pgfparserdef{myparser}{initial}{the letter b}{c}
\pgfparserdef{myparser}{initial}{the letter c}{a}
\pgfparserdef{myparser}{initial}{the character ;}{\pgfparserswitch{final}}

\pgfparserdef{myparser2}{initial}{the letter a}{\pgfparserswitch{s2}}
\pgfparserdef{myparser2}{s2}{the letter b}{ba\pgfparserswitch{initial}}
\pgfparserdef{myparser2}{s2}{the letter c}{ca\pgfparserswitch{initial}}
\pgfparserdef{myparser2}{initial}{the letter b}{c}
\pgfparserdef{myparser2}{initial}{the letter c}{b}
\pgfparserdef{myparser2}{initial}{the character ;}{\pgfparserswitch{final}}

  \expandafter\pgfparserparse\expandafter{\expandafter myparser\expandafter}#1;%

    \expandafter myparser2\expandafter}#1;%

\textbf{\pgfparserparse{myparser}caac; \pgfparserparse{myparser2}abbcac;}
\emph{\myparserone{caac} \myparsertwo{abbcac}}

The result:

parsers results

share|improve this answer
Thanks, this is a really nice solution. This would be even better if we can produce the parser automatically from the code... –  Xoff Jul 20 '12 at 6:21
@Xoff: citation: "The syntax for the code definition is not important." ;-) –  Paul Gaborit Jul 20 '12 at 6:25
You're right, and it is enough for what I need. Thank you :) –  Xoff Jul 20 '12 at 6:28

A reasonably efficient implementation of the first one is



\def\xxlc#1,#2{\lccode`#1=`#2 }




which typesets abba if run through plain TeX.

share|improve this answer
This is not as general as I hoped, but it's very nice to use the lowercase macro to achieve the goal, I like it :) –  Xoff Jul 19 '12 at 16:45

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.