5

I'm trying to add a figure with sub captions in a R bookdown project as follows

---
output:
  pdf_document:
    extra_dependencies: "subfig"
---
     
```{r  echo=F, out.width = "50%",fig.showtext=TRUE,fig.show='hold',fig.cap="TITULO"}
par(mfrow=c(1,2))
knitr::include_graphics("ts_mult.png") 
knitr::include_graphics("ts_ad.png") 
```

and I get the following result

enter image description here

then I tried to add the subcaptions ("imagen a", "imagen b") as follows

```{r  echo=F, out.width = "50%",fig.showtext=TRUE,fig.show='hold',fig.cap="TITULO",fig.subcap=c("imagen a", "imagen b")}
par(mfrow=c(1,2))
knitr::include_graphics("ts_mult.png") 
knitr::include_graphics("ts_ad.png") 
```

but that didn't work and it throws the following error

! Undefined control sequence.
<recently read> \subfloat 

Error: LaTeX failed to compile Tesis_AE.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.

How can I correctly add the subcaptions?

Carlos Luis Rivera
  • 3,108
  • 18
  • 45
User 2014
  • 183
  • 6

1 Answers1

7

You just need to add fig.subcap=c("A subtitle","Another subtitle") in the chunk.

In my environment, at least, fig.showtext=TRUE is not necessary and rather this causes an error: Error in loadNamespace(name) : there is no package called 'showtext' Calls: <Anonymous> ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart Execution halted. That's why I excluded the setting in my answer.

---
output:
  pdf_document:
    extra_dependencies: "subfig"
    keep_tex: yes
---
     
```{r echo=F, out.width = "50%",fig.show='hold',fig.cap="TITULO", fig.subcap=c("Subtitulo1","Subtitulo2")}
par(mfrow=c(1,2))
knitr::include_graphics("your-path-to/image1.png") 
knitr::include_graphics("your-path-to/image2.png") 
```

Session Info

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=Japanese_Japan.932  LC_CTYPE=Japanese_Japan.932    LC_MONETARY=Japanese_Japan.932
[4] LC_NUMERIC=C                   LC_TIME=Japanese_Japan.932    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2   

> rmarkdown::pandoc_version()
[1] ‘2.7.3’

enter image description here

Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • that's just what I tried but when trying to run the code it throws me the error that I describe in my question *(! Undefined control sequence. \subfloat )* – User 2014 Aug 31 '20 at 07:05
  • I don't know why I have this error if apparently the code is correct and it has worked for you. – User 2014 Aug 31 '20 at 07:13
  • 2
    @User2014 Could you retry with my updated answer (`keep_tex: yes`) and add your tex file to the original question? We need to see whether `subfig` package is really loaded. It's also good to tell your version of R and Pandoc (to do so, run `sessionInfo(); rmarkdown::pandoc_version()` first). – Carlos Luis Rivera Aug 31 '20 at 08:06
  • I already ran in code with the suggested modification but I still get the same error. `rmarkdown::pandoc_version() [1] ‘2.7.3’` – User 2014 Sep 01 '20 at 06:02
  • `> sessionInfo() R version 4.0.2 (2020-06-22) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363) Matrix products: default attached base packages: [1] stats graphics grDevices utils datasets methods [7] base loaded via a namespace (and not attached): [1] compiler_4.0.2 bookdown_0.20 htmltools_0.5.0 [4] tools_4.0.2 yaml_2.2.1 rmarkdown_2.3 [7] knitr_1.29 xfun_0.15 digest_0.6.25 [10] rlang_0.4.7 evaluate_0.14` – User 2014 Sep 01 '20 at 06:08
  • 3
    Ummm, it's weird since my environment is same as yours and I cannot replicate your error. Did you find `\usepackage{subfig}` in your tex file? – Carlos Luis Rivera Sep 01 '20 at 09:57
  • 2
    The package was not in the main tex file, however it was solved, I had to add \usepackage{subfig} in the preamble.tex file, thanks for your suggestions. – User 2014 Sep 03 '20 at 02:24
  • 1
    Happy to hear your success! If you think this answer is worthy, you can click the accept mark! – Carlos Luis Rivera Sep 04 '20 at 22:57