25

I made a presentation in RStudio with RMarkdown/Knitr - it compiles without problems to a pdf (via LaTeX Beamer).

But I cannot get the LaTeX file. Is there any way to export also the LaTeX file which should be produced in the conversion?

2
  • 10
    Add the option output: pdf_document: keep_tex: yes
    – Werner
    Commented Sep 14, 2016 at 20:23
  • 5
    Which is also accessible thorugh the compiling options next to the knit button. Commented Sep 14, 2016 at 20:49

2 Answers 2

21

(This was not the original accepted answer, but has since been accepted and then edited to reflect this change.)

As of late 2018, the following works:

---
title: "Untitled"
author: "Author Person"
date: "November 26, 2018"
output: 
  pdf_document: 
    keep_tex: yes
---

Note that this answer is the YAML automatically inserted by the RStudio program (circa 2018) when you follow the instructions above to use the GUI to make this setting. So I assume the formatting is in the preferred style.

6
  • This is what I was looking for and it works. When exporting ,the .tex file is stored in a tmp folder. Would you know a way to store the .tex file somewhere else, say where you also save the .rmd file?
    – Jelle
    Commented Jan 20, 2019 at 16:58
  • my .tex is put in the folder containing the rmd file.
    – Alex
    Commented Jan 24, 2019 at 4:55
  • works also if beamer_presentation: is used instead of pdf_document. As of 2022, the keep_tex: part must be indented!
    – PatrickT
    Commented Jan 31, 2022 at 0:40
  • Thank you for bringing that to my attention (although I saw it very late). Can the answer be edited to not confuse others with the accepted answer remark?
    – Revan
    Commented Feb 16, 2023 at 8:32
  • Yep, apparently I can, and it is done. Commented Feb 17, 2023 at 18:31
17

enter image description here In RStudio:

  • Click the gear - options button next to knit.
  • Click output options.
  • Click advanced.
  • Click Keep tex source file . . .

to answer the 1st comment, here is some sample LaTeX

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{DF <-}\KeywordTok{read.table}\NormalTok{(}
\DataTypeTok{text=}
\StringTok{"Year State Histadrut Private}
\StringTok{1985     27    26       47}
\StringTok{1993     10    14       76}
\StringTok{"}\NormalTok{,     }\DataTypeTok{header=}\OtherTok{TRUE}\NormalTok{)}

\KeywordTok{library}\NormalTok{(ggplot2)}
\KeywordTok{library}\NormalTok{(reshape2)}

produced by compiling

some simple RMD

```{r}
DF <-read.table(
text=
"Year State Histadrut Private
1985     27    26       47
1993     10    14       76
", header=TRUE)

library(ggplot2)
library(reshape2)
```

The output looks like: pdf output

3
  • What happens to r code chunks or plots produced by code chunks? How do they translate to LaTeX?
    – acylam
    Commented Dec 15, 2016 at 18:47
  • 1
    The backend is pandoc. The code chunk can be executed, or not, and echoed into your pdf, or not. You can have the graph or table output in your pdf with or without some portion of the code that was used to produce it. Commented Dec 15, 2016 at 19:36
  • The indentation appears to have changed and this doesn't work anymore. See the other answer for indentation that works as of 2022.
    – PatrickT
    Commented Jan 31, 2022 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.