How to suppress automatic figure numbering in Rmarkdown / pandoc - r

I have the following Rmarkdown (.Rmd) document where I call existing .png images and create a .pdf with captions. By default, pandoc? is automatically adding "Figure #." before the caption for each picture. I can see how this would be the normal thing to do, but in my case I would like to define this. I have found variations on this topic but don't seem to find a solution. Below is an example of how my .Rmd file looks:
---
title: "TITLE"
author: "ME"
date: "`r Sys.Date()`"
output:
pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
![Caption for figure 1](figures/plot1.png)
\newpage
![Caption for figure 2](figures/plot2.png)

You could use the caption-package
Create a .tex-file that you specify the following in, this below remove the entire label and you are free to hardcode the labels.
\usepackage{caption}
\captionsetup[figure]{labelformat=empty}
Then your .rmd should look like this:
---
title: "TITLE"
author: "ME"
date: "`r Sys.Date()`"
output:
pdf_document:
includes:
in_header: YourName.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
![Caption for figure 1](figures/plot1.png)
\newpage
![Caption for figure 2](figures/plot2.png)
Simplified: As suggested in the comments, we can achieve this within our .Rmd file, as shown below.
---
title: "TITLE"
author: "ME"
date: "`r Sys.Date()`"
output:
pdf_document:
header-includes:
- \usepackage{caption}
- \captionsetup[figure]{labelformat=empty}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
![Caption for figure 1](figures/plot1.png)
\newpage
![Caption for figure 2](figures/plot2.png)

Related

How to include a multipage PDF in RMarkdown with dynamic paths?

I was able to figure out how to include a multi-page PDF document in my RMarkdown output using "\includepdf", but I need to be able to change the path to different PDFs based on conditions in the data. I'd like to be able to call the PDF file path based on an object I set in a chunk beforehand (e.g. "test"). Any ideas?
Thanks!
---
title: "Personnel Reports"
output:
pdf_document
header-includes:
- \usepackage{pdfpages}
---
This works:
\includepdf[pages=-,pagecommand={}]{Person1_report.pdf}
```{r global_options, include=FALSE}
test <- "Person2_report.pdf"
```'
This doesn't work:
\includepdf[pages=-,pagecommand={}]{test}
---
title: "Personnel Reports"
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{pdfpages}
---
```{r global_options, include=FALSE}
test <- "example-image-duck.pdf"
```
\includepdf[pages=-,pagecommand={}]{`r test`}

Unable to include multiple flextable in RMarkdown PDF minipage

I am trying to use the R {flextable} package to create a PDF. It doesn't like {multicol} (SO: Flextable seems to be incompatible with multicol LaTex package) due to longtables not being allowed in multicol. So I have instead used {minipage}.
When trying with a single flextable the document succesfully knits:
---
title: "Untitled"
date: "30/08/2021"
output:
pdf_document:
latex_engine: lualatex
geometry: margin=1.5cm
---
\begin{minipage}[t]{0.5\linewidth}
```{r iris}
flextable::flextable(iris[1:5, ])
```
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
Content on the right hand side
\end{minipage}
However, when adding in a second table, it doesn't convert correctly to the .tex file:
---
title: "Untitled"
date: "30/08/2021"
output:
pdf_document:
latex_engine: lualatex
geometry: margin=1.5cm
---
\begin{minipage}[t]{0.5\linewidth}
```{r iris}
flextable::flextable(iris[1:5, ])
```
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
```{r iris2}
flextable::flextable(iris[1:5, ])
```
\end{minipage}
The .tex content looks fine for the first minipage, but appears as this in the second minipage:
\textbackslash begin\{minipage\}{[}t{]}\{0.5\linewidth\}
Is there something I need to add to the Rmd file to prevent this from happening? I have tried using print and cat and causes the same output/error.
The problem seems to be that Pandoc does not understand that the second \begin{minipage}[t]{0.5\linewidth} is supposed to be verbatim LaTeX as well.
As a workaround, you can mark this line as raw LaTeX:
```{=latex}
\begin{minipage}[t]{0.5\linewidth}
```
The same applies to the closing \end{minipage}.
However, this generates a paragraph break between the two minipages such that they are not side-by-side anymore. The only remedy I found thus far is to use the raw LaTeX syntax for the first minipage, too:
---
title: "2 Flextables"
output:
pdf_document:
latex_engine: lualatex
keep_tex: yes
geometry: margin=1.5cm
---
```{=latex}
\begin{minipage}[t]{0.5\linewidth}
```
```{r iris}
flextable::flextable(iris[1:5, 2:4])
```
```{=latex}
\end{minipage}%
\begin{minipage}[t]{0.5\linewidth}
```
```{r iris2}
flextable::flextable(iris[1:5, 2:4])
```
```{=latex}
\end{minipage}
```
Output:

Specify custom powerpoint theme in R markdown

I am using R markdown to create a presentation. Here's a sample code:
---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: powerpoint_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with Plot
```{r pressure, fig.width=30, fig.asp=0.618,
out.width="200%"}
plot(pressure)
```
I want to apply a custom template that is specified by my organisation. How do I specify the custom theme? I read online that this can be specified by:
---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: powerpoint_presentation:
reference_doc: my_theme.pptx
---
But how does this work? Where do I have to store the my_theme.pptx file?
I hope you don't need this answer now, but just in reference for all of the guyz who were seeking for the answer like me.
Go to >> R\win-library\version e.g. 4.0\officer >>> paste your custom template on this path.
It will work

R Markdown : \#ref() not working

I'm having a lot of trouble getting basic references to work in R Markdown. To reduce complexity from my original project, I've decided to use the bookdown example code, but I'm experiencing the same problem. Here's a link to the intro exmample code: https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd
When I use Knitr to HTML or PDF the file is being generated fine but the references are not working, instead the file will just containt "#ref(example)". Here is an image to show better the output (my emphasis added in red):
Direct link to image: https://i.imgur.com/2yxB5h3.png
Here is a minimal example:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```
With the output appearing as such:
https://i.imgur.com/J3UECqn.png
If you want make use of the bookdown extensions in a normal rmarkdown document you can use bookdown::html_document2 and bookdown::pdf_document2 instead of rmarkdown::html_document and rmarkdown::pdf_document. Example:
---
title: "Minimal"
output:
bookdown::html_document2:
fig_caption: yes
bookdown::pdf_document2:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```
Looks like I was getting my syntax confused by reading the bookdown guide while using just R markdown. Thanks to Ralf for pointing me in the this direction. The correct minimal code would be like so:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \ref{fig:minGraph}
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

Rmarkdown Beamer presentation, option clash clash for xcolor

I am trying to build a beamer presentation using rmarkdown. In my presentation I want to include tables using the kable, and kableExtra packages. I am having issues with this because one of the LaTex packages that kableExtra requires is already loaded by the beamer presentation with different options. This is the error message that I receive.
! LaTeX Error: Option clash for package xcolor.
I have been searching for a fix for this but have not had any luck. I have found solutions on the LaTex pages, here and here, but I do not know LaTex and I have not figured out how to apply these solutions in the rmarkdown arena. I have tried looking at the Latex templates in rmarkdown, but I do not understand it well enough to try and implement these solutions.
Any thoughts or solutions would be much appreciated. Here is just a quick sample of the .Rmd that gives the error.
---
title: "Untitled"
author: "Author"
date: "April 28, 2018"
output:
beamer_presentation:
keep_tex: true
header-includes:
- \usepackage[table]{xcolor}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:6]
```
## Slide with R Output
```{r cars, echo = TRUE}
kable(dt, format = "latex")
```
## Slide with Plot
```{r pressure}
plot(pressure)
```
The linked answer on the TeX stackexchange suggests adding table to the class options for the document e.g. \documentclass[a4paper,table]{article}. In order to do this in RMarkdown, you can use a classoption: line in your YAML header:
---
title: "Untitled"
author: "Author"
date: "April 28, 2018"
classoption: table
output:
beamer_presentation:
keep_tex: true
---

Resources