I found myself wanting to install many missing LaTeX packages last night, and it looks like there might approaches to do this in tinytex, but I could not quite work this out.
I get the sense that maybe what I'm trying to do is already possible in tinytex, and I was going to ask this as an issue on the repo there, but it seems that @yihui prefers that we ask a question here on RStudio community first, so I am doing that there.
I guess while I'm here, would the workflow suggested in chaletex:
For what I know, when using tinytex, if you knit a Rmd file in rstudio, it will find the missing latex package deps and install those missing packages before rendering to pdf.
This is what I use so far and its working fine.
From the doc, this work with those engine pdflatex() , xelatex() , and lualatex() .
The function that parses packages is tinytex::parse_packages() and the function y ah compiles while trying to install packages is tinytex::latexmk
You'll find some information in how the R package tinytex is doing in https://yihui.name/tinytex/r/
I think I had this problem because I was editing a .tex file directly in RStudio, rather than directly editing the .Rmd file.
I don't often find myself in the position where I am editing a .tex file, so maybe that's why I hadn't encountered it before.
Reading through the tinytex documentation, it seems like I should have called latexmk() on the TeX file. That is super useful!
But I still feel like it would be really useful to have a solution to extract all the packages required by a LaTeX document, and then have a way to try and install them.
The log file approach in tinytex::parse_packages() is great, but I think that something more explicit like extract_packages() would signal to me the user a way forward.
Do you think I should I submit an issue on tinytex, or is it better to have discussion here?
This seems indeed like a nice approach. I would suggest you open a feature request in tinytex with your implementation. I think it could at least be added as a new independant function if not replacing the current system. If you even directly put it in a (draft?) PR, it would be easy for Yihui to see if it is of interest for Tinytex and I think his opinion would be valuable (he may have tried already your approach and know a not obvious drawback).
I think your approach may work in most cases, but parsing a .tex document using regular expressions won't be 100% reliable. That was a nice hack, though.