Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

How can I create RMarkdown files that render well in PDF and HTML and which also contain numbered figures?

$
0
0

I have been working on some documents that should render in both HTML and PDF. For referencing in Latex I have used the \ref{fig:fig1}style and giving figures a numbered caption.

For referencing in HTML I have tried several other things. Using bookdown::html_document2 enables me to use \@ref(fig:fig1).

But this is still not the same text. Is there a way of referencing figures that works in both PDF and HTML and uses the same tag in the source Rmd-file?

I've tried using knitr::opts_knit$get("rmarkdown.pandoc.to") to create a function that switches between contexts, but this seems unsatisfactory. Are there any better options?

Thanks :)

I wrote this helper function, but this seems too complex.

ref <- function(label,output = knitr::opts_knit$get("rmarkdown.pandoc.to")){
  if(!is.null(output)){
    if(output == "latex") {
      return(
        paste0("\\ref{",label,"} ")
        )
    }
    if(output == "html") {
      return(paste0("\\@ref(",label,") "))
    }
  }
}

------------ Solution ---------------

When using both pdf_document2 and html_document2 as output from bookdown, the regular \@ref(fig:fig) version works.


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>