rstudio / rticles

LaTeX Journal Article Templates for R Markdown
https://pkgs.rstudio.com/rticles/
1.46k stars 516 forks source link

`rticles::joss_article` removes hyperlinks from figure captions if `out.width` is used for chunk options #379

Closed IndrajeetPatil closed 3 years ago

IndrajeetPatil commented 3 years ago

I noticed this behavior while using rticles template for a JOSS paper.

Minimal YAML for the document:

---
title: 'Example paper title'
authors:
  - name: Jane Doe
output: rticles::joss_article
link-citations: yes
---

rmarkdown code to generate a figure with a caption:

Here is an empty plot.

```{r empty, fig.cap="To read more: https://en.wikipedia.org/wiki/Ggplot2"}
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point()

# with `out.width`

````md
```{r, warning=FALSE, message=FALSE, echo=FALSE}
knitr::opts_chunk$set(out.width = "100%")

<img width="741" alt="Screenshot 2021-04-01 at 19 47 26" src="https://user-images.githubusercontent.com/11330453/113335736-a4a0b280-9325-11eb-9589-b9b01a0167f1.png">

# without `out.width`

<img width="734" alt="Screenshot 2021-04-01 at 19 47 57" src="https://user-images.githubusercontent.com/11330453/113335751-ab2f2a00-9325-11eb-9be3-c0a9f557c9a1.png">

# session info

``` r
library(rticles)
options(width = 200)
sessioninfo::session_info(include_base = TRUE)
#> ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.5 (2021-03-31)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2021-04-01                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  ! package     * version date       lib source                            
#>    assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)                    
#>    backports     1.2.1   2020-12-09 [1] CRAN (R 4.0.3)                    
#>    base        * 4.0.5   2021-04-01 [?] local                             
#>    cli           2.3.1   2021-02-23 [1] CRAN (R 4.0.4)                    
#>  P compiler      4.0.5   2021-04-01 [2] local                             
#>    crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.3)                    
#>  P datasets    * 4.0.5   2021-04-01 [2] local                             
#>    debugme       1.1.0   2017-10-22 [1] CRAN (R 4.0.2)                    
#>    digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.2)                    
#>    ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)                    
#>    evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.1)                    
#>    fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.3)                    
#>    fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)                    
#>    glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)                    
#>  P graphics    * 4.0.5   2021-04-01 [2] local                             
#>  P grDevices   * 4.0.5   2021-04-01 [2] local                             
#>    highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)                    
#>    htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)                    
#>    knitr         1.31    2021-01-27 [1] CRAN (R 4.0.3)                    
#>    lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.3)                    
#>    magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)                    
#>  P methods     * 4.0.5   2021-04-01 [2] local                             
#>    pillar        1.5.1   2021-03-05 [1] CRAN (R 4.0.4)                    
#>    pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.2)                    
#>    purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.2)                    
#>    reprex        1.0.0   2021-01-27 [1] CRAN (R 4.0.3)                    
#>    rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)                    
#>    rmarkdown     2.7.4   2021-03-26 [1] Github (rstudio/rmarkdown@a11240d)
#>    rticles     * 0.19    2021-03-04 [1] CRAN (R 4.0.2)                    
#>    sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)                    
#>  P stats       * 4.0.5   2021-04-01 [2] local                             
#>    stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)                    
#>    stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)                    
#>    styler        1.4.1   2021-03-30 [1] CRAN (R 4.0.4)                    
#>    tibble        3.1.0   2021-02-25 [1] CRAN (R 4.0.4)                    
#>  P tools         4.0.5   2021-04-01 [2] local                             
#>    utf8          1.2.1   2021-03-12 [1] CRAN (R 4.0.4)                    
#>  P utils       * 4.0.5   2021-04-01 [2] local                             
#>    vctrs         0.3.7   2021-03-29 [1] CRAN (R 4.0.4)                    
#>    withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.3)                    
#>    xfun          0.22    2021-03-11 [1] CRAN (R 4.0.4)                    
#>    yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)                    
#> 
#> [1] /Users/patil/Library/R/4.0/library
#> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
#> 
#>  P ── Loaded and on-disk path mismatch.

Created on 2021-04-01 by the reprex package (v1.0.0)

cderv commented 3 years ago

Yes this is a currently expected behavior as fig.cap option can't really take Markdown syntax. In fact, it depends on the output format and type of image inclusion that will be produce.

However, this is kind of hidden and advanced but you should see the difference in the intermediary md file (set keep_md: TRUE in your YAML)

Without out.width, Markdown syntax will be used to insert the plot. This means caption will be processed by Pandoc to be converted to LaTeX. Code generated by knitr in .md file for Pandoc to process:

![To read more: https://en.wikipedia.org/wiki/Ggplot2](Untitled_files/figure-latex/empty-1.pdf) 

When you set out.width, knitr will produce a figure environment and caption will be written as is in LaTeX - it won't be processed as Markdown by Pandoc. Code generated by knitr in .md file for Pandoc to process:

\begin{figure}
\includegraphics[width=1\linewidth]{Untitled_files/figure-latex/empty-1} \caption{To read more: https://en.wikipedia.org/wiki/Ggplot2}\label{fig:empty}
\end{figure}

So you see the difference.

If you want url in LaTeX, you should use the corresponding command (and escaping because R)

```{r empty, fig.cap="To read more: \\url{https://en.wikipedia.org/wiki/Ggplot2}", out.width = "100%"}
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point()


This work well when you produce only LaTeX output but don't if you want also HTML from the same code. That is why there is a trick in **bookdown** to allow using `fig.cap` with Text Reference: https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#text-references

So this is not a **rticles** specificity but a current **knitr** behavior because Pandoc Markdown can't by default handle all latex case. 

We may revisit that in the future if Pandoc Figures syntax evolve. 
IndrajeetPatil commented 3 years ago

Thanks a ton for your kind and detailed response, Christophe! :)

If you want url in LaTeX, you should use the corresponding command (and escaping because R)

This solved the issue for me 🎉

I will let you decide if you want to close this or keep it open.

cderv commented 3 years ago

I don't think there is anything we can do in rticles sides directly. It should be in other packages.

however, we currently have some solutions so I guess it is fine.

Thanks!

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.