Sign up ×

In an attempt to specify a seemingly endless amount of arguments to a macro, I would like to supply only one #1, where #1 is a string that contains a bunch of specific characters. For example


Then, I'd like to sequentially parse #1 on a character-by-character basis and do something based on the character. As in the example above, characters could be anything from numbers 0...9, letters a...z and A...Z (so conditioning should be case sensitive) or symbols -,/,...

The bigger picture here is create a macro that uses chessfss to typeset a chess board or arbitrary shape using FEN notation. The symbol - will denote a blank square, numbers will denote empty squares (with no piece on it), / will denote the next rank, and letters will denote the colour (via capitalization) and type (via the specific letter) of the piece.

The code provided by the skak package is limited to a fixed 8-rank (or file) chess board. Specifically, skak provides \InitBoard that is defined as


where each one of the 8 arguments is processed in a very "nested fashion" using (I'm skipping some intermediate steps for the sake of brevity):



I would like to steer clear from this since it is not entirely reader-friendly and somewhat unappealing, and rather use some "case statement", where I condition on the characters in the macro argument, one by one. The boolexpr package provides such a switch environment, but I'm not sure how to extend it to work in the way I want to.

share|improve this question
Try parser module of pgf package. It defines a finite state machine. –  Leo Liu Nov 1 '11 at 16:16

1 Answer 1

up vote 5 down vote accepted

I have implemented character or token parsers like this in my tikz-timing, collcell and the ydoc packages. They are all available under and on CTAN, of course. Basically you are either use \futurelet or a macro argument (#1) to read the next token or character and branch accordantly, then call your input collection macro recursively.

In collcell I added a case statement for tokens which could be changed to take a character instead. The current syntax is:


where the token was read before using \futurelet\collect@cell@lettoken\next. See the source code of the case statement for more details.

In ydoc I started with a large \ifcase statement but then changed it to a look-up table-like feature: There is one macro define for every input character, e.g. \@namedef{mypkg@char@<char>}{<code for this character>}, and then the parser macro only need to read the next character and use \@nameuse{mypkg@char@#1} (or \csname directly). As long you don't have any special tokens like {, } and spaces this works fine. Otherwise you need to use \futurelet/\@ifnextchar before to check for these first.

It is also possible to always use tokens with \futurelet and use \meaning to build the macro names, i.e. \@nameuse{mypkg@char@\meaning\@let@token}. This works with all tokens, normal or special. The handler macro should then remove the token from the input stream.

A simply character-by-character parser based on this looks like this:


\defcharcode{q}#1{% reads further character!




This prints: (1) (j) (q4) (.) (.) (.).

The recursive call is placed in every character handler so that there is nothing after it so that it can read further input character if required.

share|improve this answer
How would you extend the last MWE to incorporate an "else" to the "case" statement? I'm thinking to check whether mymacro@char@#1 exists, but not sure how. –  Werner Nov 2 '11 at 2:07
Withe e-TeX \ifcsname mymacro@char@#1\endcsname \@nameuse{mymacro@char@#1\expandafter} \else <else> \fi, otherwise \expandafter\ifx\csname mymacro@char@#1\endcsname\relax <else> \else \@nameuse{mymacro@char@#1\expandafter}\fi. –  Martin Scharrer Nov 2 '11 at 6:43

Your Answer


