I am struggling for three days now with the following issue and it's driving me crazy:

I am trying to use the "compile pdf" in knitr (R Studio) to compile a basic .rnw file:




But I get the following error message:

Running pdflatex.exe on sweave-test.tex...failed
Error running C:/Users/ERMI-0~1/AppData/Local/Programs/MiKTeX/miktex/bin/x64/pdflatex.exe (exit code -1073740791)

The problem seems to be that R uses this path to access the pdflatex.exe:

Calling Sys.getenv("PATH") gets me the a different path however, where pdflatex.exe is actually located (I checked):

[1] "C:\\Program Files\\R\\R-4.1.1\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files\\Git\\cmd;C:\\Users\\Ermi-05-2021\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Ermi-05-2021\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\"

but when I check specifically with Sys.which("pdflatex"), it shows this weird path:


Is this path correct after all and points to the Miktex package repository? In that case, why is the command failing? Or is the path actually wrong? If so, how can I change it? I tried the solutions in this post: https://stackoverflow.com/questions/51814278/set-path-to-miktex-for-pdflatex-in-r but didn't succeed.

Also, I unistalled and reinstalled R Studio, R and Miktex several times without success, the problem remains.

Can anyone help me with this?

  • knitr appears to be broken for MikTeX installations. See RNW file: "Compile PDF" button fails after MiKTeX update. The suggestion there is to do: tinytex::install_tinytex() at the console, and CompilePDF. You will need content like "Hello!" between begin{document} and end{document} to produce a pdf file. I tried this, but it didn't work for me.
    – Ross
    Sep 7 at 14:51
  • As Ross said, too basic. Add 2+2=\Sexpr{2+2} and check again.
    – Fran
    Sep 7 at 17:27
  • Adding content to the rnw file didn't help, but the suggestion in the link Ross shared solved it for me! I simply installed tinytex and set R in Global Options > Sweave Options to use tinytex when compiling .tex files. This worked for me! Thanks for the help!
    – Ermi Atha
    Sep 9 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.