2

I'm trying to compile a simple Rnw file in Rstudio using knitr "compile pdf" functionality. But it gives me an error.

This is what I have in hello.Rnw:

\documentclass{article}

\begin{document}

Hello World

\end{document}

Error:

Running pdflatex.exe on hello.tex...failed

Error running C:/PROGRA~1/MIKTEX~1.9/miktex/bin/x64/pdflatex.exe (exit code 1)

However if I typeset hello.tex using TeXworks, it generates a pdf file without any error. I checked my Sys.getenv("PATH") variable, and it shows the correct MiKTeX location. So I'm not sure why RStudio pdflatex is not working.

"C:\\Users\\zk0q8r5\\Documents\\R\\R-3.5.0\\bin\\x64;
C:\\ProgramData\\Oracle\\Java\\javapath;
C:\\WINDOWS\\system32;
C:\\WINDOWS;
C:\\WINDOWS\\System32\\Wbem;
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;
C:\\Program Files\\1E\\NomadBranch\\;
C:\\Program Files (x86)\\Microsoft Application Virtualization Client;
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;
C:\\Program Files (x86)\\WebEx\\Productivity Tools;
C:\\Program Files\\TortoiseSVN\\bin;
C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64;
C:\\MiKTeX\\miktex\\bin\\x64\\"
Troy
  • 13,741
ska
  • 31
  • do you have a pdflatex.exe in C:\Program Files\MiKTeX 2.9\miktex\bin\x64 ? – David Carlisle May 02 '18 at 21:21
  • @David, Yes, I've verified that there is a pdflatex.exe file at this location. – ska May 02 '18 at 22:07
  • @Fran, I'm not sure how it is going to MIKTEX~1.9 while there is no such version. I don't see any such version. I also tried rebooting the system but it didn't help. – ska May 02 '18 at 22:09
  • 1
    @Fran That is an 8.3 filename: https://en.wikipedia.org/wiki/8.3_filename You should not interpret ~1 as literally ~1. – Yihui Xie May 03 '18 at 02:26
  • @YihuiXie Hahaha ...you are right. How I did not realize! The mistake is doubly silly because for a long time I was a DOS user before and after the onset of long names in Windows. Even today I hate names not limited to 8.3 characters in any OS, in part due to the problems that I had with the ~1 truncations. – Fran May 03 '18 at 09:39
  • A possible source of this error code: privileges issues. On a new installation with R studio (or from a cmd prompt) executing/invoked with standard user privileges, pdflatex issued that error (along with a miktex db lock error I believe), whereas from a 'run as administrator' invocation, no issue. Having temporarily admin rights, I uninstalled/reinstalled miktex, this time selecting the installation option (in the installer) to allow miktex use for all users on the machine. Issue resolved: success with std user privileges . <An answer, not a comment, but I don't have the rep to post as such> – peter a g Dec 09 '20 at 21:57

3 Answers3

1

So I figured out the problem. There were two MikTeX installations on my machine. And the R PATH variable was pointing to the wrong one. I simply followed the instructions from the below link and it resolved the issue. Thank you! https://stackoverflow.com/questions/33650869/how-can-i-set-the-latex-path-for-sweave-in-r

ska
  • 31
1

I had the exact same problem. My solution was in this thread:

Latex Error: 'Sweave.sty' not found?

To find the solution I first had to get a better error message than

Running pdflatex.exe on hello.tex...failed

Error running C:/PROGRA~1/MIKTEX~1.9/miktex/bin/x64/pdflatex.exe (exit code 1)

I noticed that when I try to compile the .Rnw file it creates a .tex file. So I opened that .tex file and tried to compile it in my regular LaTeX work environment (VSCode for me), and I got this error message:

LaTeX Error: File `Sweave.sty' not found.

So really the problem was that LaTeX couldn't find this file and the solution to that (for me) is in the link above. I had to write this all out because solving this has taken so much of my time so I don't want it to be for nothing.

1

I had the same error message, i.e.:

Error running C:/PROGRA~1/MIKTEX~1.9/miktex/bin/x64/pdflatex.exe (exit code 1)

I checked the PATH, which was OK -- and I had only one tex installation, so Sunny Kanugo's answer wouldn't work.

Just to give it a try, I found an earlier *.tex file that had been created in RStudio with knitr, and compiled it with PdfLaTeX. During this, MikTeX installed a number of sty files, and finally, a PDF was successfully created. And the error disappeared: that is, I could compile PDF files from inside RStudio.

So it looks like when you run PdfLaTeX from inside RStudio, some of the packages won't be installed and an error will be given instead. Below is the "Hello world" tex file that I will use next time I have this problem (which may easily happen if I have to reinstall MiKTeX for some reason); copy everything from the textbox and save it as plain text file with *.tex extension; then open command prompt in the right place (i.e. the folder where you saved that tex file), type pdflatex, and then the name of the file, and see if a PDF appears.

\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}



Hello World

\end{document}
epR8GaYuh
  • 2,432
lebatsnok
  • 111
  • I had the exact same problem. When this question was posted however I wasn't, it was only when I uninstalled R and Latex and installed them again now. RStudio used to always install the packages via Miktek on the fly but it seems it didn't for some reason? My PATH was correct also, only one version of Miktek installed. I'm not sure what changed. – Prevost Jan 25 '21 at 16:52
  • It seems I had to change the MikTex package setting to always install packages on the fly. It was previously set to ask me first (I had always set it to ask me first but for some reason now it didn't work...). It is working fine now. – Prevost Jan 25 '21 at 18:03