4

I have the following code, but I don't understand why the TikZ code does not accept addition and subtraction operations but accepts multiplication and division operations.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,3,4,5} {
 \node[draw,circle,minimum size=0.2cm,fill=blue!20] () at ({\the\numexpr 
    72*(\i-1) \relax}:2cm) {\i};
  \node[draw,circle,minimum size=1cm,fill=red!20] () at (
   \i*360/5:1cm ) {\i};
 }
\end{tikzpicture}
\end{document}

It has to go through the following processing {\the\numexpr 72*(\i-1) \relax}, but \i*360/5 is Ok. At least addition and subtraction are more basic operations than multiplication and division, why is there such an inconsistent design?

Edits: Many thanks to Cabohah for correcting my mistaken idea. But I have another example, why does the second code show an error.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,3,4,5} {
  \node[draw,circle] (\i) at (72*\i-36:1cm) {$\i$};
}
\foreach \i in {1,2,3,4} {
  \draw (\i) -- ({\i+1});
}
\end{tikzpicture}
\end{document}

Package pgf Error: No shape named `1+1' is known. }...

It seems that TikZ does not accept any operation at this time.

1 Answer 1

8

The problem is not the subtraction but the (…), because this is shape syntax. So using (\i)*360/5 would also be wrong. But you don't need \numexpr:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,3,4,5} {
  \node[draw,circle,minimum size=0.2cm,fill=blue!20] () at (
  {72*(\i-1)}:2cm) {\i};
  \node[draw,circle,minimum size=1cm,fill=red!20] () at (
   \i*360/5:1cm ) {\i};
 }
\end{tikzpicture}
\end{document}

Using the extra grouping is enough.

enter image description here

4
  • Thank you for correcting my mistaken idea.
    – licheng
    Commented Feb 4, 2023 at 14:05
  • Sorry. See my second code. The problem seems to be different here. A series of error reports: Package pgf Error: No shape named `1+1' is known. }...
    – licheng
    Commented Feb 4, 2023 at 14:17
  • 1
    @licheng The second example is different from the first one, here you want indeed the named nodes, but the argument of (…) is not calculated it is only an expanded string, so (\i+1) just would result in (1+1), (2+1) etc. If you want the result of a calculation, you should do the calculation before, e.g., \tikzmath{\j=\i+1;}, and use the new variable. However, because this is a new question you should not mix it into the already answered question, but really ask a new one.
    – cabohah
    Commented Feb 4, 2023 at 16:55
  • 1
    (1) is a coordinate (or a node) called 1; that's very different from (1,1) (which is a coordinate with x=1 and y=1). So ({\i+1}) is looking for a node/coordinate named the expansion of \i concatenated with +1.
    – Rmano
    Commented Feb 4, 2023 at 22:34

You must log in to answer this question.

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