rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.75k stars 1.27k forks source link

word_document2 counts sub-plots towards figure numbers #1014

Open jooyoungseo opened 3 years ago

jooyoungseo commented 3 years ago

When fig.show = "hold" is used for multiple plots, figure counting result is different between html_document2 and word_document2.

Word output adds each count per sub figure whereas html only counts main plot.

Reprex

Test Rmd File

---
output:
  bookdown::word_document2: default
  bookdown::html_document2: default
---

```{r, echo = FALSE, fig.cap = "First figure."}
plot(1:5, 1:5)
plot(1:5, 1:5)
cat("\n\n")
boxplot(dist ~ speed, data = cars)
plot(1:5, 1:5)

* Render the file:

``` r
rmarkdown::render("test.Rmd", output_format = "all")

Rendering Results

word_output html_output

Session Info

rmarkdown::pandoc_version()
#> [1] '2.10.1'
xfun::session_info("bookdown")
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19042)
#> 
#> Locale:
#>   LC_COLLATE=English_United States.1252 
#>   LC_CTYPE=English_United States.1252   
#>   LC_MONETARY=English_United States.1252
#>   LC_NUMERIC=C                          
#>   LC_TIME=English_United States.1252    
#> system code page: 65001
#> 
#> Package version:
#>   base64enc_0.1.3      bookdown_0.21.4      digest_0.6.27       
#>   evaluate_0.14        glue_1.4.2           graphics_4.0.3      
#>   grDevices_4.0.3      highr_0.8            htmltools_0.5.0.9002
#>   jsonlite_1.7.1       knitr_1.30.2         magrittr_1.5        
#>   markdown_1.1         methods_4.0.3        mime_0.9            
#>   rlang_0.4.8          rmarkdown_2.5.3      stats_4.0.3         
#>   stringi_1.5.3        stringr_1.4.0        tinytex_0.27.1      
#>   tools_4.0.3          utils_4.0.3          xfun_0.19.3         
#>   yaml_2.2.1

Created on 2020-11-08 by the reprex package (v0.3.0.9001)

cderv commented 3 years ago

Hi believe this is a side effect of several changes in knitr, and it is a know issue with knitr: https://github.com/yihui/knitr/pull/1760

(When I reproduce you example, there is no caption for the two middle figures)

At first, fig.show='hold' was not supported for office document (See https://github.com/rstudio/bookdown/issues/249#issuecomment-262358329) but this seems to have changed (in https://github.com/yihui/knitr/pull/1756). And there is still the issue mentioned above.

@atusy do you agree ? Do you think this is linked to what you worked on in the knitr package ?

I think there is also an issue with the counter in bookdown if two plots have the same caption label.

atusy commented 3 years ago

Yes, I agree.

However, if we expect a single caption for two middle figures, I don't know how. We will need some ooxml trick because figures are in a single line on the intermediate markdown, and Pandoc considers they are not captioned.

![cap1](fig1.png)![cap1](fig.png)
cderv commented 3 years ago

Yes I am not sure we can easily have the same output in HTML and Word. In docx, I am not even sure this is allowed to have one caption for several figure (without tricks) - It seems to me the built-in caption feature is per image. But maybe ooxml could help here as you said... This will require some thinking. 🤔

I wonder if officer or officedown handle this.

atusy commented 3 years ago

Another possibility is using custom-style with Divs and Spans. Captioned figures inherit the Captioned Figure style, and their captions inherit the Image Caption style. You can see how the result would be by:

::: {custom-style="Captioned Figure"}
```{r, fig.show='hold'}
plot(1)
plot(2)

:::

::: {custom-style="Image Caption"} Caption :::



However, I am not sure if it is worth doing...
Personally, I would combine sub-figures by using **patchwork** package or **cowplot** package because

- it is more explicit that they are a set of figures, not individual ones.
- layout becomes more flexible.
- tagging is easyy (e.g., `(a)`, `(b)`, ...)
cderv commented 3 years ago

Personally, I would combine sub-figures by using patchwork package or cowplot package because

yes that would be a great advice IMO.