29

I have the following code:

\draw[step=1.0,black,thin] (0.5,0.5) grid (5.5,4.5);

which I would like to have generate grid lines in the Y direction at y = {0.5, 1.5, 2.5, 3.5, 4.5} and grid lines in the X direction at x = {0.5, 1.5, 2.5, 3.5, 4.5, 5.5}.

However, I get grid lines at y = {1.0, 2.0, 3.0, 4.0} and x = {1.0, 2.0, 3.0, 4.0, 5.0} with half cells around the edges of the grid. So instead of having a closed uniform grid, I get one with open edges and two half cells at the borders.

Does anybody know why it doesn't honor the starting point and stride?

22

The points specify where the grid lines begin and end. The brown grid lines below are the ones you want, but note that that the blue lines which were specified with (0.5,0.5) grid (5.5,4.5) do indeed begin at x=0.5, and y=0.5 and end at x=5.5 and y=4.5.        

enter image description here

\documentclass{article}
\usepackage{tikz}

\newcommand*{\xMin}{0}%
\newcommand*{\xMax}{6}%
\newcommand*{\yMin}{0}%
\newcommand*{\yMax}{6}%
\begin{document}
\begin{tikzpicture}
    \foreach \i in {\xMin,...,\xMax} {
        \draw [very thin,gray] (\i,\yMin) -- (\i,\yMax)  node [below] at (\i,\yMin) {$\i$};
    }
    \foreach \i in {\yMin,...,\yMax} {
        \draw [very thin,gray] (\xMin,\i) -- (\xMax,\i) node [left] at (\xMin,\i) {$\i$};
    }

\draw [step=1.0,blue, very thick] (0.5,0.5) grid (5.5,4.5);
\draw [very thick, brown, step=1.0cm,xshift=-0.5cm, yshift=-0.5cm] (0.5,0.5) grid +(5.5,4.5);
\end{tikzpicture}
\end{document}
25

You can use the xshift and yshift options:

\draw[step=1.0,black,thin,xshift=0.5cm,yshift=0.5cm] (0.5,0.5) grid (5.5,4.5);
  • Is it possible to "easily" add the labels? – anderstood Aug 30 '15 at 23:09
  • Probably not, but perhaps you can put it in a separate command, so that you can use e.g. \gridlabel{xmin}{ymin}{xmax}{ymax}? – Tom Bombadil Aug 31 '15 at 7:06

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.