简体   繁体   中英

Wrapping figures generated in a knitr chunk when knitting to pdf

In a .Rmd document, I am generating multiple related figures (from a list) in a knitr chunk.

When knitting to html, those figures are wrapped properly and all visible.

When knitting to pdf, the figures are all one after the other and only the first two are visible (and half of the third). Here is some code that reproduces the issue:

---
title: "Example figure wrapping problem"
output:
    pdf_document:
        keep_tex: true
classoption:
    landscape
---

# SK-N-SH plex panel {.tabset .tabset-fade}

```{r, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=9, fig.show="hold", hightligh=TRUE, warnings=TRUE, error=FALSE, cache=FALSE, echo=FALSE, dpi=100)
```

```{r}
for (ii in 1:6) {
    plot(1:3, 1:3, main=ii)
}
```

I figured that the problem comes from the generation of the .tex file, which contains this line:

\includegraphics{figure/unnamed-chunk-2-1.png}\includegraphics{figure/unnamed-chunk-2-2.png}\includegraphics{figure/unnamed-chunk-2-3.png}\includegraphics{figure/unnamed-chunk-2-4.png}\includegraphics{figure/unnamed-chunk-2-5.png}\includegraphics{figure/unnamed-chunk-2-6.png}

Adding line breaks every two includegraphics solves the problems:

\includegraphics{figure/unnamed-chunk-2-1.png}\includegraphics{figure/unnamed-chunk-2-2.png}
\includegraphics{figure/unnamed-chunk-2-3.png}\includegraphics{figure/unnamed-chunk-2-4.png}
\includegraphics{figure/unnamed-chunk-2-5.png}\includegraphics{figure/unnamed-chunk-2-6.png}

However it is obviously not practical as there are many more figures. I could also run sed 's/}\\(\\\\includegraphics\\)/}\\r\\1/g' on the file but it feels like uselessly complicating the compiling process.

Is there a native knitr or rmarkdown way to solve my problem.

If you specify fig.align="center" and fig.show="asis" in the code chunk it seems to work. For example

---
title: "Example figure wrapping problem"
output:
    pdf_document:
        keep_tex: true
classoption:
    landscape
---

# SK-N-SH plex panel {.tabset .tabset-fade}

```{r, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=9, fig.show="hold", hightligh=TRUE, warnings=TRUE, error=FALSE, cache=FALSE, echo=FALSE, dpi=100)
```

```{r fig.align="center",fig.show="asis"}
for (ii in 1:6) {
    plot(1:3, 1:3, main=ii)
}
```

looks fine. It generates LaTeX code

\begin{center}\includegraphics{Untitled_files/figure-latex/unnamed-chunk-2-1} \end{center}

\begin{center}\includegraphics{Untitled_files/figure-latex/unnamed-chunk-2-2} \end{center}

etc.

which breaks up the figures. You can use fig.align="right" or fig.align="left" instead, but it's essential that you don't use fig.show="hold" , or all the figures are wrapped together, and you return to the original problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM