# Using beamer tools in tikz, without creating slides

I want to create a tikz picture that makes usage of beamer commands such as \only<2>, thereby creating multiple pages and adding lines etc. only in the second page. I then later on want to include these pages in a presentation.

Currently, I'm doing that by using \documentclass{beamer}, and putting the tikzpicture inside a frame. However, this forces me to finish the presentation in this document: I need to specify frame title, and also footer, and presentation styles.

I would much rather just create the plain pictures here, include them in the other presentation using \begin{frame}{Foo} \includegraphics{...}\end{frame}. However, when I do that inside beamer, there is unnecessary white space around the tikzpictures. When I don't do it inside beamer, I cannot use the \only. What should I do?

%\documentclass[tikz]{standalone}
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{frame}

\begin{tikzpicture}
\begin{axis}
\addplot[blue, thick] {x};
\only<2>{\addplot[blue, thick, dashed] {2*x};}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}

• There is the beamerarticle package (part of the beamer class) that allows you to use beamer commands in other classes (such as article), which might be useful here. See page 209 of the manual. Feb 20 '19 at 13:02
• @Marijn Using \usepackage[noxcolor]{beamerarticle} allows me to compile everything as tikz standalone, but will only show the "last page" of each slide, instead of having several pages. Unfortunately, I cannot find this use caseto be detailed in the manual. Feb 20 '19 at 13:54
• How about standalone with \standaloneframes, as e.g. in tex.stackexchange.com/a/168705/121799 ?
– user121799
Feb 20 '19 at 14:58
• @marmot Better, but pictures are not full scale in the page. Including the pdfs in other presentation will add unnecessary white space. Feb 20 '19 at 16:20
• Doesn't tex.stackexchange.com/a/168705/121799 specifically tell you how to include pictures of that sort? (Or are you just looking for an ordinary \ifnum? See e.g. tex.stackexchange.com/a/459641/121799 for how to use it for drawing certain things only on certain "frames". Sometimes \ifcase can be useful.)
– user121799
Feb 20 '19 at 16:24

## 1 Answer

UPDATE: standalone and beamer and \only do work, just not with crop but with preview.

\documentclass[beamer,tikz,preview]{standalone}
\usepackage{pgfplots}
%\usetikzlibrary{overlay-beamer-styles} % for advanced overlay options
\pgfplotsset{compat=1.16}
\begin{document}
\begin{standaloneframe}
\begin{tikzpicture}
\begin{axis}
\addplot[blue, thick] {x};
\only<2>{
\addplot[blue, thick, dashed] {2*x};
}
\end{axis}
\end{tikzpicture}
\end{standaloneframe}
\end{document}


This becomes much simpler and better IMHO when using overlay-beamer-styles.

\documentclass[beamer,tikz,preview]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{overlay-beamer-styles} % for advanced overlay options
\pgfplotsset{compat=1.16}
\begin{document}
\begin{standaloneframe}
\begin{tikzpicture}
\begin{axis}
\addplot[blue, thick] {x};
\addplot[blue, thick, dashed,visible on=<2>] {2*x};
\end{axis}
\end{tikzpicture}
\end{standaloneframe}
\end{document}


OLD ANSWER (perhaps also useful for some applications, so I keep it for now): Maybe something like this? The role of beamers only is taken by \ifnum.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\foreach \X in {1,2}
{\begin{tikzpicture}
\begin{axis}
\addplot[blue, thick] {x};
\ifnum\X=2
\addplot[blue, thick, dashed] {2*x};
\fi
\end{axis}
\end{tikzpicture}}
\end{document}


Of course, you can make much more elaborate examples. Just like in beamer presentations with \only, a multipage pdf is generated. Here, however, the figures are cropped at their bounding boxes.

Of course, in a real example you may want to make sure that the coordinates do not jump, e.g. with

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\foreach \X in {1,2}
{\begin{tikzpicture}
\begin{axis}
\addplot[blue, thick] {x};
\ifnum\X=2
\addplot[blue, thick, dashed] {2*x};
\else
\addplot[blue, thick, dashed,opacity=0] {2*x};
\fi
\end{axis}
\end{tikzpicture}}
\end{document}


• This does what I wanted, albeit I find \ifnum to be a lot of overhead. I'd still be interested in a generic solution that directly allows the beamer tools such as \alert<2> etc, which all can be done with this technique but generate quite messy documents Feb 20 '19 at 16:43
• @FooBar You're right. I added a proposal that does precisely that. (I first tried using crop but this didn't work, yet preview does. Whether or not crop should work, I don't know.)
– user121799
Feb 20 '19 at 17:39