Search code examples
latexr-markdown

rmarkdown figure does not appear on pdf


I create a set of slides using rmarkdown. I add a figure using latex includegraphics which works as expected. But, when I add a figure generate via R code, the 'latex' figure disappears.

Minimal code example

---
title: "TITLE"
author: "JS"
subtitle: SUBTITLE
output:
  beamer_presentation: 
    keep_tex: yes
  ioslides_presentation: default
fontsize: 14pt
---



# Example 

\begin{figure}[t]
  \centering
  \includegraphics[scale=0.25]{include_figure}
\end{figure}

---

# Add figure via R code

```{r, message = FALSE, warnings = FALSE, eval = TRUE, echo = TRUE}
plot(rnorm(100))

Any suggestions what I do wrong?


Solution

  • Update:

    This bug has been fixed in pandoc v3.2.1. The workaround below should no longer be necessary


    For some reason rmarkdown and/or pandoc thinks that adding

    \makeatletter
    \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
    \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
    \makeatother
    % Scale images if necessary, so that they will not overflow the page
    % margins by default, and it is still possible to overwrite the defaults
    % using explicit options in \includegraphics[width, height, ...]{}
    \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
    

    to the preamble would be a good idea... which it is evidently not.

    You can avoid the problem by specifying the height of the image instead of a scaling factor:

    ---
    title: "TITLE"
    author: "JS"
    subtitle: SUBTITLE
    output:
      beamer_presentation: 
        keep_tex: yes
      ioslides_presentation: default
    fontsize: 14pt
    ---
    
    
    
    # Example 
    
    \begin{figure}
      \includegraphics[height=4cm]{example-image-duck}
    \end{figure}
    
    ---
    
    # Add figure via R code
    
    ```{r, message = FALSE, warnings = FALSE, eval = TRUE, echo = TRUE}
    plot(rnorm(100))
    ```