knitr: keep_tex not keeping the tex file

Occasionally I have to tweak a .tex file generated by knitr, and I have included the following in the prefix to the .Rmd file:

output: 
    pdf_document: 
    keep_tex:  true

Now, however, including this no longer keeps the .tex file. What do I have to do to save the .tex file when the compiled tex is displayed?

Thanks for your help.
Larry Hunsicker

Hmm - at first glance the YAML options look okay. How are you knitting your file? With Rstudio knit button? using knitr::knit(your_file)?

I used the RStudio knit button. But given your question, I used knitr:knit("my file'). That generated a new file with the extension .md, and a subdirectory with some of my graphics. But still no .tex file. The .md file appears to be the result of some massaging of the ,Rmd file, generating the graphics before "texing" the .Rmd. Am I correct about that?

Sort of! Here’s a description of the process starting with rmarkdown:render() (the more typical starting point, and what the RStudio knit button is running under the hood):

When you run render , R Markdown feeds the .Rmd file to knitr ⧉, which executes all of the code chunks and creates a new markdown (.md) document which includes the code and its output.

The markdown file generated by knitr is then processed by pandoc ⧉ which is responsible for creating the finished format.

(source)

Note that running rmarkdown::render() yourself isn’t quite the same as using the knit button, unless you make sure to start a new R session first — see here: 2.2 Compile an R Markdown document | R Markdown: The Definitive Guide

Edited to add:

Maybe double check that your YAML indentation is correct in the actual file? It doesn’t look quite right in your pasted example above, but that could be a copy-paste artifact. YAML is very picky about indentation, and indentation problems are the most common source of this sort of issue. Correct indentation for this case (from the RMarkdown book):

---
title: "Habits"
output:
  pdf_document:
    keep_tex: true
---
3 Likes

Actually away from the computer right now so I can’t test anything out. However, as a temporary fix you could try going old school and use Sweave(‘your fille’). Should just generate a tex file which you’ll have to compile separately.

Amazing!!! Adding a tab to indent "keep_tex" fixed the problem! Whodathunk?
Many thanks.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.