# Parsing and programming in TikZ

Background: I'be been reading the PGF manual (keys section) and today the TeX Book just arrived in the mail and just started reading chapter 7 (How TeX reads what you type) -- feeling like Mickey Mouse in "The Sorcerers Apprentice".

I want (but this is not the question) to do a routine that does something like that:

   input:   a,b ; c,d ; e,f.
output:  a\nodepart{b} && c\nodepart{d} && e\nodepart{f}\\


My idea was to use the PGF parser to read the input. If I were using the languages I know (old Basic, Python, Maple script) I would save intermediary results in a variable but for what I understood so far LaTeX/TeX/ and TikZ/PGF do not work in this way. There is no string manipulation routines. I know we can store stuff into pgf keys. Before you stop reading I better ask my questions:

1. Are there any examples of (LaTeX/TeX/ and TikZ/PGF) code that reads some input of variable lenght possibly more than ten and spits some other code?
2. With the example above in mind, which references should I read to be able to implement the routine in an efficient way?

Post-script:

1. Reading the suggested tags, I just learn about the LaTeX parse package, will check it also.
2. @egreg: The ultimate goal will be to define a bimatrix environment in TikZ to display two-player games in normal form. The idea is to create outputs similar to this http://www.maths.lse.ac.uk/Personal/stengel/bimatrixgame/example.pdf but with the intuitive and human-like language we see in TikZ constructs.
-
In How do I split a string? you have other pointers –  JLDiaz May 12 '14 at 15:47
@JLDiaz: Thanks! Please correct me if I am wrong. The accepted answer to the question you mentioned works only if the input has a fixed number of parts but the second answer does the job, right? –  Sergio Parreiras May 12 '14 at 15:55
Yes, the second one uses some low-level tricks to adapt itself to an arbitrary number of words. You can have fun trying to understand this solution armed with your new book of spells :-) –  JLDiaz May 12 '14 at 16:00
It's important to know where and how you want to use this. Can you make an example? –  egreg May 12 '14 at 16:16
You may be able to use any of the options listed in How to iterate over a comma separated list? –  Werner May 12 '14 at 16:31

This is an adaptation of the second answer of the question How do I split a string? to your problem. It is not exactly what you want, but it can help to get you on the way.

\documentclass[a4paper]{article}
\makeatletter
\def\processArg#1,#2{%
#1 nodepart #2
}
\def\myutil@empty{}
\def\severalparts#1;#2\@nil{%
\def\NextArg{#2}%
\processArg#1%
\ifx\myutil@empty\NextArg
\let\next\@gobble
\else XX
\fi
\next#2\@nil
}%
\def\ProcessString#1{%
\let\next\severalparts
\next#1;\@nil %
}%
\makeatother

\begin{document}
\ProcessString{a,b;c,d;e,f}
\end{document}


This the pdf output:

-
@SergioParreiras And, as Mickey the sorcercer learned the hard way, always remember to include a termination condition in your loops. Otherwise, infinite expansion will lead to overflow :-) –  JLDiaz May 13 '14 at 8:19

If you are willing to slightly change the input format a simpler solution is to use \foreach:

## Notes:

• This may or may not work for you depending on your particular application.
• The intermediate step using an \edef allows this to be used with a string, or a macro defined string. See TikZ \foreach loop with macro-defined list for more details.

## Code:

\documentclass[a4paper]{article}
\usepackage{tikz}

\newcommand*{\ProcessString}[1]{%
\edef\StringToProcess{#1}%
\foreach \x/\y in \StringToProcess {%
\x\ nodepart \{\y\} XX
}%
}%

\newcommand*{\MyStrings}{x/y,w/u,1/2}%

\begin{document}
\ProcessString{a/b,c/d,e/f}

\bigskip
\ProcessString{\MyStrings}

\end{document}

-
Why \StringToProcess intermediate variable? You can use \foreach ... in {#1}. There are unknown caveats? –  JLDiaz May 12 '14 at 16:52
@JLDiaz: The intermediate step is useful as you can use it with a direct string, or a string defined via a macro. See updated solution. –  Peter Grill May 12 '14 at 16:58
@PeterGrill: Neat! But is there a way we can have the characters \  and { in the output? That is: a\nodepart{b} instead of a nodepart b? –  Sergio Parreiras May 12 '14 at 17:03
@SergioParreiras: Sure, just add a \{ and \}. Have updated solution. However, as I warned in the solution, this may or may not work for you depending on the actual application. –  Peter Grill May 12 '14 at 17:12
@Sergio I haven't tested, but try saying \string\nodepart or \cs{nodepart} in place of the plain nodepart. –  Sean Allred May 12 '14 at 17:14