Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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?

share|improve this question
    
The solution seems to be quite complicated. I could set the grid as part of the background canvas but in this case the coordinate origin sits somewhere in the headline of the frame. Beamer also provides some own grid feature, but this is without annotated axes. There are some ways to run macros at the beginning of a frame (1,2) but what one would want is to execute your code at the beginning of the content, which seems to be not that trivial. –  Benedikt Bauer Oct 19 '12 at 19:07
add comment

1 Answer

Well the remaining step is to put it in the background canvas.

\documentclass{beamer}
\usepackage{tikz}
\newcommand{\turntikzgridon}{
\setbeamertemplate{background canvas}{
    \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=west,gray] at (0,\x) {\tiny \x};
        }
    \end{tikzpicture}
}
}
\newcommand{\turntikzgridoff}{\setbeamertemplate{background canvas}{}}
\begin{document}
\turntikzgridon %<--- Turn it on
\begin{frame}{Frame title}
    Some frame 1
\end{frame}

\begin{frame}{Frame title 2}
    Some frame 2
\end{frame}
\turntikzgridoff %<----- Turn it off
\begin{frame}{Frame title 3}
    Some frame 3
\end{frame}

\end{document}

enter image description here

If you want to apply different backgrounds,images etc. then you need to use \addtobeamertemplate which will also overwrite the grid.

share|improve this answer
    
This is nice, but as I mentioned in the question, I'd like the grid to be the last component to be drawn, so that it's on top of everything else. I'm not near a tex installation to test this out right now, but I'm guessing from the figure and because you applied it to the background canvas, that this is one of the first layers to be drawn... –  Lorem Ipsum Oct 21 '12 at 2:10
    
@yoda Oops. Missed that. I'll check –  percusse Oct 21 '12 at 3:01
    
No worries, I still upvoted it :) btw, what controls the origin? As you can see in my example, the origin is in the center left and in some other slides of mine, it is lower left (but not the corner), whereas in yours it is top left, even though I can't see any obvious declaration. It looks like it varies depending on what items are already on the slide (e.g. frame title/no title, etc.) Would it be possible to force the origin to be at the same spot in every slide? –  Lorem Ipsum Oct 21 '12 at 3:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.