Hi,
In TEX, how is it possible to split a string such as \mana{X1 2W/UB R /G}
into parts and to feed them to another macro (in this case, to replace the macro invocation by something like \m{X}\m{12}\m{W/U}\m{B}\m{R/G}
), grouping by very simple rules, namely: a) consecutive digits form a group, b) a slash creates a group of two nearby characters, c) spaces are to be stripped completely?
I tried the substr
package but it wasn’t too helpful, only allowing one to find certain substrings. Hand-written loops such as
\def\lcpass#1.{}
\def\lcloop#1#2.{%
\ifempty{#2}%
#1%
\let\continue=\lcpass%
\else%
\let\continue=\lcloop%
\fi%
\continue#2.}
\def\lastchar#1{\lcloop#1.} % returns the last character of a string
fail to work when the string ends in whitespace, neither was I successful with \futurelet
.
In general, how does one approach the task of parsing strings in TEX? For example, the texmate
package allows one to write things like |1 e4 e5 Nf3 Nc6|
and automatically draws corresponding chess positions. How does it do it? What can I read about looping over characters in a string and other TEX hacks like this?