Skip to content

Instantly share code, notes, and snippets.

@yihui
Created February 11, 2012 20:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yihui/1803930 to your computer and use it in GitHub Desktop.
Save yihui/1803930 to your computer and use it in GitHub Desktop.
Another minimal beamer example for knitr
% this is a simplified version of
% https://github.com/yihui/knitr/blob/master/inst/examples/knitr-beamer.Rnw
\documentclass{beamer}
\begin{document}
<<setup, include=FALSE>>=
# smaller font size for chunks
opts_chunk$set(size = 'footnotesize')
@
\title{A Minimal Demo of knitr}
\author{Paul Hiemstra \and Yihui Xie}
\maketitle
% very important to use option [fragile] for frames containing code output!
\begin{frame}[fragile]
You can test if \textbf{knitr} works with this minimal demo. OK, let's
get started with some boring random numbers:
<<boring-random>>=
set.seed(1121)
(x=rnorm(20))
mean(x);var(x)
@
\end{frame}
\begin{frame}[fragile]
The first element of \texttt{x} is \Sexpr{x[1]}. Boring boxplots
and histograms recorded by the PDF device:
<<boring-plots, fig.width=5, fig.height=5, out.width='.45\\linewidth', fig.show='hold'>>=
## two plots side by side (option fig.show='hold')
boxplot(x)
hist(x,main='')
@
\end{frame}
\end{document}
@mallerhand
Copy link

font size option is not working for me. (The older trick with the footnote environment works). The script above gives me normalsize whatever size I ask for, (?highlight). Have tried many options. Want to shrink the size of R code in a frame.
Windows 7
MikTeX 2.9
R 2.15.0
tikzDevice_0.6.2
knitr_0.5

@mallerhand
Copy link

Possible weird clue, the size option has an effect on hash and quote marks. For example in the comment line about two plots side by side, the hash signs and the quotes get bigger or smaller as I change:
size = 'huge'
size = 'tiny'
But the rest of the comment and all the R code stays the original size. Something to do with available fonts?

@yihui
Copy link
Author

yihui commented May 12, 2012

you need to install the development version; this will be a new feature in v0.6: https://github.com/yihui/knitr/blob/master/NEWS

@mallerhand
Copy link

Thanks for response and link to news of v0.6. Development version not easy to install under Windows, (no Rtools for R-2.15.0, spaces in paths problem, too much). Maybe come back when 0.6 is on CRAN.

@yihui
Copy link
Author

yihui commented May 12, 2012

I have pushed a version to R-Forge, so you should be able to install the Windows version once the build is ready there: https://r-forge.r-project.org/R/?group_id=656

@ptoche
Copy link

ptoche commented Dec 4, 2014

Would you have a beamer example with references working properly for figures and tables?

The reference bit works, but the label does not print in the figure caption:
<<Figure1, fig.cap = 'My Figure'>>=
...etc.
plot(x, y)
@
\end{frame}
The above has a caption like: Figure: My Figure instead of Figure 1: My Figure

This \ref{fig:Figure1} prints as Figure 1, as expected.

And how to do the same with a table, e.g.
<<Table1, echo = FALSE, results = 'asis'>>=
library(xtable)
xtable(summary(lm(y~x))$coefficients, caption ="Summary")
@
Couldn't get it to work at all inside the beamer class.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment