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:



a <- 4 


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


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

%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\def\maxwidth{ %

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}

 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize

\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


\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\hlstd{a} \hlkwb{<-} \hlnum{4}


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

  • When LaTeX looks for a file, it nearly always looks first in the working folder you are compiling from. Glad you got it working, knitr and beamer work very nicely together. Mar 23, 2015 at 23:04

2 Answers 2


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).

  • Thanks, I have followed your instructions above but I still get the same problems in TeXworks. The reported line numbers are different, but the errors are the same (undefined controlled sequence etc.). The .pdf is not produced (unlike in RStudio). Mar 23, 2015 at 11:46
  • If I remove the knitr part of LaTeX code it now compiles. I have looked at the demos previously on mentioned website and if I run his minimal examples I still get the same errors. Mar 23, 2015 at 11:56
  • On my system (Win 8.1, Miktex 2.9, R 3.1.3, Texmaker 4.4.1) your code runs fine. The error messages are coming from the compile of the *.tex file created by knitr. You need to post the *.Rnw, the resulting *.tex, and the portion of the *.log with the errors. I now suspect you have a system setup problem. Mar 23, 2015 at 13:24
  • Thanks - I have edited the question to include these details. Mar 23, 2015 at 14:01
  • Your *.tex compiles fine on my system, so the problem is in the LaTeX setup. You should be compiling with one of pdflatex, latex, Xelatex, or Lualatex. Mar 23, 2015 at 14:18

The problem occurred because of an apparently faulty copy of framed.sty in the directory where basic.tex was stored. This faulty copy was not removed in any reinstalls as it was in the folder in which my .tex file was run from, not in Program Files etc. This explains why I had the same problem on my computers at work and home - I was running from the same flash drive on both PCs.

Removing the old framed.sty and replacing it with a fresh one from CTAN solved the problem.

Another lesson learnt: if something isn't working try putting it in a folder with the bare minimum contents and running the code from there

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .