2

I am trying to draw an 8*8 rectangular grid using Tikz and have the following code. But I get an error "Package pgf Error: No shape named v18 is known". The bit of the code that is commented out works fine (horizontal lines). I see no reason why the rest should not work. Am I missing something basic?

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}[
    vertex/.style={draw,circle,thick,inner sep=0.25em}
  ]
    %Draw lattice
    \foreach \x in {0,...,7}{
      \foreach \y in {0,...,7}{
    \node[vertex] (v\x\y) at (\x,\y) {};
      }
    }  

    \foreach \x in {0,...,5}{
      \foreach \y in {0,...,5}{
%   \def\l{\the\numexpr\y+1}
    \def\m{\the\numexpr\x+1}
%   \draw (v\x\y) -- (v\x\l); %Horizontal lines
    \draw (v\x\y) -- (v\m\y); %Vertical lines
      }
    }


  \end{tikzpicture}
\end{document}

2 Answers 2

1

The purpose of \the\numexpr is to avoid defining new macros. Just use it instead of these macros.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}[
    vertex/.style={draw,circle,thick,inner sep=0.25em}
  ]
    %Draw lattice
    \foreach \x in {0,...,7}{
      \foreach \y in {0,...,7}{
    \node[vertex] (v\x\y) at (\x,\y) {};
      }
    }  

    \foreach \x in {0,...,5}{
      \foreach \y in {0,...,5}{
    \draw (v\x\y) -- (v\x\the\numexpr\y+1); %Horizontal lines
    \draw (v\x\y) -- (v\the\numexpr\x+1\relax\y); %Vertical lines
      }
    }


  \end{tikzpicture}
\end{document}

enter image description here

If you really want to define new macros, use

\pgfmathtruncatemacro\l{\y+1}
\pgfmathtruncatemacro\m{\x+1}

or the evaluate key

\foreach \x [evaluate=\x as \m using {int(\x+1)}] in {0,...,5}{
  \foreach \y [evaluate=\y as \l using {int(\y+1)}] in {0,...,5}{
\draw (v\x\y) -- (v\x\l); %Horizontal lines
\draw (v\x\y) -- (v\m\y); %Vertical lines
  }
}

However, I would like to argue that not defining a new macro is the by far best option since you may overwrite previous definitions.

I also personally prefer to avoid using \x and \y since they have their own meaning in the calc syntax. Rather, I'd suggest to use \X and \Y instead

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}[
    vertex/.style={draw,circle,thick,inner sep=0.25em}
  ]
    %Draw lattice
    \foreach \X in {0,...,7}{
      \foreach \Y in {0,...,7}{
    \node[vertex] (v\X\Y) at (\X,\Y) {};
      }
    }  

    \foreach \X in {0,...,5}{
      \foreach \Y in {0,...,5}{
    \draw (v\X\Y) -- (v\X\the\numexpr\Y+1); %Horizontal lines
    \draw (v\X\Y) -- (v\the\numexpr\X+1\relax\Y); %Vertical lines
      }
    }


  \end{tikzpicture}
\end{document}
1
  • I was initially trying to use \numexpr, but I was unaware of the \relax command. Thanks!
    – ckamath
    Commented Dec 16, 2019 at 19:01
1

You want to \edef, rather than \def:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}[
    vertex/.style={draw,circle,thick,inner sep=0.25em}
  ]
    %Draw lattice
    \foreach \x in {0,...,7}{
      \foreach \y in {0,...,7}{
    \node[vertex] (v\x\y) at (\x,\y) {};
      }
    }  

    \foreach \x in {0,...,5}{
      \foreach \y in {0,...,5}{
    \edef\l{\the\numexpr\y+1}
    \edef\m{\the\numexpr\x+1}
    \draw (v\x\y) -- (v\x\l); %Horizontal lines
    \draw (v\x\y) -- (v\m\y); %Vertical lines
      }
    }


  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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