6

There is something I cannot understand. I'm using array indexing and it works for determining the radius of the circles and to positioning the nodes. However it does not work as the node content.

\documentclass{standalone}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\def\radi{{1,2,3,4}}
\foreach \x/\a [count=\n] in {0/15,1/345,2/345,3/345}
    \draw[very thin, draw=red!50!white] (0,0) circle [radius={\radi[\x]}] node at (\a:{\radi[\x]}) {{\radi[\x]}};


\end{tikzpicture}
\end{document}

enter image description here

5
  • Why don't you use simply {\n} as label?
    – Red
    Sep 24, 2013 at 9:17
  • ups, I did not realized that would be a solution in the Minimal Working Example (MWE) I prepared. In my real situation I do need to access the vector.
    – Sik
    Sep 24, 2013 at 9:28
  • If you need to count the items in the list, you may want to use the count option of the foreach loop. Sep 24, 2013 at 9:36
  • 5
    The node content is not a TikZ/pgf formula. You may use \pgfmathparse{...} then \pgfmathresult... Sep 24, 2013 at 9:37
  • The solutions offered here work only for numeric arrays. To anybody from the future, if you are looking for a way to index an array for arbitrary content, see: tex.stackexchange.com/q/551501/69346 Jul 21, 2020 at 10:53

1 Answer 1

5

Please don't use color with TikZ!! It already loads xcolor.

Paul Gaborit already gave you the answer that a brace pair doesn't necessarily mean a math operation. Here it is just put inside a text box and TikZ doesn't evaluate it. You can either go parsing the math inside a \pgfextra{...} and use \pgfmathresult or you can explicitly define more variables that use the loop variable via evaluate key.

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\def\radi{{1,2,3,4}}
\foreach \x/\a [
    count=\n,
    evaluate=\x as \myvar using ({\radi[\x]})
    ] in {0/15,1/345,2/345,3/345}
    \draw[very thin, draw=red!50!white] 
        (0,0) circle [radius={\myvar}] 
        node at (\a:{\radi[\x]}) {\myvar};


\end{tikzpicture}
\end{document}

enter image description here

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .