Closed bayeslearner closed 10 months ago
I don't think this is the special character. Using URL in \include_graphics
is not supported in PDF. So the image needs to be downloaded first.
Easier reprex:
---
title: "Image URL"
output:
beamer_presentation:
keep_tex: true
pdf_document:
keep_tex: true
---
# test
![](https://raw.githubusercontent.com/rstudio/hex-stickers/main/PNG/knitr.png)
! Package pdftex.def Error: File `https://raw.githubusercontent.com/rstudio/hex-stickers/main/PNG/knitr.png' not found: using draft setting.
Error:
! LaTeX failed to compile test2.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test2.log for more info.
Backtrace:
x
1. \-rmarkdown::render("C:/Users/chris/Documents/test2.Rmd", encoding = "UTF-8")
2. \-rmarkdown:::latexmk(...)
3. \-tinytex::latexmk(file, engine, if (biblatex) "biber" else "bibtex")
4. \-tinytex:::latexmk_emu(...)
5. \-tinytex (local) run_engine()
6. +-tinytex:::system2_quiet(...)
7. \-tinytex (local) on_error()
8. \-tinytex:::show_latex_error(file, logfile)
Exécution arrêtée
This is more a rmarkdown limitation I would say.
Fonction knitr::knit_embed_url()
can help with this for now. Otherwise you need to download locally before inserting.
@yihui How is that suppose to work in R Markdown using url for image and PDF output ? I am surprised we don't download image locally to embed. 🤔 I really thought is would work.
--to pdf
. We bypass this step so we loose the featureShouldn't a fonction like knit::include_graphics()
handle downloading image when inserted in a known format like PDF where this won't work ?
I think we should try to do something (download image?), somewhere (knitr or rmarkdown), somehow (Lua filter in rmarkdown to catch all images ? Or extenting knitr::include_graphics()
to support url...
Right. Pandoc will download images when compiling to PDF, but R Markdown doesn't. This is due to the fact that R Markdown only uses Pandoc to render the intermediate .tex
file, in which case Pandoc wouldn't download images (it downloads images only when rendering to .pdf
directly).
We can certainly try to download images inside knitr::include_graphics()
. The only tricky thing is the download file paths, and how to clean them up after PDF is generated. I think Pandoc just download images to temporary paths, and delete them afterwards. We could do the same thing, but knitr::include_graphics()
needs to know the temp paths, e.g., instructed by rmarkdown::render()
in some way.
Personally I don't prefer images being downloaded again every time the PDF is generated. That can be slow and wastes the bandwidth. I'd cache the download like this: https://stackoverflow.com/a/46333724/559676 It's easy to make a function out of this solution if desired.
web_image = function(url, path = xfun::url_filename(url)) {
if (!file.exists(path)) xfun::download_file(url, path)
knitr::include_graphics(if (knitr::pandoc_to('html')) url else path)
}
Then you call
```{r}
web_image('https://cdn.mathpix.com/cropped/2023_07_28_a103dc94c1ec860738c6g-31.jpg?height=443&width=345&top_left_y=1857&top_left_x=867')
Yes I would do something like that do. A new function seems good for this ! I would just not download the image for html
@bayeslearner Are you okay with using the web_image()
function above? Do I need to add this function to knitr?
Seems to be what we need.
I've added it as a new function download_image()
in the dev version of knitr. Thanks!
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.
The following doc works fine in html but fails beamer. The generated latex file contains special characters. Should it be wrapped in url package or something?
By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.