5

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

  • Why don't you use simply {\n} as label? – Red Sep 24 '13 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 '13 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. – Claudio Fiandrino Sep 24 '13 at 9:36
  • 5
    The node content is not a TikZ/pgf formula. You may use \pgfmathparse{...} then \pgfmathresult... – Paul Gaborit Sep 24 '13 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 – Utkan Gezer Jul 21 at 10:53
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

| improve this answer | |

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.