2
votes

The problem:

I have several Rmarkdown documents representing different sections of my thesis (e.g. chapter 1 results, chapter 2 results), and I would like to combine the knitted latex versions of these documents with other TeX files into a master TeX 'thesis' document. The problem is, that when knitting to PDF and keeping the TeX file to include in the master TeX document, Rstudio auto-generates a bunch of preamble that ends up clashing with my preamble in the master.TeX document.

A less than ideal, workaround:

I have been working around this issue by deleting this preamble by hand before including the knitted output into the master.tex file.

So far my current workflow is as follows:

  • set the output to pdf_document in the YAML header, with the keep_tex option set to true.
  • knitPDF using the Rstudio button
  • delete the preamble at the beginning of the knitted TeX files by hand.
  • include the files into my master.tex file using \include{}

The question:

I would like to convert Rmd to LaTeX, without having to delete the preamble manually before including the knitted TeX files into a master TeX file. How do I do this?

Sweave is not an option:

I'm sure I could achieve this by working with Sweave, however I like the flexibility of output formats that Rmarkdown offers: I can knit to html and view in my browser as I'm writing, to quickly view the progress of my work and ensure that there are no bugs, and I can also choose to knit to word to share with my supervisor who works only in word. Then, when I'm ready to compile everything into my master LaTeX document for printing, I can select knit to PDF in Rstudio to obtain the latex output.

2

2 Answers

3
votes

Here is a simpler solution using the LaTeX package docmute.

Your main Rmarkdown document, (e.g., main.Rmd) should load the docmute package and include your other files (e.g., part1.Rmd and part.Rmd), once knitted and located in the same directory, with \input or \includelike this:

---
title: "Main"
output: pdf_document 
header-includes:
- \usepackage{docmute}
---
# Part 1
\input{part1.tex}
# Part 2
\input{part1.tex}

Then your other files just need to have a keep_tex argument in the YAML front matter. Like this:

---
title: "Part 1"
output: 
  pdf_document:
    keep_tex: true
---

Something

and this:

---
title: "Part 2"
output: 
  pdf_document:
    keep_tex: true
---

Something else

Then all you need to do is knit part*.Rmd's before main.Rmd and all the content will appear in main.tex and main.pdf. The docmute package strips the preamble from the input .tex files so you will need to make sure anything you need before \begin{docunent} in the input files is also in main.Rmd.

4
votes

I have since come up with a workaround:

create_latex <- function(f){
  knitr::knit(f, 'tmp-outputfile.md');
  newname <- paste0(tools::file_path_sans_ext(f), ".tex")
  mess <- paste('pandoc -f markdown -t latex -p -o', shQuote(newname),"tmp-outputfile.md")
  system(mess)}

The function above takes an Rmd file as its input, knits the file to md, and then converts it to TeX by calling pandoc from the command-line.

The secret ingredient lies in the pandoc call... When knitting using Rstudio, Rstudio must be calling the pandoc standalone -s flag when it compiles the pdf. This generates a 'standalone' document, i.e. one that contains latex preamble. This is obviously necessary when you want to view the PDF, but conflicts with my needs.

I am instead seeking to generate a latex 'fragment' from Rmd with knitr, that can be later incorporated into my master Latex file. So the solution was simply to create a pandoc command line call that omits the -s standalone flag. I achieved this by calling pandoc from R with system() inside the above function.

Hope this helps anyone out there having this problem, but would be great if I was able to change Rstudio's settings to avoid bothering with this hack. Suggestions and feedback welcome.