# Filling a full beamer slide with tikzDevice output from R

Q: What's the best way to fill an entire beamer slide with tikzDevice output (i.e., the .tex file output from the tikz() function in R's tikzDevice package)?

I've included some undesirable examples below. In summary:

1. Example 1: Can see that the input .pdf looks sloppy.
2. Example 2: Can see that simply inputting the tikzDevice output leads to spilling out over the slide
3. Example 3: Can see that simply inputting the tikzDevice output designed to fill the text region works just fine. But this is not my goal. I want to fill the entire slide
4. Example 4: Can see that the \resizebox{} leads to spillage. I was surprised this didn't work
5. Example 5: Including the tikzDevice output within another tikzpicture environment. This actually works fine. But a tikzpicture environment within a tikzpicture environment? Seems hacky. There must be a better way.
6. Example 6: Well, there's no actual example here. I'm just hypothesizing that there are some minimal edits one can make directly to the tikzDevice output which will lead to the desired outcome. But I've mucked around and come up with nothing.

R Code

################### Setup
library(tikzDevice)
beamer.parms = list(paperwidth   = 364.19536/72,  # converts pt to in
paperheight  = 273.14662/72,
textwidth    = 307.28987/72,
textheight   = 269.14662/72,
x = rnorm(1000)

#################### PDF Plot
pdf(file = "./img/img0010a-eg.pdf",
width = beamer.parms$paperwidth, height = beamer.parms$paperheight)
plot(x, main = "my title", las = 1, type = "n")
grid(lwd=2, lty="dashed", col="gray")
points(x)
dev.off()

######################## TikZ image to fill full beamer slide
tikz("./img/img0010b-eg-full.tex",
width = beamer.parms$paperwidth, height = beamer.parms$paperheight)
plot(x, main = "my title", las = 1, type = "n")
grid(lwd=2, lty="dashed", col="gray")
points(x)
dev.off()

######################## TikZ image to fill text region of beamer slide
tikz("./img/img0010c-eg-text.tex",
width = beamer.parms$textwidth, height = beamer.parms$textheight)
plot(x, main = "my title", las = 1, type = "n")
grid(lwd=2, lty="dashed", col="gray")
points(x)
dev.off()


tikzDevice output (img0010b-eg-full.tex)

\begin{tikzpicture}[x=1pt,y=1pt]
<coloring commands>
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (365.56,274.17);
<more coloring commands>
<drawing commands>
\end{scope}
\end{tikzpicture}


LaTeX Code

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}  % fills a full slide, put is ugly because of pdf input
\begin{tikzpicture}[remember picture,overlay]
\node[at=(current page.center)] {
\includegraphics[width=\paperwidth, height=\paperheight]{./img/img0010a-eg}

};
\end{tikzpicture}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}  % spills off to the right
\input{./img/img0010b-eg-full.tex}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}  % fills the text region of the slide. But goal is to fill ALL.
\input{./img/img0010c-eg-text.tex}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}  % Surprised this didn't work. Spills off to the right.
\resizebox{\paperwidth}{\paperheight}{%
\input{./img/img0010b-eg-full.tex}
}%
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}  % This works. But a tikzpicture environment inside of a
% tikzpicture environment? This seems hacky. Must be a better
% way.
\begin{tikzpicture}[remember picture,overlay]
\node[at=(current page.center)] {
\input{./img/img0010b-eg-full.tex}
};
\end{tikzpicture}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}


Resulting slides

-
You can use tikz with R .. ?! Thank you so much for alerting me to this fact! –  mathematical.coffee Mar 1 '12 at 23:28
It's incredible really. By inputting R plots output through tikzDevice you get the plot font matching the style and size of the surrounding latex document. You can edit all the text of the plot to include complicated mathematics as well. It's great! I just need to be able to fill a god-damned slide! I can't believe this is so hard. –  brianjd Mar 1 '12 at 23:35
@MartinSchröder Is there a smart way to migrate this question over without just copy, pasting, and re-uploading everything? I can't seem to find an alternative. –  brianjd Mar 2 '12 at 19:52
I'm not sure if this post should really be migrated here... tikzDevice is specific to R, whereas beamer is for tex. I have given an answer specific to R, anyway. –  Yihui Mar 5 '12 at 7:43
possible duplicate of Image on full slide in beamer package –  diabonas Mar 5 '12 at 12:32

## migrated from stackoverflow.comMar 2 '12 at 23:38

This question came from our site for professional and enthusiast programmers.

I think you can do this with the knitr package if you have some experience with Sweave (knitr is an alternative to Sweave with many enhancements including support to tikzDevice). I have created a full example at https://gist.github.com/1977162 and you can compile it by

library(knitr); knit('fullfig.Rnw') # or knit2pdf('fullfig.Rnw')


The output looks like this:

One thing I did not understand about your post was so many tricks, and what I did is quite simple: I just use the normal \includegraphics[width=.9\paperwidth] to include the plot in the beamer slide. However, knitr did one important thing in the background: convert the tikz file to PDF after creating the tikz plot with tikzDevice (this is the default behavior when chunk option dev='tikz'). There are long stories behind knitr, but I guess my answer here is enough for this specific question.

One obvious advantage of using knitr is that you can generate the slides dynamically with one command (you do not need to open R, library(tikzDevice), draw the plot, insert it into beamer manually and compile). If you want to know more about knitr, you can go to http://yihui.name/knitr/

-

I often use:

\begin{frame}[plain]
\hbox to \hsize{\hss
\input{./img/img0010b-eg-full.tex}%
\hss}
\end{frame}


(That is equivalent to your last example, but without Tikz.)

The problem is that there is a small margin on the left of the page, and you have to somehow ignore it if you want the picture to bleed to the edges of the page.

-
Interesting approach. Approach also did not cover up navigation symbols at the bottom. Another advantage of the tikzpicture within a tikzpicture approach. –  brianjd Mar 9 '12 at 19:51

The problem is the left margin of 1cm set by beamer that shifts your diagram to the right. I see two possibilities to overcome this:

1. Removing the margins with \newgeometry{margin=0pt}, including the figure and restoring the original margins with \restoregeometry afterwards.
2. Painting the figure into the background, which isn't affected by the page margins.

My experiments showed that the first option could have some strange side effects, so I'd go with the second solution:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\begin{frame}
\maketitle
\end{frame}

{
\setbeamertemplate{background}{\input{./img/img0010b-eg-full.tex}}
\frame{}
}
\end{document}


The braces are for keeping the changes local (you don't want the figure to appear as a background on every slide), the actual frame is an empty one because the diagram is already drawn in the template.

Not having R installed, I used this picture as the demo figure img0010b-eg-full.tex:

\begin{tikzpicture}
\clip (0,0) rectangle (\paperwidth,\paperheight);
\filldraw[draw=black,line width=10pt,fill=blue] (0,0) rectangle (\paperwidth,\paperheight);
\end{tikzpicture}


The result looks like this:

-