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?


  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.
  • 1
    In How do I split a string? you have other pointers
    – JLDiaz
    May 12, 2014 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? May 12, 2014 at 15:55
  • 2
    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, 2014 at 16:00
  • 1
    It's important to know where and how you want to use this. Can you make an example?
    – egreg
    May 12, 2014 at 16:16
  • 2
    You may be able to use any of the options listed in How to iterate over a comma separated list?
    – Werner
    May 12, 2014 at 16:31

2 Answers 2


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.

  #1 nodepart #2
 \else XX
   \next#1;\@nil %


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, 2014 at 8:19

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

enter image description here


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



    \foreach \x/\y in \StringToProcess {%
        \x\ nodepart \{\y\} XX




  • 1
    Why \StringToProcess intermediate variable? You can use \foreach ... in {#1}. There are unknown caveats?
    – JLDiaz
    May 12, 2014 at 16:52
  • 2
    @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. May 12, 2014 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? May 12, 2014 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. May 12, 2014 at 17:12
  • 1
    @Sergio I haven't tested, but try saying \string\nodepart or \cs{nodepart} in place of the plain nodepart. May 12, 2014 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.