0

How exactly does positioning in beamer+tikz work?

  1. where is the absolute origin (0,0) on a slide? Why when I specify (0,0) does it seem to move around the slide depending on the size of whats being plotted? Does it exist absolutely somewhere?
  2. Can I draw the absolute grid (as opposed to some grid) with absolute positioning information on slides to help me when trying to position items?
  3. Can someone help me make sense of the example below?

Im reading and rereading the tikz (especially chapter 13) and beamer documentation, but cant seem to figure out what is going on.

\documentclass[xcolor=dvipsnames]{beamer}
\usepackage{tikz,ifthen}
\usetikzlibrary{calc, intersections,through,backgrounds}
\usepackage{pgfpages}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage{amsmath,amssymb,amsthm,mathrsfs,bbm,bm}
\usepackage{graphicx,subcaption,float,enumerate}
\begin{document}
\begin{frame}{Slide}
    \begin{tikzpicture} 
        \draw[help lines] (0,0) grid (0.5\textwidth,0.5\textheight);
    \end{tikzpicture}
\end{frame}
\end{document}

When I remove the 0.5, why is the grid off the slide in all dimensions?

Note: There is this related unanswered question. This question is not specifically about this MWE, just in general how to look at a slide, pick a point with my eye, and put something there exactly and absolutely in tikz.

9
  • 2
    Use \begin{tikzpicture}[remember picture, overlay] for the pictures. You can fix the origin of your tikzpictures with something like: \node[xshift=<x>,yshift=<y>] at (current page.south west) with <x> and <y> the real position of the node measured from the bottom left corner of the frame.
    – Skillmon
    Apr 20, 2018 at 18:49
  • 2
    My previous comment was stupid, I forgot remember picture. So the correct way (IMHO) is \begin{tikzpicture}[overlay,remember picture] \draw[help lines] (current page.south west) grid (current page.north east); \end{tikzpicture}.
    – user121799
    Apr 20, 2018 at 19:18
  • 2
    @marmot I was just about to write a comment :)
    – Skillmon
    Apr 20, 2018 at 19:19
  • 1
    @Skillmon Always remember remember picture ;-) Any chance you want to answer the question?
    – user121799
    Apr 20, 2018 at 19:19
  • 1
    In your example, in the settings you use, beamer wants to center the picture vertically and just puts it with some standard indent. If you were to add stuff, that would move the picture around. If you increase width and/or height, the picture will eventually overshoot. Therefore, you will be better off if you follow @Skillmon's advice and use overlay,remember picture.
    – user121799
    Apr 20, 2018 at 19:32

2 Answers 2

5

If you use remember picture, overlay as options to a tikzpicture you can then use current page as a coordinate (well actually you use .north or stuff like that, just like you can use node.north as the top of a node named node).

The remember picture has the effect that the positioning of the picture is adapted to match the specified positions because the tikzpicture is not guaranteed to be at the bottom left of the current page -- you use it somewhere in the frame environment and it is there as far as LaTeX is concerned. TikZ stores that position in the .aux file and moves the picture accordingly on the second run. Therefore you need at least two runs with this approach to get correct positioning. overlay just makes sure that your picture doesn't have a size as far as LaTeX's typesetting is concerned (it still introduces white space around the spot it is used in if used at the wrong place -- make sure to not start a new paragraph accidentally and put a % after \end{tikzpicture} to not introduce unwanted spaces).


If you want to place arbitrary stuff on a specified coordinate on a frame, I'd use a \node in a tikzpicture which I know is on that slide. For a small template I created for myself I used the following approach:

  1. Put a tikzpicture in the beamer template background. In that tikzpicture put a macro like \MyPlacedStuff and after the tikzpicture put \gdef\MyPlacedStuff{}.

  2. In a frame you can now add stuff with \xdef\MyPlacedStuff{\unexpanded\expandafter{\MyPlacedStuff <stuff you want to place on the slide>}}

You should write a macro or two around point 2 to ease the process.

The following does that and adds the help lines if you use \HelpLinestrue outside of the frame (until you use \HelpLinesfalse).

\PlaceAt takes the coordinates in multiples of \paperwidth and \paperheight as first argument in (), optional arguments to a \node in [], and finally the contents of the node in {}. The first and last are mandatory, options to the node are optional.

\AddToPlaced takes only one argument, which should be able to contain anything you can add to a tikzpicture.

Beware the background might already contain stuff which you hereby would overwrite. You'll have to sort that out if it conflicts. It seems to not conflict in your MWE.

\documentclass[xcolor=dvipsnames]{beamer}
\usepackage{tikz,ifthen}
\usepackage{xparse}
\usetikzlibrary{calc, intersections,through,backgrounds}
\usepackage{pgfpages}
\usetheme[progressbar=frametitle]{metropolis}

\newif\ifHelpLines
\newcommand*\MyPlacedStuff{}
\setbeamertemplate{background}{%>>>
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle(\paperwidth,\paperheight);
    \ifHelpLines
      \draw[help lines] (0,0) grid (\paperwidth,\paperheight);
    \fi
    \MyPlacedStuff
  \end{tikzpicture}\gdef\MyPlacedStuff{}}

\NewDocumentCommand{\PlaceAt}{ >{\SplitArgument{1}{,}}r() O{} +m }
  {%
    \PlaceAtB#1{#2}{#3}%
  }
\NewDocumentCommand{\PlaceAtB}{ m m m +m }
  {%
    \xdef\MyPlacedStuff%
      {%
        \unexpanded\expandafter{\MyPlacedStuff
          \node[#3] at (#1\paperwidth,#2\paperheight) {#4};%
        }%
      }%
  }
\newcommand{\AddToPlaced}[1]
  {%
    \xdef\MyPlacedStuff%
      {%
        \unexpanded\expandafter{\MyPlacedStuff#1}%
      }%
  }

\begin{document}
\HelpLinestrue
\begin{frame}{Slide}
  \PlaceAt(.8,.6){This text}
  \AddToPlaced{\draw (.1,1) -- (.9\textwidth,.9\textheight);}
\end{frame}
\end{document}

enter image description here

8
  • thank you for your answer. It accomplishes the goal of the MWE, but perhaps Im not being clear. My basic question is this: How does positioning work in a beamer slide, and a tikz image? I guess a long answer to that is the beamer doc and the tikz docs. Is there a shorter answer around?
    – user79950
    Apr 20, 2018 at 20:08
  • 1
    @user79950 In beamer you get your frame filled from top to bottom with stretches inserted depending on your positioning (vertically centred, top aligned, ...). The frame's title and subtitle are added at first, then the body, then the foot. There might be some other elements, but those are the main parts. In TikZ you put stuff at positions inside the tikzpicture using coordinates. The tikzpicture has its own coordinate system. It is converted to a single object with a bounding box for TeX's typesetting. To mix the two worlds (beamer and TikZ), you can use something like the above.
    – Skillmon
    Apr 20, 2018 at 20:13
  • 1
    @user79950 to be clearer: beamer doesn't use TikZ coordinates for positioning but a positioning based on TeX's typesetting algorithms. There simply is no "absolute grid" in beamer by default.
    – Skillmon
    Apr 20, 2018 at 20:35
  • 2
    @user79950 You asked a similar question here, right? I think that you may benefit from trusting TeX to do the positioning for you. Personally I almost never use absolute placement but only relative. I can see that in the very beginning it is a bit hard to anticipate where certain elements will be placed, but trust me that after a relatively short time you'll have figured that out. I regard it as a big advantage of LaTeX that it takes care of the positioning and lets me focus on the relevant things.
    – user121799
    Apr 21, 2018 at 1:05
  • 1
    @user79950 all those beamer elements aren't aligned to any coordinate system. It is just TeX's typesetting fitted into a slide. There is no real coordinate system in beamer. Just a page and a measurement how much of it is filled. Every frame environment inserts those common elements (headline, frame title, body, footline, etc.) and stacks those over each other. The closest you can get to what you want is either a \begin{tikzpicture}[remember picture,overlay] or the above approach.
    – Skillmon
    Apr 22, 2018 at 2:03
2

For what its worth, this is a simple example of something I was trying to achieve.

\begin{frame}
    \begin{tikzpicture}[remember picture,overlay] 
        \node at (current page.north west) [fill=red!20,rounded
    corners,below right] {Top Left}; 
    \node at (current page.south west) [fill=red!20,rounded
    corners,above right] {Bottom Left}; 
    \node at (current page.south east) [fill=red!20,rounded
    corners,above left] {Bottom Right}; 
    \node at (current page.north east) [fill=red!20,rounded
    corners,below left] {Top Right}; 
    \node at (current page) [fill=red!20,rounded
    corners] {Center}; 
    \end{tikzpicture}
\end{frame}

You must log in to answer this question.

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