[Tex/LaTex] Positioning and Coordinates in Tikz+Beamer

beamertikz-pgf

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.

Best Answer

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