[Tex/LaTex] knitr in RStudio produces ‘Undefined control sequence’, alongside other issues

knitrrstudio

I am getting multiple issues being reported when I try to 'Compile PDF' for a knitr document in RStudio (Windows 7), although the .pdf is still produced in the working directory. I am using R 3.1.3, I have installed MiKTeX and have definitely installed knitr. I have set my global options in RStudio to weave .Rnw files using knitr. When I attempt to compile the following minimal script (basic.Rnw:

\documentclass{article}

\begin{document}

Test:
<<>>=
a <- 4 
@

\end{document}

The following issues are reported in the Compile PDF tab:

  • Line 57 Undefined control sequence. 1
  • Line 57 Undefined control sequence. 1
  • Line 57 Missing number, treated as zero. 2
  • Line 57 Illegal unit of measure (pt inserted) 3
  • Line 57 You can't use \unskip' in vertical mode. 4
  • Line 61 Undefined control sequence.1

and

Warning message: running command
'"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" –quiet –pdf
"inline.tex" –max-iterations=20 -I
"C:/PROGRA~1/R/R-31~1.3/share/texmf/tex/latex" -I
"C:/PROGRA~1/R/R-31~1.3/share/texmf/bibtex/bst"' had status 1

in the console.

The numbers at the end aren't reported and correspond to the following messages in the .log

  1. Undefined control sequence The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., '\hobx'), type `I' and the correct spelling (e.g., 'I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.
  2. Missing number, treated as zero A number should have been here; I inserted '0'. (If you can't figure out why I needed to see a number, look up `weird error' in the index to The TeXbook.)
  3. Illegal unit of measure (pt inserted) Dimensions can be in units of em, ex, in, pt, pc, cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one! I'll assume that you meant to say pt, for printer's points. To recover gracefully from this error, it's best to delete the erroneous units; e.g., type `2' to delete two letters. (See Chapter 27 of The TeXbook.) Otherwise just continue, and I'll forget about whatever was undefined.
  4. You can't use '\unskip' in vertical mode Sorry…I usually can't take things from the current page. Try 'I\vskip-\lastskip' instead.

If I change to <<echo=FALSE>>= the errors are not presented and the .pdf opens up, but if I add print("Hello world") it returns again, so it something to do with printing any form of R code.

I have looked at a solutions online and it seems that many of the issues occur when there is something missing from the preamble. I understand it used to be necessary add \usepackage{Sweave} for Sweave, is a similar thing required for knitr? The tutorial I watched did not seem to indicate this was the case.

I am very new to R and LaTeX (both < 1 week) so apologies if the answer is something very obvious. Also my first post here so please do let me know if there are any issues with formatting etc.

EDIT: Now in R/TeXWorks – similar problems if I run the .Rnw file as above in R ( knit("basic.Rnw") ) the following .tex file is produced

\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}}}%

\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}

Test:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{a} \hlkwb{<-} \hlnum{4}
\end{alltt}
\end{kframe}
\end{knitrout}

\end{document}

TeXworks does not compile the .pdf and I have to abort typesetting. If I do this, the following error is reported in basic.log !

Undefined control sequence. \kframe …argin \hskip \columnwidth
}\MakeFramed
{\advance \hsize -\width … l.56 …0.969, 0.969}\color{fgcolor}\begin{kframe}

and then if I go the Errors, warnings, badboxes, following the link with the error takes me to the line\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe} .

Best Answer

The compile sequence for a file using LaTeX and R code which is to be processed through knitr is as follows where * is your filename without extension. I would recommend that you run these using TeXworks (bundled with MikTeX) and R before you diagnose the problem in RStudio. RStudio in the past was very picky on setup of supporting programs (I have not used it recently)

  1. The *.tex file is renamed with the suffix *.Rnw (this is case sensitive)
  2. Within the now *.Rnw file you need to use R-blocks to load any needed R library's and do any computations.
  3. When the *.Rnw file is ready for a compile (and you file is ready when renamed) you then need to start R, change the working directory to the same one as the *.Rnw file. Now you need to Load the knitr package. (If needed install it and thereafter just load it.
  4. In the R console you issue the command knit *.Rnw and this will create a *.tex file with the R commands replaces with the output.
  5. Now return to your LaTeX and run pdflatex twice and then run your viewer.

If you are using an IDE there are setups which allow you to do all of this from within the LaTeX IDE. However I am not familiar with current version of RStudio. In the past it was not able to run knitr from RStudio as then RStudio was hardwired for Sweave.

I would recommend you visit http://yihui.name/knitr/ as the author of knitr has provided an extensive help and demo site(examples).