is_latex <- function() { identical(knitr::opts_knit$get('rmarkdown.pandoc.to'), "latex") } embed_jpg <- function(path, dpi) { dim <- jpg_dim(path) if (is_latex()) { width <- round(dim[2] / dpi, 2) knitr::asis_output(paste0( "\\includegraphics[", "width=", width, "in", "]{", path, "}" )) } else { knitr::asis_output(paste0( "" )) } } jpg_dim <- function(path) { dim(jpeg::readJPEG(path, native = TRUE)) }