rstudio / blogdown

Create Blogs and Websites with R Markdown
https://pkgs.rstudio.com/blogdown/
1.74k stars 331 forks source link

Citation in Figure Caption Does Not Work #703

Closed amarakon closed 2 years ago

amarakon commented 2 years ago

I have this code block:

```{r pie, fig.cap = "Operating system market share [@desktop-os-marketshare]", dev = 'svglite'}
slices <- c(74.83, 15.37, 4.93, 2.48, 2.38)
lbls <- c("Windows", "macOS", "Unknown", "Linux", "Chrome OS")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices, labels = lbls, col = rainbow(length(lbls)))

Here is the `desktop-os-marketshare` from my bibliography file:

```latex
@article{desktop-os-marketshare,
    title = "Desktop Operating System Market Share Worldwide",
    author = "Statcounter",
    year = "2022",
    url = "https://gs.statcounter.com/os-market-share/desktop/worldwide"
}

The citation [@desktop-os-marketshare] works outside of figure captions. Here is what it looks like in a figure caption.

Here is the output of xfun::session_info("blogdown"):

Linking to librsvg 2.54.0
> xfun::session_info("blogdown")
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Gentoo Linux

Locale:
  LC_CTYPE=en_CA.utf8       LC_NUMERIC=C
  LC_TIME=en_CA.utf8        LC_COLLATE=en_CA.utf8
  LC_MONETARY=en_CA.utf8    LC_MESSAGES=C
  LC_PAPER=en_CA.utf8       LC_NAME=C
  LC_ADDRESS=C              LC_TELEPHONE=C
  LC_MEASUREMENT=en_CA.utf8 LC_IDENTIFICATION=C

Package version:
  base64enc_0.1.3  blogdown_1.9     bookdown_0.26    bslib_0.3.1
  digest_0.6.29    evaluate_0.15    fastmap_1.1.0    fs_1.5.2
  glue_1.6.2       graphics_4.1.2   grDevices_4.1.2  highr_0.9
  htmltools_0.5.2  httpuv_1.6.5     jquerylib_0.1.4  jsonlite_1.8.0
  knitr_1.39       later_1.3.0      magrittr_2.0.3   methods_4.1.2
  mime_0.12        promises_1.2.0.1 R6_2.5.1         rappdirs_0.3.3
  Rcpp_1.0.8.3     rlang_1.0.2      rmarkdown_2.14   sass_0.4.1
  servr_0.24       stats_4.1.2      stringi_1.7.6    stringr_1.4.0
  tinytex_0.38     tools_4.1.2      utils_4.1.2      xfun_0.30
  yaml_2.3.5

Hugo version: 0.97.1
>
>

Here is the output of blogdown::check_site():

> blogdown::check_site()
― Running a series of automated checks for your blogdown website project...
○ A successful check looks like this.
● [TODO] A check that needs your attention looks like this.
| Let's check out your blogdown site!
― Checking config.toml
| Checking "baseURL" setting for Hugo...
○ Found baseURL = "https://amarakon.github.io/amarakon.com/"; nothing to do here!
| Checking "ignoreFiles" setting for Hugo...
● [TODO] Set "ignoreFiles" to ["\\.Rmd$", "\\.Rmarkdown$", "_cache$"]
| Checking setting for Hugo's Markdown renderer...
○ All set! Found the "unsafe" setting for goldmark.
― Check complete: config.toml

― Checking .gitignore
● [TODO] .gitignore was not found. You may want to add this.
― Checking Hugo
| Checking Hugo version...
○ Found Hugo. You are using Hugo 0.97.1.
| Checking .Rprofile for Hugo version used by blogdown...
○ blogdown is using Hugo 0.97.1 to build site locally.
― Check complete: Hugo

― Checking netlify.toml...
● [TODO] netlify.toml was not found. Use blogdown::config_netlify() to create file.
― Checking content files
| Checking for validity of YAML metadata in posts...
○ All YAML metadata appears to be syntactically valid.
| Checking for previewed content that will not be published...
● [TODO] Found 2 files with a future publish date:

  content/blog/2022-05-05-gentoo-linux-should-be-approved-by-the-free-software-foundation/index.md
  content/blog/2022-05-05-gentoo-linux-should-be-approved-by-the-free-software-foundation/index.Rmd

  If you want to publish today, change a file's YAML key to 'date: 2022-05-04'
● [TODO] Found 2 files marked as drafts. To un-draft, run the command:

  blogdown::edit_draft(c(
  "content/blog/2022-05-04-gentoo-linux-the-best-operating-system-ever-made/index.md",
  "content/blog/2022-05-04-gentoo-linux-the-best-operating-system-ever-made/index.Rmd"
  ))

  and change a file's YAML from 'draft: true' to 'draft: false' or delete it
| Checking your R Markdown content...
○ All R Markdown files have been knitted.
● [TODO] Found 1 R Markdown file to update by re-rendering:

  content/blog/2022-05-04-gentoo-linux-the-best-operating-system-ever-made/index.Rmd

  To update a file, re-knit or use blogdown::build_site(build_rmd = 'timestamp')
| Checking for .html/.md files to clean up...
○ Found 0 duplicate .html output files.
○ Found 0 incompatible .html files to clean up.
| Checking for the unnecessary 'content/' directory in theme...
○ Great! Your theme does not contain the content/ directory.
― Check complete: Content

> 
> 
yihui commented 2 years ago

Please provide a full reproducible example every time you file a Github issue to report a problem here. It's hard for us to help you without a reproducible example. I tried to create a new site and add the citation. It worked fine for me:

image

amarakon commented 2 years ago

The source code is on GitHub. And my website is at https://amarakon.github.io/amarakon.com.

Try cloning my repository and changing one of my blog posts to include this code block:

```{r pie, fig.cap = "Operating system market share [@desktop-os-marketshare]", dev = 'svglite'}
slices <- c(74.83, 15.37, 4.93, 2.48, 2.38)
lbls <- c("Windows", "macOS", "Unknown", "Linux", "Chrome OS")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices, labels = lbls, col = rainbow(length(lbls)))
yihui commented 2 years ago

Next time it will be much better if you could provide an example directly instead of providing instructions. The example could be a Git branch. With your instructions, I had to do a few extra things:

  1. Delete https://github.com/amarakon/amarakon.com/blob/2dfb0f5fd94c5a598d34147b9d73493dc5a9792d/.Rprofile#L1 (not sure what that was for, but you'd need knitr::opts_chunk instead of opts_chunk).
  2. The static/bibliography.bib seems to have a syntax error somewhere. I had to delete all entries in it but keep one (for testing purpose).
  3. Copy the chunk you provided into a post.

Anyway, I'm able to reproduce the problem now. Unfortunately, this is not easy to fix and it's unlikely to be fixed. You have three possible options:

  1. Do not use citations in the figure caption.
  2. Or do not use link-citations: true: https://github.com/amarakon/amarakon.com/blob/2dfb0f5fd94c5a598d34147b9d73493dc5a9792d/content/blog/2022-05-05-gentoo-linux-should-be-approved-by-the-free-software-foundation/index.Rmd#L11
  3. Or do not use blogdown.method = 'markdown': https://github.com/amarakon/amarakon.com/blob/2dfb0f5fd94c5a598d34147b9d73493dc5a9792d/.Rprofile#L21
amarakon commented 2 years ago

The reason I set opts_chunk instead of knitr::opts_chunk is that I already set library (knitr) in /usr/lib64/R/etc/Rprofile.site. Should I change it to make it work for everybody?

Also, I fixed this issue by setting blogdown.method = 'html'. Thanks.

yihui commented 2 years ago

I already set library (knitr) in /usr/lib64/R/etc/Rprofile.site. Should I change it to make it work for everybody?

I strongly recommend that you do not touch Rrofile.site, because it will be wiped out the next time you upgrade R, and it's not good for reproducibility (because other people don't have access to your system file). If you need to do anything during R startup, it is better to use .Rprofile or ~/.Rprofile instead.

Anyway, good to know you fixed the issue with the third option!

amarakon commented 2 years ago

Thanks, I will switch to ~/.Rprofile and change opts_chunk to knitr::opts_chunk.