I'm trying to use tikzmath to draw lines between a set of points, however I've an error ERROR: Missing \endcsname inserted. on the line \ni=\i+1;. Any idea why?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math,calc}

\begin{document}

\begin{tikzpicture}
  \tikzmath{
    \n=5;
  }
  \foreach \i in {-\n,...,\n} {
    \foreach \j in {-\n,...,\n} {
      \node[circle,fill,inner sep=1pt] at ($(\i+.3*\j,.5*\j)$) (n-\i-\j) {};
    }
  }
  \foreach \i in {-\n,...,\n-1} {
    \foreach \j in {-\n,...,\n-1} {
      \tikzmath{%
        \ni=\i+1;
        \nj=\j+1;
      };
      \draw[] (n-\i-\j) -- (n-\i-\nj);
    }
  }
\end{tikzpicture}
\end{document}
  • I suspect the problem is that \ni is defined to be a number in TeX’s eyes (it’s defined with \mathchardef). If I change \ni to \nx, I don't get the error, but other ones due to wrong code. – egreg Apr 19 at 9:39
  • Addition to @egreg's comment the wrong code is the last \n-1 parts in the foreach list. TikZ doesn't evaluate the expressions there. You might add \numexpr before it. – percusse Apr 19 at 9:42
up vote 7 down vote accepted

Due to how \tikzmath works, you can’t use variable names that correspond to already defined commands that point to mathematical symbols.

The standard definition of \ni is by \mathchardef\ni="3233 (in a glorified form in fontmath.ltx) and TeX considers \mathchardef tokens as numbers when it’s looking for them.

The same error happens, for instance, if I use \propto instead of \ni.

Use another name.

With other fixes, as suggested by percusse:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math,calc}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}
  \tikzmath{
    \n=5;
  }
  \foreach \i in {-\n,...,\n} {
    \foreach \j in {-\n,...,\n} {
      \node[circle,fill,inner sep=1pt] at ($(\i+.3*\j,.5*\j)$) (\n-\i-\j) {};
    }
  }
  \foreach \i in {-\n,...,\numexpr\n-1} {
    \foreach \j in {-\n,...,\numexpr\n-1} {
      \tikzmath{%
        \nni=\i+1;
        \nj=\j+1;
      };
      \draw[] (\n-\i-\j) -- (\n-\i-\nj);
    }
  }
\end{tikzpicture}
\end{document}
  • You could also do \let\ni\undefined at the start of the tikzpicture. – egreg Apr 19 at 10:12
\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math,calc}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}
\def\n{5}
  \foreach \i in {-\n,...,\n} {
    \foreach \j in {-\n,...,\n} {
      \node[circle,inner sep=1pt] at ($(\i+.3*\j,.5*\j)$) (n-\i-\j) {};
    }
  }
  \foreach \i [evaluate=\i as \ni using int(\i+1)] in {-\n,...,\numexpr\n-1} {
    \foreach \j [evaluate=\j as \nj using int(\j+1)] in {-\n,...,\numexpr\n-1} {
      \draw (n-\i-\j) -- (n-\ni-\nj);
    }
  }
\end{tikzpicture}
\end{document}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.