How to export R Notebook to LaTex (.tex)? [duplicate] - r

In R, when compiling a markdown document using rmarkdown::render(...), how can I retain the intermediate .tex file that is produced from knitting.
I have tried setting the clean=FALSE argument, but this retains the figures, not the final tex file or auxiliary files, which I need to inspect for debugging purposes.

You can specify it in your YML header with:
output:
pdf_document:
keep_tex: true
---
More options on the rmarkdown site.

Related

Default knit directory via YAML header in RStudio?

Is there a YAML option, which sets knit directory for an Rmd file? I.e. does the same as illustrated in Fig.1.
The problem is that every time I copy files or share a project with other people (which usually have different default RStudio options than I do), the knit directory should be reset manually in each file.
I'm aware of the global option (Fig.2), but I need a more reproducible solution which works for individual Rmd files in both both RStudio notebook and knitting modes.
And, for instance, I know that there is an option such as:
editor_options:
chunk_output_type: console
Any ideas?
Fig. 1 Setting knit directory via menu commands.
Fig. 2 Global option to set default knit directory in RStudio.
You could try to specify the knit field in the front-matter of our .Rmd file. Example .Rmd that would knit into "some_dir" created in your home directory:
---
title: "Untitled"
output: html_document
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding, output_dir = "~/some_dir/")
})
---
# Some content
Some content

Multiple sets of Shared Options in R Markdown

Is it possible to have multiple sets of shared options for R Markdown?
This is my problem: I have a folder with a bunch of markdown files. The files can be divided into two groups:
html_document and
revealjs::revealjs_presentation.
I would like to factor out common YAML code from each of these groups. Now I know that I can create a _output.yaml file which would capture common YAML, but I essentially need to have two of these files, one for each of the output formats.
I saw the use of pandoc_args suggested here and I gave it a try as follows:
---
title: Document Type 1
output:
html_document:
pandoc_args: './common-html.yaml'
---
and
---
title: Document Type 2
output:
revealjs::revealjs_presentation:
pandoc_args: './common-reveal.yaml'
---
However using this setup the options from the included YAML files don't get processed.
Any other suggestions would be appreciated!
You can specify multiple output formats in the same _output.yaml file like this (just some example options):
html_document:
self_contained: false
revealjs::revealjs_presentation:
incremental: true
Then you have to render all output formats which cannot be done directly using the RStudio GUI. Instead you have to enter the following into the R console:
rmarkdown::render(input = "your.Rmd",
output_format = "all")
Ideally make sure that there's no output key in the YAML front matter of the .Rmd document itself. Otherwise the output options in _output.yaml file might get overridden. Unfortunately I couldn't find a comprehensive documentation of the exact behavior. Some of my observations so far:
Output options defined in the YAML front matter of the .Rmd document itself always override those specified in the shared options file _output.yaml.
Specifying an output format using the default option set (like pdf_document: default) in the YAML front matter of the .Rmd document itself completely overrides all options specified in _output.yaml. But if you don't explicitly specify the default options (like output: pdf_document; which is only possible for a single output format at once), the _output.yaml content is fully regarded.
If you have specified options for multiple output formats in _output.yaml, only the first one gets rendered when pressing the knit button in RStudio (even if you explicitly press knit to HTML/PDF/Word). You have to use rmarkdown::render(output_format = "all") to render the other formats, too.

rmarkdown: render to .pdf using existing .md files created for .html

Is that possible at all? At the moment I have to render my .Rmd files twice, once for an html and once for a pdf report. Each of them take about 50 mins. So if I can use the html .md files created after rendering, that would save me 50 mins.
You can keep the markdown output of knitr with keep_md: yes
---
output:
html_document:
keep_md: yes
---
Then, using pandoc, you can produce your pdf from this file; in a terminal (not a R console):
pandoc mydoc.md -o mydoc.pdf
You'll have to install pandoc if you haven't already, to use it without knitr.
You might loose a few things, though, because knitr adapts its md intermediary file to the final output.
It is possible to render multiple outputs, but not with the "knit" Button in RStudio. Write your desired output in the YAML header and then use output_format = "all" as argument in
rmarkdown::render(<your-rmd-file.rmd>, output_format ="all")
So the YAML header looks like:
title: "multiple outputs"
output:
pdf_document: default
html_document: default
Or any option you want to set for the different output formats.
If your .md document is already created, you can simply use:
library("rmarkdown")
render("mydoc.md", output_format = "pdf_document")
If you always need to generate both html and pdf, then render both at the same time using J_F's solution.
Alternatively, you can tell the compiler to keep the intermediate .md file. Then, you can easily compile that into other formats.
Go to:
1. RStudio > Open your .Rmd file
2. Click on the Gear (settings) drop down and choose Output Options
3. Choose Advanced
4. Check Keep markdown source file
5. Click OK
6. Knit to HTML
7. Open .md file in editor
8. In Gear (settings) drop down, select PDF as Output Format, AND select (No Preview)
9. In the Preview dropdown, select Preview PDF and a .pdf file should be created
As an alternative to steps 2-5, edit your file so that the header includes:
---
title: "blah blah"
output:
html_document:
keep_md: yes
---

R markdown - set options for LaTeX packages

If I set an output parameter in .Rmd file in following way:
output:
bookdown::pdf_book:
base_format: rticles::elsevier_article
keep_tex: true
in the produced .tex file, endfloat package will be used with option 'nomarkers'. In other words, following line will be included in produced .tex file:
\usepackage[nomarkers]{endfloat}
How can I set options for LaTeX packages used by the output?
In my example, I wish to set endfloat options to 'markers', so that mentioned line in .tex file will looks as follows:
\usepackage[markers]{endfloat}
The simple solution
header-includes:
- \usepackage[markers]{endfloat}
output:
bookdown::pdf_book:
base_format: rticles::elsevier_article
keep_tex: true
works not, because there are package conflicts. The other solution I tried and worked, was to download the package from GitHub https://github.com/rstudio/rticles and change the file template.tex to your needs. This file is located in /inst/rmarkdown/templates/elsevier_article/resources/. For me this worked and the .tex file has the desired output as you expect it.

Add beamer frame options in knitr/rmarkdown

I'm trying to add frame numbers to my Beamer presentation written in rmarkdown. However, I would like to suppress the numbers on the title page using the \begin{frame}[plain] option (from the second answer here: https://tex.stackexchange.com/questions/82794/removing-page-number-from-title-frame-without-changing-the-theme). However, when compiling from rmarkdown to tex, the \titlepage already creates a frame environment, so in effect I get a double frame and thus an error.
So when compiling this:
---
output:
beamer_presentation:
includes:
in_header: header.tex
---
\begin{frame}[plain]
\titlepage
\end{frame}
I get this in latex:
\begin{frame{
\begin{frame}
\titlepage
\end{frame}
\end{frame}
In the header.tex I have this:
\let\otp\titlepage
\renewcommand{\titlepage}{\otp\addtocounter{framenumber}{-1}}
So my workaround now is to just use a plain \maketitle in rmarkdown, then compile to .tex, add the [plain] option, then compile to pdf. However, I would like to avoid that intermediate step. Is this possible in rmarkdown?
rmarkdown uses pandoc to convert a Rmd file to a pdf via beamer/latex. pandoc uses templates to control how the conversion goes.
One way to deal with your problem is to :
Download the default beamer template rmarkdown uses and open it.
Change line 137 from this :
\frame{\titlepage}
To this :
\frame[plain]{\titlepage}
Add the path to your modified template in your Rmd file :
---
output:
beamer_presentation:
includes:
in_header: header.tex
template:/path/to/new/template.tex
---
Note that you need to specify the whole path, or store the template where pandoc can find it (~/.pandoc/templates on a linux machine)
Add {.plain} after the title as in:
----
# I'm the title {.plain}
Source: Pandoc User’s Guide

Resources