52

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?

0

2 Answers 2

39

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}
2
  • A newbie reflection: from this example and several others I got the idea of grid line coordinates: it uses multiples of step (which is 1cm by default), and the parameters in (startx, starty) grid (endx, endy) basically define the clipping region whereas in order to shift it we need to use the xshift yshift that you mentioned. It seems so counterintuitive to me that it is so much 'centered' around the origin (I do find it convenient though, it's just surprising). I was trying to find the grid shift parameters in the TikZ documentation for the grid command and found nothing. Commented Jun 19, 2022 at 19:47
  • 1
    @AndreySurovtsev: Welcome to TeX.SE. I think the authors tried to not have more options than necessary. Since xshift and yshift are used throughout to shift the coordinate system, it is convienient to use the same options for shifting grids. I agree that when starting out there is a lot to take in with TikZ, but once you use it for a while things get easier, at least the basics. Commented Jun 19, 2022 at 21:26
37

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);
2
  • Is it possible to "easily" add the labels?
    – anderstood
    Commented Aug 30, 2015 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}? Commented Aug 31, 2015 at 7:06

You must log in to answer this question.

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