# Tikz for drawing grid-like graph: strange behaviour

I would like to draw a grid-like graph (see the figure below).

The following code gives the result shown in the figure. I don't understand why the edges are not vertical.

In addition, the commented code is for drawing the horizontal edges. However, it causes errors.

\tikzstyle{vertex}=[circle,draw,minimum size=1cm]
\tikzstyle{every node}=[vertex]
\begin{tikzpicture}
\foreach \x in {1,3,5,7}
{
\foreach \y in {1,3,5,7}
{
\node (\x \y) at (\x,\y) {\x, \y};
\ifnum\y>1
\pgfmathparse{\y-2}
\draw  (\x \y)  -- (\x \pgfmathresult);
\fi
%                 \ifnum\x>1
%                        \pgfmathparse{\x-2}
%                        \draw  (\x \y)  -- (\pgfmathresult \y);
%                 \fi
}
}
\end{tikzpicture}


• use int(...) for integer result, and use comma in (\x \y)  – percusse May 23 '14 at 13:41
• I'm looking at this, but note that \pgfmathparse returns the output with a .0 appended – Chris H May 23 '14 at 13:42

# Problem

Consider \pgfmathparse{\y-2} where \x = 1 and \y = 3. Then your output will be 1.0. Hence the edge will be drawn from (1 3) to (1 1.0) where .0 denotes the angle at which the edge will enter the node. You will need to get rid of all the decimal places by using \pgfmathparse{int(\y-2)}

# Solution

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
vertex/.style={draw,circle,minimum size=1cm},
every node/.style={vertex}
]
\foreach \x in {1,3,5,7}
{
\foreach \y in {1,3,5,7}
{
\node (\x \y) at (\x,\y) {\x, \y};
\ifnum\y>1
\pgfmathparse{int(\y-2)}
\draw  (\x \y) -- (\x \pgfmathresult);
\fi
\ifnum\x>1
\pgfmathparse{int(\x-2)}
\draw  (\x \y)  -- (\pgfmathresult \y);
\fi
}
}
\end{tikzpicture}
\end{document}


Here it is in full - I see I commented just after @percusse but thinking along similar lines. You can leave the node names untouched, I think of them as being like variable names and don't like spaces in them, so didn't put the spaces back in after testing. The only change you really need is \pgfmathparse{int(\y-2)}:

\tikzstyle{vertex}=[circle,draw,minimum size=1cm]
\tikzstyle{every node}=[vertex]
\begin{tikzpicture}
\foreach \x in {1,3,5,7}
{
\foreach \y in {1,3,5,7}
{
\node (\x\y) at (\x,\y) {\x, \y};
\ifnum\y>1
\pgfmathparse{int(\y-2)}
\draw  (\x\y)  --  (\x\pgfmathresult);
\fi
%                 \ifnum\x>1
%                        \pgfmathparse{\x-2}
%                        \draw  (\x \y)  -- (\pgfmathresult \y);
%                 \fi
}
}
\end{tikzpicture}