knitr: automate producing multiple versions of PDF for beamer slides - r

For lectures, I am using knitr to produce LaTeX beamer slides as a PDF. For a given lecture, I want to produce also (a) 1-up handout (using the handout option, and (b) the same handout formatted 4-up.
I find I have to run knitr 3 times to do this as shown below. Is there a way to simplify this work flow?
A lecture stub:
\documentclass[10pt,table]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
...
\end{document}
And I run knitr as
knit2pdf("Lecture1.Rnw")
To get the 1-up handout (which suppresses the separate pages when you use transitions), I edit the first line to:
\documentclass[10pt,table,handout]{beamer}
and run
knit2pdf("Lecture1.Rnw" output="Lecture1-1up.tex")
Finally, to get the 2 x 2 version, I use the LaTeX pgfpages package,
\documentclass[10pt,table,handout]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}[letterpaper,landscape]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
And run:
knit2pdf("Lecture1.Rnw" output="Lecture1-4up.tex")
(I found that with beamer, I could not simply print the PDF 4-up using Adobe Acrobat -- it generated a corrupt PDF file. I was forced to use pgfpages)
Then, of course I have to revert my .Rnw file to the original if I need re-do the slides. Very tedious. There must be a better way.

Related

Compile Bookdown to Markdown?

Is there any way to take a Bookdown project, and build it as Markdown instead of HTML or TeX?
I ask because I need to post-process the final Markdown output from Bookdown, in order to extract R and Python notebooks for download.
In more detail, I am using Bookdown to build a textbook that embeds notebooks to download, where the notebooks contain subsets of the code and text in the bookdown .Rmd files. For example, a single chapter could contain more than one notebook.
In order to do this, I put start and end comment markers in the RMarkdown input text to identify the section that will be a notebook, and then post-process the generated Markdown files to extract the notebook section. As in something like:
<!--- notebook: first_section.Rmd
-->
Some explanation, maybe using Bookdown extra markup such as #a_citation.
```{r}
a <- 1
a
```
<!--- end of notebook
-->
More markdown.
```{r}
# More code not in notebook.
b <- 2
```
Obviously I could use the input RMarkdown pages, but this would be ugly, because all the extended Bookdown markup such as citations, cross-references and so on, would appear in raw and ugly form in the generated notebook. So I'd really like to be able to get the final output Markdown, after merging, resolving of citations and cross references. Is there any way of doing that?
My question is similar to this as-yet unanswered question, but adds my motivation for an official solution to this problem.
With the latest version of bookdown on CRAN, you can use the output format bookdown::markdown_document2, e.g.,
output:
bookdown::markdown_document2:
base_format: rmarkdown::md_document
variant: gfm

Creating a title and numbering of sections on pdf while using R markdown with pandoc

I am using the markdown document created by R and I am trying to create the pdf file from the markdown using pandoc. Everything else works fine but I want the title of the document to appear and the numbering on the sections as in default Latex document. It seems the title defined on rmarkdown appears as a section title for the pdf.
I was able to create double spacing, enter line numbers etc by using a options.sty file. Below is the code that I used to create a pdf.
For options.sty I used:
\usepackage{setspace}
\doublespacing
\usepackage[vmargin=0.75in,hmargin=1in]{geometry}
\usepackage{lineno}
\usepackage{titlesec}
\titleformat{\section}
{\color{red}\normalfont\Large\bfseries}
{\color{red}\thesection}{1em}{}
\titleformat{\subsection}
{\color{blue}\normalfont\Large\bfseries}
{\color{blue}\thesection}{0.8em}{}
\title{Monitoring Stations}
\author{Jdbaba}
I used knitr to create the R markdown file. In the above options.sty file, it seems the program is not taking title and author part. It seems I am missing something.
The code I used to convert markdown to pdf is as follows:
pandoc -H options.sty mydata.md -o mydata.pdf
In latex document, the pdf would have the automatic numbering as well. But my pdf is missing that. Can anyone suggest how numbering can be enabled on the pdf document created using pandoc ?
Thanks.
Pandoc takes the title from a title block in the Markdown file. This is a Pandoc-specific extension to Markdown. The block should have the following format:
% title
% author(s) (separated by semicolons)
% date
So, in your case:
% Monitoring Stations
% Jdbaba
% March 6, 2013
To have the sections numbered, you'll need to run Pandoc with the --number-sections option.

Can I use a knitr inline expression in the title of a latex document?

I'd like to use a Knitr/Sweave in-line call (\Sexpr{}) in the title of a LaTeX document, after the \begin{document} command but before the \maketitle command. The in-line R code would extract one or two pieces of information from an R data-frame created early in the R script I'm embedding in LaTeX.
I have a couple of Knitr chunks that create a data.frame from which I derive the information I want to put in the Title. I've tried placing these chunks between LaTeX's \begin{document} call and the \title code, like this:
\documentclass
[LaTex Preamble]
\begin{document}
[%% Knitr chunks that initialize an R data-frame]
\title \Sexpr{--a snippet of R code that extracts an element from the data-frame --}
\maketitle
... (rest of the LaTeX document)
and I've also tried putting the Knitr chunks in the preamble to the LaTeX code before \begin{document} statement.
But in Knitr seems to ignore code (other than initialization) that is placed ahead of the \maketitle call in LaTeX, so the in-line snippets included the title look like errors to Latex and it halts output.
I can't find any information in the Knitr documentation on including in-line code in the Title of a LaTeX document.
Any ideas?
OK: Found the solution thanks to the hint from #ben-bolker below. Ben uses the formatting of R chunks before output to an RNW file (in a 2-step Knitr process: latex -> rnw -> pdf) . But I'm compiling the LaTeX file to PDF in one-step without going to an RNW file from inside TeXShop (on Mac OSX). I found that I could get Ben's example to work using the RNW delimiters (<<>>=) and one-step compiling. But I couldn't mix the usual LaTeX chunk-delimiters (%%begin.rcode and %% end.rcode) and the RNW in-line statement hook (\Sexpr{}). The latter didn't work no matter how I fiddled with it. Eventually I found that the correct in-line hook for LaTeX is \\rinline{}.
It's not very clear in the Knitr documentation that this is the required format for LaTeX and I found it eventually mainly thanks to Ben's example. Best, Peter
Update 2 ... and then there's RTFM (or the 'cheat sheet' in this case): http://cran.r-project.org/web/packages/knitr/vignettes/knitr-refcard.pdf
Hmm. The following file works for me:
\documentclass{article}
<<echo=FALSE>>=
x <- 5
#
\title{The number is \Sexpr{x^2}}
\begin{document}
\maketitle
Some stuff
\end{document}
with knitr version 0.8 on Ubuntu 10.04, via knit2pdf("knitr_title.Rnw") ...

How to convert .Rmd file to .Rnw file of Rstudio?

I really like using knitr in Rstudio and have been using it to write markdown presentations and data analysis. I want to use the same code and results in a paper and want to convert the code chunks in Rmd file ```{r} to the chunks of Rnw file << >>= #.
This allows using the same document and code written for presentation for the main paper as well.
Is there a way of converting between code chunks of markdown and Rnw files ?
or the entire file itself as apart from the difference in syntax of code chunks, they are quite similar in the markup (converting the text to latex is easy with say pandoc)
Instead of converting the whole document, you can just externalize your R code so it can be shared across different documents; see http://yihui.name/knitr/demo/externalization/
Once you have read_chunk('Rcode.r'), you can use ```{r label} in your Rmd and <<label>>= in your Rnw document, where label comes from the line ## #knitr label in the R script.

Getting Sweave code chunks inside some framed box?

I would like to make an R code chunk (in Sweave) printed inside a framed box in the resulting pdf.
Is there an easy solution for doing that?
The short answer is that yes, there is an easy way. Just add the following lines, or something like them to the preamble of your Sweave document:
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
frame=single}
This works because the appearance of code (and output) chunks is controlled by the definition of the Sinput and Soutput environments. These are both Verbatim environments as provided by the LaTeX package fancyvrb. (Click here for a 73 page pdf describing the numerous options that fancyvrb provides).
A quick look in the file Sweave.sty reveals the default definition of those two environments:
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
To change those definitions, just add \DefineVerbatimEnvironment statements of your own devising either: (a) at the end of the Sweave.sty file; or (b) at the start of your *.Snw document.
Finally, here's an example to show what this looks like in practice:
\documentclass[a4paper]{article}
\usepackage{Sweave}
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
frame=single}
\title{Sweave with boxes}
\begin{document}
\maketitle
<<echo=FALSE>>=
options(width=60)
#
Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.
<<>>=
print(rnorm(99))
#
\end{document}
knitr, a successor of Sweave, by default outputs all echoed R code in boxes, and also formats it to the margins. Other nice features include syntax coloring and PGF integration.
Sweave code of average complexity needs only minor if any adaptions to run with knitr.

Resources