5

I am trying to draw a batch of circles using the following code. However, I failed to create the figure i want. and i got a error saying undefined control sequence. argument 0,...,\NY. But if i change \NY to 4. it works well. Why cannot i use \NY in the {0,...,\NY}?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usetikzlibrary{math}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}
  \tikzmath{
    \VX = 30;
    \VY = 30;
    int \NX = 6;
    int \NY = 4;
    int \nix; int \niy;
    \OX = 0; \OY = 0;
    for \niy in {0,...,\NY}{%
      for \nix in {0,...,6}{%
        {\node [circle, draw = red, minimum size = \VX pt] at (\nix*\VX pt,\niy*\VY pt) {};};
      };
    };
  };
\end{tikzpicture}
\end{document}
7

Things like

int \NY = 4;

are not allowed. Use

int \NY; \NY = 4;

instead and your MWE will work:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usetikzlibrary{math}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}
  \tikzmath{
    \VX = 30;
    \VY = 30;
    int \NX; \NX = 6;
    int \NY; \NY = 4;
    int \nix; int \niy;
    \OX = 0; \OY = 0;
    for \niy in {0,...,\NY}{%
      for \nix in {0,...,\NX}{%
        {\node [circle, draw = red, minimum size = \VX pt] at (\nix*\VX pt,\niy*\VY pt) {};};
      };
    };
  };
\end{tikzpicture}
\end{document} 

enter image description here

2

Alternatively you could use \newcommand\NX{6} \newcommand\NY{4} in the preamble, then you could use them also in the loop.

enter image description here

MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usetikzlibrary{math}
\usetikzlibrary{positioning,calc}
\newcommand\NX{6}
\newcommand\NY{4}
\begin{document}
\begin{tikzpicture}
  \tikzmath{
    \VX = 30;
    \VY = 30;
    int \nix; int \niy;
    \OX = 0; \OY = 0;
    for \niy in {0,...,\NY}{%
      for \nix in {0,...,\NX}{%
        {\node [circle, draw = red, minimum size = \VX pt] at (\nix*\VX pt,\niy*\VY pt) {};};
      };
    };
  };
\end{tikzpicture}
\end{document}

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.