# Tikz: Problem using “of” on subsequent Beamer frames

Compiling this MWE yields the desired result: a beamer frame with two nodes positioned relative to each other.

\documentclass[mathserif]{beamer}
\usepackage{pgfplots}
\begin{document}

\begin{frame}
\begin{figure}[t]
\begin{tikzpicture}[node distance=5mm and 5mm]
\usetikzlibrary{positioning}
\node (inc) [label=above:Incumbent] {};
\node (ent)   [below left=25mm of inc,label=left:Entrant] {};
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}


The following code, which includes a second copy of exactly the same frame, however, causes PDFLaTeX to throw up the error ! Package PGF Math Error: Unknown operatoro' or of' (in '25mm of inc').

\documentclass[mathserif]{beamer}
\usepackage{pgfplots}
\begin{document}

\begin{frame}
\begin{figure}[t]
\begin{tikzpicture}[node distance=5mm and 5mm]
\usetikzlibrary{positioning}
\node (inc) [label=above:Incumbent] {};
\node (ent)   [below left=25mm of inc,label=left:Entrant] {};
\end{tikzpicture}
\end{figure}
\end{frame}

\begin{frame}
\begin{figure}[t]
\begin{tikzpicture}[node distance=5mm and 5mm]
\usetikzlibrary{positioning}
\node (inc) [label=above:Incumbent] {};
\node (ent)   [below left=25mm of inc,label=left:Entrant] {};
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}


Does anyone know why this happens, and how I can have multiple slides using the of positioning command in my presentation?

• The \usetikzlibrary part should go into the preamble and not been executed twice. It loads an external package that may well define global symbols, hence the clash. – Daniel Jan 24 '14 at 17:19
• I have added this as an answer. – Daniel Jan 25 '14 at 21:21

Load packages and libraries only in the preamble!

The issue is caused by the \usetikzlibrary{positioning} inside the frame environment.

Generally, packages in LaTeX are intended to be loaded only inside the preamble of the document. Many of them introduce global elements, add to document-specific hooks, or use other features that may lead to unwanted side effects if loaded inside a scope or behind \begin{document}. This holds for \usepackage, but also for most package-specific extension mechanisms, such as \usetikzlibrary.

The following compiles without any problems:

\documentclass[mathserif]{beamer}
\usepackage{pgfplots}
\usetikzlibrary{positioning}  % Always load TikZ libraries in the preamble!
\begin{document}

\begin{frame}
\begin{figure}[t]
\begin{tikzpicture}[node distance=5mm and 5mm]
\node (inc) [label=above:Incumbent] {};
\node (ent)   [below left=25mm of inc,label=left:Entrant] {};
\end{tikzpicture}
\end{figure}
\end{frame}

\begin{frame}
\begin{figure}[t]
\begin{tikzpicture}[node distance=5mm and 5mm]
\node (inc) [label=above:Incumbent] {};
\node (ent)   [below left=25mm of inc,label=left:Entrant] {};
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}