yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.4k stars 878 forks source link

Using fig.subcap and fig.align does not give expected result #1907

Open StephenGerry opened 4 years ago

StephenGerry commented 4 years ago

I am using fig.subcap to create sub-figures, which normally works really well. But when the figures do not fill the full width of the page I'd like to centre the combined image. However using fig.align='center' does not centre all of the subfigures properly.

Here is an example:

---
output:
  pdf_document:
    extra_dependencies: "subfig"
    keep_tex: true
---

```{r, fig.cap='Figure 1', fig.subcap=c('a','b','c'), fig.ncol=1, out.height='30%', fig.align='center'}

plot(1:10)
plot(cars, pch=19)
boxplot(Sepal.Width ~ Species, data=iris)


By filing an issue to this repo, I promise that

- [x] I have fully read the issue guide at https://yihui.org/issue/.
- [x] I have provided the necessary information about my issue.
    - If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    - If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('knitr')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('yihui/knitr')`.
    - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.
StephenGerry commented 4 years ago

Apologies, these are the chunk options I used {r, fig.cap='Figure 1', fig.subcap=c('a','b','c'), fig.ncol=1, out.height='30%', fig.align='center'}

cderv commented 4 years ago

I can reproduce the issue. I guess the Latex code generated is not quite right for this usage.

Do you know how to produce such vertically centered aligned figure in Latex using \subfloat ? If so it could help us know what to do, otherwise I'll look into it.

StephenGerry commented 4 years ago

Thanks very much Christophe. I am no Latex expert I'm afraid! But this link (https://tex.stackexchange.com/questions/457917/subfloat-vertical-alignment) implies that the problem is something to do with using '\newline' between the subfloats. Replacing 'newline' with the double backslash in the tex file does indeed seem to work.