In tikz, I define a variable outside \foreach loop, and use \pgfmathtruncatemacro command to increase it inside the loop.
Below is my MWE.
\documentclass{article}
\usepackage{pgfplots,tikz,tikz-3dplot}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\pgfmathtruncatemacro\j{1};
\foreach \i in {1,4,7}
{
\node at (\i,1.4) {\j};
\pgfmathtruncatemacro\j{\j+1};
\node at (\i+1.5,1.4) {\j};
\pgfmathtruncatemacro\j{\j+1};
\node at (\i,0) {\j};
\pgfmathtruncatemacro\j{\j+1};
\node at (\i+1.5,0) {\j};
}
\end{tikzpicture}
\end{figure}
\end{document}
Instead of increasing the value of \j up to 12, it resets back to 1 after only one iteration of the loop. What am I doing wrong?
\xdef\j{\j}in last line inside loop. – Jul 08 '20 at 11:47