4

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.
7
  • 1
    In How do I split a string? you have other pointers Commented 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? Commented 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 :-) Commented 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? Commented 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? Commented May 12, 2014 at 16:31

2 Answers 2

3

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:

Result

1
  • @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 :-) Commented May 13, 2014 at 8:19
2

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

enter image description here

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} 
5
  • 1
    Why \StringToProcess intermediate variable? You can use \foreach ... in {#1}. There are unknown caveats? Commented 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. Commented 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? Commented 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. Commented 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. Commented May 12, 2014 at 17:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.