From Jake's answer to Drawing on an image with TikZ , I learned how to display gridlines on my beamer slides during development. It has greatly helped me in precise positioning of tikz nodes and objects. The code that I slightly modified and am using now, is:
\begin{tikzpicture}[remember picture, overlay]
\draw[help lines,xstep=.25,ystep=.25,gray!20] (current page.south west) grid (current page.north east);
\draw[help lines,xstep=1,ystep=1,gray] (current page.south west) grid (current page.north east);
\foreach \x in {-15,-14.5,...,15} {
\node [anchor=north, gray] at (\x,0) {\tiny \x};
\node [anchor=east,gray] at (0,\x) {\tiny \x};
}
\end{tikzpicture}
which, on a slide, looks like this:

Now, I find myself repeatedly adding these lines manually for each slide and commenting them out when I'm done. It's getting cumbersome, and also causing my file to bloat. I would like to be able to toggle gridlines on and off with a single command. How can I:
- Convert the above snippet into a command that, if set in the preamble, turns gridlines on for all the slides (or frames) and
- If the command is called from inside a
\begin{figure}...\end{figure}, turns it on only for that slide.
Needless to say, these shouldn't mess with any existing tikz figures on that slide.
For now, I've simplified a lot of the bloat by defining a new command:
\newcommand{\gridlines}{
\begin{tikzpicture}[remember picture, overlay]
\draw[help lines,xstep=.25,ystep=.25,gray!20] (current page.south west) grid (current page.north east);
\draw[help lines,xstep=1,ystep=1,gray] (current page.south west) grid (current page.north east);
\foreach \x in {-15,-14.5,...,15} {
\node [anchor=north, gray] at (\x,0) {\tiny \x};
\node [anchor=east,gray] at (0,\x) {\tiny \x};
}
\end{tikzpicture}
}
and just calling \gridlines before the \end{frame} in each slide. A big improvement over copying 4-5 lines of code each time, but still not as convenient as a global on/off option.
Ideally, I would like it to be applied last, so that it overlays on everything else on the slide. Would it be possible to hack/redefine \end{frame} so that it becomes \gridlines\end{frame} instead?
