How to install missing LaTeX Packages

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 ended up writing to help simplify the process of installing LaTeX packages listed in a LaTeX file.

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:

read_tex("path/to/file.tex") %>%
  tex_extract_preamble() %>%
  tex_extract_pkgs() %>%

Be something that could be ported into tinytex?

1 Like

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

Hope it helps.


I discovered the Twitter thread later so I post it here to make this discussion complete because it has interesting answers

Adding another approach to tinytex could be interesting.
I did not have issues with the existing one but if it could improve...

1 Like

Thanks @cderv!

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 have filed a request to the RStudio IDE more than a year ago, so please upvote it or show your interest by replying to it:

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.


OK, I've chimed in on the github issue :slight_smile:

Totally agree that parsing a .tex document using regex won't be 100% reliable, but I'm glad you like the hack :slight_smile:


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.