Ashley Baldry
Ashley Baldry

Reputation: 869

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.

Upvotes: 1

Views: 795

Answers (1)

CL.
CL.

Reputation: 14957

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: Output

Upvotes: 1

Related Questions