Unable to include multiple flextable in RMarkdown PDF minipage - r

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:

Related

How to plot an R graph within latex code in Rmarkdown pdf document

Trivial but annoying issue with Rmarkdown. I am trying to display landscape and portrait pages within the same pdf document from Rmarkdown.
I searched different solutions and decided to stick to the latex option (R chunk inside LaTeX in an rmarkdown document). However, even with that option I cannot manage to put R code within latex commands.
---
title: ""
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document
---
\newpage
\blandscape
```{r}
plot(1,1)
```
\elandscape
\newpage
More portrait
```{r}
summary(cars)
```
I got an error:
output file: hello.knit.md
! LaTeX Error: \begin{landscape} on input line 113 ended by \end{document}.
Error: LaTeX failed to compile hello.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See hello.log for more info.
Execution halted
while the following code is working:
---
title: ""
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document
---
\newpage
\blandscape
```{r}
1+1
```
\elandscape
\newpage
More portrait
```{r}
summary(cars)
```
I have no idea why :D
Does someone have one ?
Instead of defining new commands, you can directly use the underlying commands of an environment:
---
title: ""
header-includes:
- \usepackage{lscape}
output: pdf_document
---
\newpage
\landscape
```{r}
plot(1,1)
```
\endlandscape
\newpage
More portrait
```{r}
summary(cars)
```

Extracting a list of LaTeX figure and caption labels from RMarkdown to copy into Overleaf

I am working with coauthors. We want to produce all figures and tables (including an appendix) in a single RMarkdown document but write the paper jointly in Overleaf (excellent for simultaneous editing, which we don't need for the statistical code).
Here is an example Rmd which knits to a pdf. (The pdf will be appended manually to the main paper.)
---
title: "Nice document"
output: pdf_document
date: '2022-08-12'
---
```{r}
library(kableExtra)
```
```{r pressure, echo=FALSE, fig.cap="\\label{fig:pressure} Nice caption."}
plot(pressure)
```
```{r echo=FALSE, fig.cap="\\label{fig:diffpressure} Better caption."}
kable(head(mtcars), longtable = T, booktabs = T, caption = "Cool table", label = "tab:carssummary")
```
\appendix
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
\renewcommand{\thetable}{S\arabic{table}}
```{r echo=FALSE, fig.cap="\\label{fig:diffpressure} Better caption."}
plot(pressure/3.5)
```
I want a extract a character vector from the Rmd that is the following:
\begin{figure}
\caption{empty}
\label{fig:pressure}
\end{figure}
\begin{table}
\caption{empty}
\label{tab:carssummary}
\end{table}
\appendix
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
\renewcommand{\thetable}{S\arabic{table}}
\begin{figure}
\caption{empty}
\label{fig:pressure2}
\end{figure}
I will then copy and paste this to the bottom of the Overleaf doc, enabling us to do the automated cross referencing with minimal hassle (and easy updating if and when the analysis output changes).
How can I extract that LaTeX code from the Rmd?
Assuming your markdown document is called test.rmd and you included the keep_tex: true option to the header
---
title: "Nice document"
output:
pdf_document:
keep_tex: true
date: '2022-08-12'
---
You can upload the test.aux file to overleaf and then include the cross-refs in your overleaf document with the xr-hyper package:
\documentclass{article}
\usepackage{xr-hyper}
\externaldocument{test}
\usepackage{hyperref}
\begin{document}
some cross-ref: \ref{fig:diffpressure}
\end{document}

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`}

In Rmd documents rendered to pdf, preventing extra line breaks in R chunks?

The following code results in an extra line break between rnorm(1) and the return value. How do I remove this extra line?
---
title: "Lecture2"
output: pdf_document
---
```{r}
rnorm(1)
```
I have miktex installed and am using knitr.
Maybe you should evaluate as inline code (i.e. `r norm(1)`).
This approach works only when highlight=FALSE and has the unfortunate side effect of removing the ability to create line breaks with new paragraphs...
---
title: "Untitled"
output:
beamer_presentation:
keep_tex: true
includes:
in_header: header.tex
---
```{r, highlight=FALSE}
rnorm(1)
```
in header.tex
%code from http://tex.stackexchange.com/questions/43331/control-vertical-space-before-and-after-verbatim-environment
\usepackage{etoolbox}
\makeatletter
\preto{\#verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother
%https://www.sharelatex.com/learn/Paragraph_formatting
\setlength{\parskip}{0 em}

Figure name in caption using RMarkdown

I am writing my first .rmd report using RMarkdown and then I knit it to pdf file. As I write it in my national language, I would like to have figures captions in my language too. The problem is that the default caption format is "Figure 1: My caption". I would like to change this generative "Figure 1:" part but I couldn't find how. Any help will be appreciated.
Try specifying the language in the YAML header of an rmarkdown document (documentation link):
---
title: "Crop Analysis Q3 2013"
output: pdf_document
fontsize: 11pt
geometry: margin=1in
lang: german
---
The language code will be processed by the babel LaTeX package (at least in the case of pdflatex).
I found out that it is possible to do using LaTeX:
---
title: "My raport"
output:
pdf_document:
fig_caption: yes
header-includes:
- \usepackage{caption}
---
\def\figurename{MyFigureName}
Still: is there any simpler way?
It should work like this:
---
output:
pdf_document:
fig_caption: true
---
```{r figWithCaption, echo=F, fig.cap="My Figure Caption"}
plot(cars) # your figure
```

Resources