Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using both in_header and header_includes in Rmarkdown and knitr

I have a long .txt file that contains packages and settings that I want to used in my Rmarkdown file. I also want to add statements to the header, based on the output of R calculations. In this particular case I want to add a titlegraphic that is located in another directory, like this:

working directory
|--- reports
|----| my_report.Rmd
|--- www
|----| image.png

So the header of the Rmarkdown file would look like:

output:
  beamer_presentation:
    keep_tex: true
    includes:
      in_header: header.txt
header-includes:
- \titlegraphic{\includegraphics[width=0.3\paperwidth]{`r paste0("dirname(getwd()),"image.png")`}}

If only one of the statements is included (in_header or header-includes), they work fine. But when I use them both, the content of header-includes seems to be overwritten. An example is given in the files below, where upon inspecting the resulting .tex file, I find that \usepackage{fancyhdr} is in the header, but there is no mention of the `\titlegraphic' expression.


header.txt

\usepackage{fancyhdr}

example.Rmd

title: Example 1
output:
  beamer_presentation:
    keep_tex: true
    includes:
      in_header: header.txt
header-includes:
      \titlegraphic{\includegraphics[width=0.3\paperwidth]{`r paste0("just_an_example_","logo.png")`}}
---

### This is a test
like image 235
Florian Avatar asked Jul 25 '17 12:07

Florian


People also ask

How can you compile the R Markdown document using knitr package?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.

What is knitr in rmarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.

Does rmarkdown use pandoc?

A recent version of Pandoc (>= 1.12. 3) is required to use the rmarkdown package. RStudio also automatically includes this so you do not need to download Pandoc if you plan to use rmarkdown from the RStudio IDE.

Can you use LaTeX packages in R markdown?

If you would like to create PDF documents from R Markdown, you will need to have a LaTeX distribution installed. Although there are several traditional options including MiKTeX, MacTeX, and TeX Live, we recommend that R Markdown users install TinyTeX.


1 Answers

I think what you could do is to put everything in header-includes:

---
title: Example 1
output:
  beamer_presentation:
    keep_tex: true
header-includes:
  - \titlegraphic{\includegraphics[width=0.3\paperwidth]{`r paste0("just_an_example_","logo.png")`}}
  - \input{header.txt}
---

Does it work? I can't fully reproduce your example.

like image 68
F. Privé Avatar answered Sep 25 '22 12:09

F. Privé