Multiple figures side-by-side not closing TeX's figure block with plotly content

Viewed 54

I would like to align several plotly figures in two columns with only one caption in an Rmd producing PDF output. I have tried to use plotly's subplot within the code block which was not successful, as I want each sub-figure to have its own legend. I am now trying to work with the chunk options fig.show='hold' and fig.ncol=2 and it appears that the tex-File that is knitted does not close the corresponding figure environment, causing the resulting PDF to contain a literal \begin{figure}. This problem apparently does not occur with base R figures, as per the reproducible example below.

Is there any chance that anyone here has had a similar issue before or some other remedies to prevent this from happening?

Reproducible example

Rmd-file:

---
title: "Untitled"
author: "Bob the Builder"
date: "11/15/2021"
output: 
  pdf_document:
    latex_engine: xelatex
    keep_tex: true
---

## Base R
```{r echo=FALSE, message=FALSE, results=FALSE, fig.show='hold', fig.ncol=2, fig.cap = "Plot caption goes here.", out.width="0.46\\textwidth"}
plot(iris$Sepal.Length, iris$Petal.Length)
plot(cars)
```

## Plotly
```{r echo=FALSE, message=FALSE, results=FALSE, fig.show='hold', fig.ncol=2, fig.cap = "Plot caption goes here.", out.width="0.46\\textwidth"}
plotly::plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, type = 'scatter', mode = 'markers')
plotly::plot_ly(cars, x = ~speed, y = ~dist, type = 'scatter', mode = 'markers')
```

TeX file contains the following:

\hypertarget{base-r}{%
\subsection{Base R}\label{base-r}}

\begin{figure}
\includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-1-1} \includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-1-2} \caption{Plot caption goes here.}\label{fig:unnamed-chunk-1}
\end{figure}

\hypertarget{plotly}{%
\subsection{Plotly}\label{plotly}}

\textbackslash begin\{figure\}
\includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-2-1}
\includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-2-2}

with a missing \end{figure} and \caption in the plotly-Section.

0 Answers
Related