5

I have a rather large number of nodes n1, n2,...,n41, all of them containing expresions like \textcolor{c}{blabla} and \large etc. It seem when storing all these texts in an array of the form described in List of strings for tikzpicture namely

\documentclass{article}

\usepackage{tikz}
\begin{document}
\newcommand\johnlist{{"ala","bla","cla"}}

\begin{tikzpicture}
\node[shape=circle,draw=black] at (0,0) {\pgfmathparse{\johnlist[1]}\pgfmathresult};
\end{tikzpicture}

\end{document}

does not work if for example "bla" is replaced by \Large bla.

So the question is how to create lists or arrays of TeX-compatible strings?

bmv
  • 3,588
Jens Schwaiger
  • 224
  • 1
  • 2
  • 8

3 Answers3

4

Something like this?

\documentclass{article}
\usepackage{tikz}
\newcommand{\green}[1]{\textcolor{green}{#1}}   

\begin{document}
\newcommand\johnlist{{"\noexpand\Huge ala","\noexpand\green{{bla}}","\noexpand\bfseries\noexpand\Large cla"}}

\begin{tikzpicture}
\foreach \i in {0,1,2}
{
\node[shape=circle,draw=black] at (\i,0) {\pgfmathparse{\johnlist[\i]}\pgfmathresult};
}
\end{tikzpicture}

\end{document}

enter image description here

  • This works, but it seems that
    1. \noexpand\textbf{bla} gives no bold text and
    2. \noexpand\textcolor{green}{bla} gives error messages.
    – Jens Schwaiger Jan 30 '18 at 17:07
  • @JensSchwaiger I modified the answer. And sorry, your point 2) did not show. (This happened already before and is really annoying.) Anyway, sorry for my comment. –  Jan 30 '18 at 17:24
  • I see: lower level commands will do it. – Jens Schwaiger Jan 31 '18 at 05:14
  • @JensSchwaiger No, the difference are the brackets. \noexpand\textbf{{...}} also works. You can either use a command that does not require brackets such as \bfseries (with syntax {\bfseries ...}, or commands like \textbf, but then you have to put two braces, \textbf{{...}} instead of \textbf{...}. This is because the \noexpand makes TeX to skip over the nex command. –  Jan 31 '18 at 05:22
4

My impression is that the handling of arrays in PGF is quite fragile.

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\setlist}{mm}
 {
  \clist_clear_new:c { l_jens_#1_array_clist }
  \clist_set:cn { l_jens_#1_array_clist } { #2 }
 }
\NewExpandableDocumentCommand{\listitem}{mm}
 {
  \clist_item:cn { l_jens_#1_array_clist } { #2 }
 }
\ExplSyntaxOff


\begin{document}

\setlist{john}{\textbf{ala},\Huge bla,\large cla}

\begin{tikzpicture}
\node[shape=circle,draw=black] at (0,0) {\listitem{john}{1}};
\node[shape=circle,draw=black] at (1,0) {\listitem{john}{2}};
\node[shape=circle,draw=black] at (2,0) {\listitem{john}{3}};
\end{tikzpicture}

\bigskip

\begin{tikzpicture}
\foreach \x in { 0,1,2 } {
  \node[shape=circle,draw=black] at (\x,0) {\listitem{john}{\x+1}};
}
\end{tikzpicture}

\end{document}

Note that you can do arithmetic operations on the second argument to \listitem. The indexing starts from 1.

enter image description here

egreg
  • 1,121,712
3

You could hack the random list stuff, although specifying items requires braces rather than commas, and indexing starts at 1:

\documentclass[tikz,border=5]{standalone}
\def\pgfmathrandomlistitem#1#2{\pgfmathparse{int(#2)}%
  \csname pgfmath@randomlist@#1@\pgfmathresult\endcsname}

\pgfmathdeclarerandomlist{list1}{%
  {\tiny\textcolor{red}{ala}}
  {\small\textcolor{green}{bla}}
  {\large\textcolor{blue}{cla}}
}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,2}
  \node [circle, draw] at (0, \i) {\pgfmathrandomlistitem{list1}{\i+1}};
\end{tikzpicture}
\end{document}

enter image description here

Mark Wibrow
  • 70,437
  • In [1]: https://drive.google.com/open?id=1V-o2E3tdh7VflV1IVXSk8QAbpPhHfqv2

    you may find a ``real example´´ processed first by egreg´s method and then -- by easy modifications -- by Mark Wibrows´ method.

    – Jens Schwaiger Jan 31 '18 at 12:15