0

Why is the graph drawn by the first code good, while the second code looks not good? In in the first one I use \the\numexpr \relax, and in the second code I use \tikzmath.

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

enter image description here

\documentclass{article}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}

\foreach \i in {1,2,3,4,5} {
  \node[draw,circle,minimum size=0.5cm,fill=blue!20] (\i) at (72*\i-36:1cm) 
  {$\i$};};
  \foreach \i in {1,2,3,4,5} {
   \tikzmath{\j=\i+1;}
     \ifthenelse{\i<5}{ \draw (\i) -- ( \j );}{ \draw 
     (\i)  -- (1);}
 };
\end{tikzpicture}
\end{document}

enter image description here

My expectation is that the two outputs should be consistent.

3
  • Try printing out value of j. It is 2.0 i.e. node 2, anchor 0...
    – user202729
    Feb 5 at 4:49
  • @user202729 Thanks. By the way, what command did you use to make the screen print the value \j? There is nothing by print{\j};.
    – licheng
    Feb 5 at 5:58
  • 2
    Options are \show\j, \typeout{\j}, or \showtokens\expandafter{\j}, that prints to the command-line. They all do slightly different things, documentation are in TeXbook/LaTeX reference manual/e-TeX manual.
    – user202729
    Feb 5 at 6:00

0

Browse other questions tagged .