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?

  • Is \large\pgfmathparse acceptable? – Sigur Jan 29 '18 at 13:19
  • Instead of \Large bla you should use font=\Large key on the node. – percusse Jan 29 '18 at 14:51
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

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. – user121799 Jan 30 '18 at 17:24
  • I see: lower level commands will do it. – Jens Schwaiger Jan 31 '18 at 5: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. – user121799 Jan 31 '18 at 5:22
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

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.