3

I have created a R markdown (RStudio) document and I would like to have that output in pdf combined with some part (i.e. first page) of my LaTeX document. So basically, I would like to do some bits with R Markdown (generating some R output) and write something down in LaTeX and have this two combined into 1 pdf document.

What would be the steps? Thx.

Maximilian
  • 4,177
  • 7
  • 46
  • 85
  • 2
    generate TeX from the Rmd (`pandoc -o stuff.tex stuff.rmd`) and `\include{}` it in the LaTeX document? – Ben Bolker Aug 23 '13 at 14:55
  • @Ben: This would be possibly one option but still wonder whether the Rmd+tex=pdf is possible. – Maximilian Aug 23 '13 at 15:36
  • OK, do you prefer to generate two PDF documents and merge them? – Ben Bolker Aug 23 '13 at 15:48
  • would that be within R? – Maximilian Aug 23 '13 at 15:50
  • the answer seems yes. Here: http://stackoverflow.com/questions/13273611/how-to-append-a-plot-to-an-existing-pdf-file, – Maximilian Aug 23 '13 at 16:13
  • 2
    Have you tried simply writing your LaTeX code in the .Rmd file? Normally pandoc recognizes LaTeX code, and uses it when converting to PDF. I use basic LaTeX in my .md/.Rmd files all the time, /newline, special symbols, tables and such. – Maxim.K Aug 23 '13 at 19:50
  • I don't necessarily think you can merge two *arbitrary* PDF files within R (i.e. without external tools such as `pdftk`) -- the example you linked is for adding a *plot* to an existing PDF ... – Ben Bolker Aug 23 '13 at 22:43
  • 1
    You can merge pdfs in latex with pdfpages. – Thomas Aug 24 '13 at 06:06

1 Answers1

1

To demonstrate how to insert pdf pages using the pdfpages package, I first create a pdf file with four pages using the pdf() command.

The .Rnw (Knitr) script is as following

\documentclass[a4paper]{article}
\usepackage[final]{pdfpages}
\usepackage[pagestyles]{titlesec}
\usepackage{hyperref}
\title{An example on how to add external pdf pages}
\begin{document}
\maketitle
\tableofcontents
\section{First section}
\subsection{First subsection}
This is an empty section with a chunk to create one pdf with four pages

<<mtcarsplot, echo = TRUE, eval = TRUE,  fig.show='hide'>>=
library(knitr) 
pdf(file="figure/mtcarsplot.pdf",onefile=TRUE)
ggplot(mpg, aes(drv, model)) +
      geom_point() +
      facet_grid(manufacturer ~ ., scales = "free", space = "free") +
      theme(strip.text.y = element_text(angle = 0))
ggplot(mtcars, aes(wt, mpg))+ geom_point(aes(colour=factor(cyl), size = qsec))
ggplot(mpg, aes(x=factor(cyl), y=hwy, fill=factor(cyl)))+ geom_violin(scale = "width")
mosaicplot(Titanic, color = TRUE)
dev.off()
@

\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}
\includepdf[pages={1-2,{},4},nup=2x2]{figure/mtcarsplot.pdf}

\end{document}

To insert pages for the external document use

\includepdf[<key=val>]{<filename>}

The simplest will be \includepdf[pages=-] which will include all the pages from the document.

The code to include pages is

 \includepdf[pages={1-2,{},4}],nup=2x2]{figure/mtcarsplot.pdf}

{1-2,{},4} means that I included page 1 to 2, a blank page, then page 4.

The other command is nup=2x2 which has included four pages in two rows and two columns withing the same page.

It is often usefull to include external pdf as sections or subsections of the document created, this is done with :

\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}

The output shows the four pages in one page, with one left blank, and the table of contents with the phantom section.

pdfpage_example

Cedric
  • 2,412
  • 17
  • 31