array of coordinates and array of strings

I want to use an array of coordinates and an array of names, both arrays being declared as Latex macros. The code below works but it I do not understand why the first array \Tpos is declared with single braces {} while the second has to be declared with double braces {{}}. Another strange issue is that the loop index ranges from 1 to 3m (see the \draw (pt1) -- (pt2) -- (pt3) command), while the second array is indexed from 0 to 2 (see the \Tname[\i-1] command). Oh well...

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\Tpos{( 0.5, 0.0), ( 2.0, 0.0), (-1.0,-1.0)}
\newcommand\Tname{{"A", "B", "C"}}
\begin{document}
\begin{tikzpicture}
\foreach \pos [count=\i] in \Tpos {
\node [draw=black,circle,inner sep=2pt,minimum size=4mm] (pt\i) [at=\pos] {\pgfmathparse{\Tname[\i-1]}\pgfmathresult};
}
\draw (pt1) -- (pt2) -- (pt3);
\end{tikzpicture}
\end{document}


Welcome! It may be impossible to answer the question in the way you seem to imagine since you are comparing two very different constructions, foreach loops and arrays. In arrays the counting starts from zero while the count in foreach starts at 1 by default but you can change that:

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{calc} % not needed here
\newcommand\Tpos{( 0.5, 0.0), ( 2.0, 0.0), (-1.0,-1.0)}
\newcommand\Tname{"A", "B", "C"}
\begin{document}
\begin{tikzpicture}
\foreach \pos [count=\i starting from 0] in \Tpos {
\node [draw=black,circle,inner sep=2pt,minimum size=4mm] (pt\i)
[at=\pos] {\pgfmathparse{{\Tname}[\i]}\pgfmathresult};
}
\draw (pt0) -- (pt1) -- (pt2);
\end{tikzpicture}
\end{document}


Now the count starts at 0. As for the double braces, this is because one comes from the \newcommand (or \def) syntax, and the other from the array function of pgf. In the example above I have wrapped it around \Tname in \pgfmathparse{{\Tname}[\i]} but the short answer is: yes, you need "double" braces here in one way or the other.

ADDENDUM: As for the additional question in the comments: you may just work with arrays to come close to the intended usage. If you use

\newcommand\Tarray{"( 0.5, 0.0)","( 2.0, 0.0)","(-1.0,-1.0)"}


then a single coordinate can be added with

  \path
node[draw=red,circle,inner sep=2pt,minimum size=4mm]
[/utils/exec=\pgfmathparse{{\Tarray}[\myindex]},at=\pgfmathresult ]
{\pgfmathparse{{\Tname}[\myindex]}\pgfmathresult};


Full example:

\documentclass{article}
\usepackage{tikz}
\newcommand\Tarray{"( 0.5, 0.0)","( 2.0, 0.0)","(-1.0,-1.0)"}
\newcommand\Tname{"A", "B", "C"}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,1,2} {
\node [draw=black,circle,inner sep=2pt,minimum size=4mm] (pt\i)
[/utils/exec=\pgfmathparse{{\Tarray}[\i]},at=\pgfmathresult]
{\pgfmathparse{{\Tname}[\i]}\pgfmathresult};
}
\draw (pt0) -- (pt1) -- (pt2);
\pgfmathtruncatemacro{\myindex}{1}
\path
node[draw=red,circle,inner sep=2pt,minimum size=4mm]
[/utils/exec=\pgfmathparse{{\Tarray}[\myindex]},at=\pgfmathresult ]
{\pgfmathparse{{\Tname}[\myindex]}\pgfmathresult};
\end{tikzpicture}
\end{document}


• Thanks. An additional question I have is how to draw one point at a time by accessing its coordinate via the Tpos array. Unfortunately, all my attempts to far have been unsuccessful: \coordinate [at= {\pgfmathparse{{\Tpos}[8]}\pgfmathresult}, name=pt8]; results is the error: ! Package tikz Error: Cannot parse this coordinate. And the – Alain Girault Mar 16 at 16:05
• @AlainGirault I added something that comes close to the desired usage. (BTW, please note that answers are reserved to answers of the original questions only. And in general it is better to ask follow-up questions separately because they get more attention that way, and are also free of charge.) – user194703 Mar 16 at 16:35
• Thanks. I see that the question was complex and that crafting a working solution was not easy. All the more thanks for this! Take care. – Alain Girault Mar 18 at 15:54

Just for reference!

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
% array of numbers a[i], i=0,1,2,3
\def\a{{6,8,11,12}}
% array of strings b[i], i=0,1,2,3
\def\b{{"blue","red","orange","teal"}}
% array of points (pairs of coordinates) c[i], i=0,1,2,3
\def\c{{"(1,2)","(1.5,1.5)","(3,2.5)","(5,3)"}}
\begin{tikzpicture}
\draw (0,.5)rectangle (6.5,3.5);
\foreach \i in {0,...,3}{
\pgfmathsetmacro{\ii}{int(\i+1)}
\pgfmathsetmacro{\tmpa}{\a[\i]}
\pgfmathsetmacro{\tmpb}{\b[\i]}
\pgfmathsetmacro{\tmpc}{\c[\i]}
\path (0,-\i) node[right,\tmpb,align=left]
{The \ii-th element is $\tmpa{}$ with \tmpb{} color\\
associated with the point $\tmpc$};
\fill[\tmpb] \tmpc node[above]{\tmpa} circle(2pt);
}
\end{tikzpicture}
\end{document}


Thanks. An additional question I have is how to draw one point at a time by accessing its coordinate via the Tpos array. Unfortunately, all my attempts to far have been unsuccessful:

\coordinate [at= {\pgfmathparse{{\Tpos}[8]}\pgfmathresult}, name=pt8];


results is the error:

! Package tikz Error: Cannot parse this coordinate.


Same error if I remove the enclosing {}. And the more obvious code:

\node [draw=red,rond] (pt8) at {\pgfmathparse{{\Tpos}[8]}\pgfmathresult} {H};


produces exactly the same error. I have read in another post that the \coordinate () at () command is not able to parse macros, hence my attempt to use of the \coordinate [at=] instead. It actually works with a macro, but not with the array access macro.

Ideally, I would like to write something like:

\node [draw=red,rond] (pt8) at {\pgfmathparse{{\Tpos}[8]}\pgfmathresult} {\Tname[8]};


in order to have the pt8 point in red. What am I doing wrong here? Alain