I want to retrieve a member of a non-numeric array passed to a TikZ pic as an argument, to use it as a node label. \foreach
easily does it while iterating over the array, but I cannot do it for the individual elements. The following are my two attempts that fail miserably:
\documentclass[tikz]{standalone}
\begin{document}
\tikzset{
pics/mypic/.style n args={1}{
code={
\foreach \a [count=\i] in {#1}{
\node at (\i, 1) {$\a$};
}
% Attempt 1: Doesn't work
% \node at (1, 2) {$#1[0]$};
% Attempt 2: Doesn't compile
% Error: Undefined control sequence. ... Missing $ inserted. ...
\node at (2, 2) {$\pgfmathparse{{#1}[0]}\pgfmathresult$};
}
}
}
\tikz{\pic {mypic={\mathtt{a},\mathtt{b},\mathtt{c}};}}
% \tikz{\pic {mypic={a,b,c};}} % It's the same for this one.
% \tikz{\pic {mypic={0,1,2};}} % Attempt 2 works with this one,
% \tikz{\pic {mypic={0,a,2};}} % but not with this one ("Unknown function `a').
\end{document}
Output:
I want to be able to somehow draw a node with the label #1[0]
. Regarding Attempt 2, I noticed that it yields the same errors also with the text arrays. It works fine for the all-numeric arrays, but hybrid arrays are also troubling, even if the accessed element is numeric.
"..."
, and that appears to be required to avoid blow-ups (see also p. 1026). There also appeared to be a scoping issue if\pgfmathparse
is used too early/too far away from\pgfmathresult
. I assume that\pgfmathparse
/\pgfmathresult
is also used internally, so you have to be careful that your use does not get overwritten. – moewe Jul 5 '20 at 13:51evaluate=\x as \myvar using ({\radi[\x]})
and then\myvar
that is unchanged). – moewe Jul 5 '20 at 13:51\pgfmathresult
being overwritten see also tex.stackexchange.com/q/497648/35864, tex.stackexchange.com/q/9988/35864, tex.stackexchange.com/q/510584/35864 – moewe Jul 5 '20 at 14:41\foreach
statement using the array. Finally, it's hard to accept that it cannot be done without the quotes, when un-quoted non-numeric elements can be captured/stored by the\foreach
's iteration variable just fine. – Utkan Gezer Jul 20 '20 at 20:22"..."
. I suspect some things may work without quotes, but that would be accidental and not something you can in general rely on. But I no next to nothing about TikZ so take that with a grain of salt. – moewe Jul 20 '20 at 20:24