# How to align overlaid grid in TikZ?

In trying to adjust this answer in light of the OP's comments on another one, I discovered that I could not straightforwardly align an overlaid grid so that the steps began from the same point as the grid.

Why doesn't this work and how is this supposed to be done?

The code (minus my added bugs) is adapted from one of Michael Goerz's examples.

% Minor adaptions from http://michaelgoerz.net/notes/printable-paper-with-latex-and-tikz.html
\documentclass[a4paper, 10pt]{article} % for A4 size paper
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\colorlet{dlines}{green!25!black}
\colorlet{llines}{green!25!gray}
\tikzset{
dashed lines/.style={llines, very thin, densely dashed},
strong lines/.style={dlines, very thin},
}

\begin{tikzpicture}[remember picture, overlay]
\draw[style=dashed lines] (current page.south west) ++(5mm,8.5mm) coordinate (a) grid [step=10mm] ++(200mm,280mm) coordinate (b);
\draw[style=strong lines] (a) rectangle (b);
\draw[style=strong lines] (a) grid [step=20mm] (b);
\end{tikzpicture}

\end{document}


• You get a similar issue if you attempt to use this construction in beamer to make a rectangle that covers the page. It does not go exactly to the edge. No idea why. – daleif Oct 4 '15 at 14:30
• @daleif I think that's a different issue. I'm not trying to start right at the edge in this case - starting right at the edge would work. It is starting a bit in and a bit up which seems problematic. – cfr Oct 4 '15 at 15:14
• @JohnKormylo I am running it twice. That's not the problem. – cfr Oct 4 '15 at 16:18

In place to use \draw (a) grid (b); you can use \draw[shift={(a)}] (0,0) grid (b); to have a grid starting from the bottom left corner.

\documentclass[a4paper, 10pt]{article} % for A4 size paper
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\colorlet{dlines}{green!25!black}
\colorlet{llines}{green!25!gray}
\tikzset{
dashed lines/.style={llines, very thin, densely dashed},
strong lines/.style={dlines, very thin},
}

\begin{tikzpicture}[remember picture, overlay]
\draw[style=dashed lines, shift={(current page.south west)},shift={(5mm,8.5mm)}] (0,0) coordinate (a) grid [step=10mm] (200mm,280mm) coordinate (b);
\draw[style=strong lines] (a) rectangle (b);
\draw[style=strong lines, shift={(a)}] (0,0) grid [step=20mm] (b);
\end{tikzpicture}

\end{document}


• This is a neat workaround - thanks! I didn't think to try a double-shift like that. But do you know why it is needed? Why is my code wrong exactly? – cfr Oct 4 '15 at 16:17
• @cfr because this is how grid works : it starts always from (0,0). Try to draw \draw (.5,.5) grid +(5,5);. The bad news is that I don't know some parameter start grid from=.... The good news is that we can make start grid from another point if we add a shift. – Kpym Oct 4 '15 at 16:21
• That's not what the manual says, though. It says It is important to note that the grid is always “phased” such that it contains the point (0, 0) if that point happens to be inside the rectangle. But here that's not the case, is it? – cfr Oct 4 '15 at 18:22
• Or is (0,0) at (current page.center)? – cfr Oct 4 '15 at 18:24
• @cfr no, the zero point is not at (current page.center), it is at the position where your image is on the page. And I think that the documentation is ok if you remove "if that point happens to be inside the rectangle", because in any case, if no transformations are present, the grid is aligned at (0,0). – Kpym Oct 4 '15 at 18:46