4

I am looking for a way to simplify the painstaking process of positioning different kind of elements in beamer.

I read this thread and obtained a nice grid using the command \setbeamertemplate{background} and also read this other thread and obtained an even nicer grid using the package eso-pic with the following options \usepackage[texcoord,grid,gridunit=mm,gridcolor=red!20,subgridcolor=green!40] {eso-pic}.

However, even if I have some reference now of what is where, by means of the grid coordinates, if I try to draw a simple line between two points

\begin{tikzpicture}
    \draw[solid,red,line width= 1.5pt,-stealth] (7,-5) -- (9,-4);
\end{tikzpicture}

the line is drawn between two coordinates that do not match the grid coordinates. Any idea why?

Here's an MWE. I left both the grids, just to double check that they matched.

\documentclass{beamer}
\usepackage{tikz}                       %inline graphics
\usetikzlibrary{hobby,backgrounds,trees,snakes,shapes.callouts,positioning,pgfplots.groupplots}
\usetikzlibrary{positioning}
\usepackage[texcoord,grid,gridunit=mm,gridcolor=red!20,subgridcolor=green!40]
{eso-pic}

\setbeamertemplate{background}{\tikz[overlay]{
    \foreach \x in {0,...,15} \node at (\x ,-0.25) {\small$\x$};
    \foreach \y in {-15,...,0} \node at (12.5 ,\y) {\small$\y$};
    \foreach \x in {0,0.5,...,15} \draw (\x ,-15) -- (\x ,0);
    \foreach \y in {-15,-14.5,...,0} \draw (0,\y) -- (15,\y);}
    }


\begin{document}
\begin{frame}
%
\begin{tikzpicture}
    \draw[on grid,solid,red,line width= 1.5pt,-stealth] (0,0) -- (2,2);
\end{tikzpicture}
%        
\end{frame}
\end{document} 

Cheers

gbernardi
  • 845
  • Your code won't compile for me: I get a compilation error. (This is on TeX Live on GNU/Linux.) – cfr Sep 27 '15 at 19:06
  • It's probably due to some of the tikz libraries. Try removing the first \usetikzlibrary line. – gbernardi Sep 28 '15 at 07:32

1 Answers1

6

The tikzpicture coordinates are relative to the tikzpicture. You are essentially using different coordinate systems.

The non-overlaid tikzpicture is a box sized (roughly) to fit the contents. It is (by default) placed so that the bottom of the box is on the current base line - just as an F or a g is aligned to the current base line of the text. (The g has depth, as well as height, of course, so that the descender comes beneath the base line.) Hence, the tikzpicture is 2x2 and its bottom will be on the current baseline.

The tikzpicture of the grid is a different size, so the box is a different size. That is in overlay mode so it is also aligned differently.

That is, the basic issues here are that the boxes of the two pictures are

  • of different sizes;
  • aligned differently relative to the slide.

In addition, there is no (2,2) in your grid as the maximal value of y is 0. So if the arrow was drawn relative to that grid, it would not appear within it.

To use the same coordinates, you have to use the same system of coordinates. For example:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}

\setbeamertemplate{background}{\tikz[overlay, remember picture, help lines]{
    \foreach \x in {0,...,12} \path (current page.south west) +(\x,9.25) node {\small$\x$};
    \foreach \y in {0,...,9} \path (current page.south west) +(12.5,\y) node {\small$\y$};
    \foreach \x in {0,0.5,...,12.5} \draw (current page.south west) ++(\x,0) -- +(0,9.6);
    \foreach \y in {0,0.5,...,9.5} \draw (current page.south west) ++(0,\y) -- +(12.8,0);
  }
}

\begin{document}
\begin{frame}
\begin{tikzpicture}[remember picture, overlay]
    \draw[on grid,solid,red,line width= 1.5pt,-stealth] (current page.south west) -- +(2,2);
\end{tikzpicture}
\end{frame}
\end{document}

aligned to common grid

Note that I removed a bunch of detritus from your example because the original wouldn't compile and it was irrelevant to the issue.

cfr
  • 198,882
  • Thank you very much @cfr. This is pretty cool!

    I have a couple more related questions:

    1. Is there a way to avoid the use of current page.south west within the points coordinates and, somehow, pass it as an option of the tikzpicture environment? Or, perhaps, use a new command, like \cpsw to identify it?

    2. Is there a way to have the grid on top of the content of the slide? I guess this would need a different command since here we're using \setbeamertemplate{background}, but I'm curious.

    – gbernardi Sep 28 '15 at 07:38
  • @gbernardi I'll try to think about (1) later (no time now). For (2), you can draw the grid within the slide. Then the overlay will cause it to be drawn over the other content. Just put the drawing commands after the arrow and any other content on the slide. I'm not sure why you'd want to do this, but it it should work. (Let me know if not.) – cfr Sep 28 '15 at 10:19
  • Thanks for the hint ;) Your solution for (2) does work (if the grid is drawn at the end of the slide), but I was looking for an easier-to-use solution, especially because the grid is only for a design purpose, so, once my slides are done, I'd like to remove it.

    If the command is repeated on every slide, it'll be a pain to remove it. I know I could use an if structure and make a boolean variable to show it or not, but I was thinking there's a way to overwrite the way a frame structure is created.

    – gbernardi Sep 28 '15 at 12:08