Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References \ref{fig:chunk_label} in markdown with pdf_document output are only resolved when specifying chunk options in addition to fig.cap #2391

Closed
5 tasks done
jranke opened this issue Jul 23, 2022 · 5 comments
Labels
question general questions - not an issue

Comments

@jranke
Copy link

jranke commented Jul 23, 2022

Consider the following example markdown:

---
title: MWE for figure labels
output:
  pdf_document:
    keep_md: true
    fig_height: 2
---

I think his reference should be resolved to the following Figure \ref{fig:plot_1}.

```{r plot_1, fig.cap = "Plot 1"}
plot(0)
```

Generation of the reference is triggered e.g. by requesting centering in the chunk
options (Figure \ref{fig:plot_2}).

```{r plot_2, fig.cap = "Plot 2", fig.align = "center"}
plot(0)
```

Rendering with rmarkdown::render() shows that only the second reference is resolved:

image

The markdown generated for the plots is the following:

```r
plot(0)
```

![Plot 1](mwe_files/figure-latex/plot_1-1.pdf)

and

```r
plot(0)
```

\begin{figure}

{\centering \includegraphics{mwe_files/figure-latex/plot_2-1} 

}

\caption{Plot 2}\label{fig:plot_2}
\end{figure}

Reading the documentation of the plot specific chunk options, especially of fig.lp, I would expect that the label would also be generated for the first chunk.

R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 11 (bullseye)

Locale:
  LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C              
  LC_TIME=de_DE.UTF-8        LC_COLLATE=de_DE.UTF-8    
  LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=de_DE.UTF-8   
  LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       

Package version:
  base64enc_0.1.3  bslib_0.4.0      cachem_1.0.6     digest_0.6.29   
  evaluate_0.15    fastmap_1.1.0    fs_1.5.2         glue_1.6.2      
  graphics_4.2.1   grDevices_4.2.1  highr_0.9        htmltools_0.5.3 
  jquerylib_0.1.4  jsonlite_1.8.0   knitr_1.39       magrittr_2.0.3  
  memoise_2.0.1    methods_4.2.1    R6_2.5.1         rappdirs_0.3.3  
  rlang_1.0.4      rmarkdown_2.14.3 sass_0.4.2       stats_4.2.1     
  stringi_1.7.8    stringr_1.4.0    tinytex_0.40     tools_4.2.1     
  utils_4.2.1      xfun_0.31        yaml_2.3.5      

Pandoc version: 2.9.2.1

Checklist

When filing a bug report, please check the boxes below to confirm that you have provided us with the information we need. Have you:

  • formatted your issue so it is easier for us to read?

  • included a minimal, self-contained, and reproducible example?

  • pasted the output from xfun::session_info('rmarkdown') in your issue?

  • upgraded all your packages to their latest versions (including your versions of R, the RStudio IDE, and relevant R packages)?

  • installed and tested your bug with the development version of the rmarkdown package using remotes::install_github("rstudio/rmarkdown")?

@cderv
Copy link
Collaborator

cderv commented Jul 26, 2022

Referencing mechanism for all figure with caption is available through bookdown::pdf_document2 which brings bookdown feature https://bookdown.org/yihui/bookdown/figures.html

Without pdf_document, you get it working in the second case because adding fig.align is among the special option that make knitr write the plot code directly in LaTeX (option that triggers that are c('out.width', 'out.height', 'out.extra', 'fig.align', 'fig.subcap', 'fig.env', 'fig.scap', 'fig.alt'))

In the first case, knitr will generate markdown code ![caption](img/path) that won't have the label feature. That is why it is not working.

Using bookdown feature would get use labelling and numbering for both PDF and HTML.

@yihui is this explain better somewhere ?

@yihui
Copy link
Member

yihui commented Jul 26, 2022

That's a clear enough explanation. I don't remember if this is documented anywhere, although I have explained it a few times before in various places.

@jranke
Copy link
Author

jranke commented Jul 26, 2022

Thank you both for your time! I did not know about bookdown::pdf_document2, I just tried it and it works nicely for my purposes. And I do not know what would be the best place to document this. I was looking at the knitr chunk options to find out how it works, but I was actually using rmarkdown::pdf_documenta and rmarkdown::render.

@cderv
Copy link
Collaborator

cderv commented Jul 26, 2022

I think the fact you ask the question here is part of the documentation for those having the same question. Using bookdown is the really the good way in that case.

Thank for asking !

@cderv cderv closed this as completed Jul 26, 2022
@cderv cderv added the question general questions - not an issue label Jul 26, 2022
jranke added a commit to jranke/yihui that referenced this issue Jul 26, 2022
yihui pushed a commit to yihui/yihui.org that referenced this issue Jul 26, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question general questions - not an issue
Projects
None yet
Development

No branches or pull requests

3 participants