Does latex(describe(...)) work under knitr with MiKTeX?
hmisc, knitr, latex, miktex
Solution
Since the `spacing` environment is undefined, add
\usepackage{setspace}
to your preamble, since `setspace` defines `spacing`.
Problem
When I create a file describe.Rnw as follows: ``` \documentclass{article} \begin{document} <<results='asis'>>= require(Hmisc) latex(describe(cars), file="") @ \end{document} ``` and try to compile it with ``` library(knitr) knit2pdf("describe.Rnw") ``` I get error messages that suggest the latex created by the latex(describe()) combination has grammatical errors: ``` LaTeX errors: P:/r/ellisp/test/describe.tex:65: LaTeX Error: Environment spacing undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help Your command was ignored. P:/r/ellisp/test/describe.tex:131: LaTeX Error: \begin{document} ended by \end{ spacing}. See the LaTeX manual or LaTeX Companion for explanation. ``` I can see the latex it has generated, pasted below - something wrong perhaps with the line spacing confusing the parsing? My whole setup is new (in fact in testing mode) and I'm new to knitr although I've successfully compiled documents, but I don't know whether there may be something wrong with my setup too. My first question is, am I doing something wrong or is this a more general problem?? ``` > latex(describe(cars), file="") \begin{spacing}{0.7} \begin{center}\textbf{ cars \\ 2 Variables~~~~~ 50 ~Observations}\end{center} \smallskip\hrule\smallskip{\small \vbox{\noindent\textbf{speed} {\smaller \begin{tabular}{ rrrrrrrrrrr } n&missing&unique&Mean&.05&.10&.25&.50&.75&.90&.95 \\ 50&0&19&15.4& 7.0& 8.9&12.0&15.0&19.0&23.1&24.0 \end{tabular} \begin{verbatim} 4 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 Frequency 2 2 1 1 3 2 4 4 4 3 2 3 4 3 5 1 1 4 1 % 4 4 2 2 6 4 8 8 8 6 4 6 8 6 10 2 2 8 2 \end{verbatim} } \smallskip\hrule\smallskip } \vbox{\noindent\textbf{dist}\setlength{\unitlength}{0.001in}\hfill\begin{picture}(1.5,.1)(1500,0)\linethickness{0.6pt} \put(0,0){\line(0,1){25}} \put(15,0){\line(0,1){25}} \put(90,0){\line(0,1){50}} \put(150,0){\line(0,1){25}} \put(165,0){\line(0,1){25}} \put(180,0){\line(0,1){25}} \put(195,0){\line(0,1){25}} \put(225,0){\line(0,1){50}} \put(240,0){\line(0,1){25}} \put(270,0){\line(0,1){25}} \put(300,0){\line(0,1){100}} \put(330,0){\line(0,1){50}} \put(375,0){\line(0,1){75}} \put(405,0){\line(0,1){75}} \put(420,0){\line(0,1){50}} \put(480,0){\line(0,1){50}} \put(495,0){\line(0,1){25}} \put(555,0){\line(0,1){50}} \put(570,0){\line(0,1){25}} \put(600,0){\line(0,1){25}} \put(630,0){\line(0,1){25}} \put(660,0){\line(0,1){50}} \put(675,0){\line(0,1){50}} \put(735,0){\line(0,1){25}} \put(780,0){\line(0,1){25}} \put(810,0){\line(0,1){25}} \put(825,0){\line(0,1){25}} \put(855,0){\line(0,1){25}} \put(930,0){\line(0,1){25}} \put(990,0){\line(0,1){25}} \put(1035,0){\line(0,1){25}} \put(1050,0){\line(0,1){25}} \put(1140,0){\line(0,1){25}} \put(1155,0){\line(0,1){25}} \put(1485,0){\line(0,1){25}} \end{picture} {\smaller \begin{tabular}{ rrrrrrrrrrr } n&missing&unique&Mean&.05&.10&.25&.50&.75&.90&.95 \\ 50&0&35&42.98&10.00&15.80&26.00&36.00&56.00&80.40&88.85 \end{tabular} \begin{verbatim} lowest : 2 4 10 14 16, highest: 84 85 92 93 120 \end{verbatim} } \smallskip\hrule\smallskip } }\end{spacing} ``` Here's my session info: sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-w64-mingw32/x64 (64-bit) ``` locale: [1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252 [3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C [5] LC_TIME=English_New Zealand.1252 attached base packages: [1] splines graphics datasets grid stats grDevices utils [8] methods base other attached packages: [1] x12_1.1-0 tseries_0.10-32 stringr_0.6.2 lubridate_1.3.0 [5] tables_0.7 Hmisc_3.12-2 Formula_1.1-1 survival_2.37-4 [9] stargazer_4.0 knitr_1.2 proto_0.3-10 extrafont_0.14 [13] Defaults_1.1-1 scales_0.2.3 reshape2_1.2.2 plyr_1.8 [17] ggplot2_0.9.3.1 foreign_0.8-53 mvtnorm_0.9-9995 loaded via a namespace (and not attached): [1] cluster_1.14.4 codetools_0.2-8 colorspace_1.2-2 dichromat_2.0-0 [5] digest_0.6.3 evaluate_0.4.4 formatR_0.9 gtable_0.1.2 [9] labeling_0.2 lattice_0.20-15 MASS_7.3-27 munsell_0.4.2 [13] quadprog_1.5-5 RColorBrewer_1.0-5 rpart_4.1-1 Rttf2pt1_1.2 [17] tools_3.0.1 zoo_1.7-10 ``` Edit - here is the describe.tex document created by knitr. ``` \documentclass{article}\usepackage{graphicx, 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.2, 0.2, 0.2} \newcommand{\hlnumber}[1]{\textcolor[rgb]{0,0,0}{#1}}% \newcommand{\hlfunctioncall}[1]{\textcolor[rgb]{0.501960784313725,0,0.329411764705882}{\textbf{#1}}}% \newcommand{\hlstring}[1]{\textcolor[rgb]{0.6,0.6,1}{#1}}% \newcommand{\hlkeyword}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}% \newcommand{\hlargument}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}% \newcommand{\hlcomment}[1]{\textcolor[rgb]{0.180392156862745,0.6,0.341176470588235}{#1}}% \newcommand{\hlroxygencomment}[1]{\textcolor[rgb]{0.43921568627451,0.47843137254902,0.701960784313725}{#1}}% \newcommand{\hlformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}% \newcommand{\hleqformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}% \newcommand{\hlassignement}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}% \newcommand{\hlpackage}[1]{\textcolor[rgb]{0.588235294117647,0.709803921568627,0.145098039215686}{#1}}% \newcommand{\hlslot}[1]{\textit{#1}}% \newcommand{\hlsymbol}[1]{\textcolor[rgb]{0,0,0}{#1}}% \newcommand{\hlprompt}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#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} \begin{kframe} \begin{alltt} \hlfunctioncall{require}(Hmisc) \hlfunctioncall{latex}(\hlfunctioncall{describe}(cars), file = \hlstring{""}) \end{alltt} \end{kframe}\begin{spacing}{0.7} \begin{center}\textbf{ cars \\ 2 Variables~~~~~ 50 ~Observations}\end{center} \smallskip\hrule\smallskip{\small \vbox{\noindent\textbf{speed} {\smaller \begin{tabular}{ rrrrrrrrrrr } n&missing&unique&Mean&.05&.10&.25&.50&.75&.90&.95 \\ 50&0&19&15.4& 7.0& 8.9&12.0&15.0&19.0&23.1&24.0 \end{tabular} \begin{verbatim} 4 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 Frequency 2 2 1 1 3 2 4 4 4 3 2 3 4 3 5 1 1 4 1 % 4 4 2 2 6 4 8 8 8 6 4 6 8 6 10 2 2 8 2 \end{verbatim} } \smallskip\hrule\smallskip } \vbox{\noindent\textbf{dist}\setlength{\unitlength}{0.001in}\hfill\begin{picture}(1.5,.1)(1500,0)\linethickness{0.6pt} \put(0,0){\line(0,1){25}} \put(15,0){\line(0,1){25}} \put(90,0){\line(0,1){50}} \put(150,0){\line(0,1){25}} \put(165,0){\line(0,1){25}} \put(180,0){\line(0,1){25}} \put(195,0){\line(0,1){25}} \put(225,0){\line(0,1){50}} \put(240,0){\line(0,1){25}} \put(270,0){\line(0,1){25}} \put(300,0){\line(0,1){100}} \put(330,0){\line(0,1){50}} \put(375,0){\line(0,1){75}} \put(405,0){\line(0,1){75}} \put(420,0){\line(0,1){50}} \put(480,0){\line(0,1){50}} \put(495,0){\line(0,1){25}} \put(555,0){\line(0,1){50}} \put(570,0){\line(0,1){25}} \put(600,0){\line(0,1){25}} \put(630,0){\line(0,1){25}} \put(660,0){\line(0,1){50}} \put(675,0){\line(0,1){50}} \put(735,0){\line(0,1){25}} \put(780,0){\line(0,1){25}} \put(810,0){\line(0,1){25}} \put(825,0){\line(0,1){25}} \put(855,0){\line(0,1){25}} \put(930,0){\line(0,1){25}} \put(990,0){\line(0,1){25}} \put(1035,0){\line(0,1){25}} \put(1050,0){\line(0,1){25}} \put(1140,0){\line(0,1){25}} \put(1155,0){\line(0,1){25}} \put(1485,0){\line(0,1){25}} \end{picture} {\smaller \begin{tabular}{ rrrrrrrrrrr } n&missing&unique&Mean&.05&.10&.25&.50&.75&.90&.95 \\ 50&0&35&42.98&10.00&15.80&26.00&36.00&56.00&80.40&88.85 \end{tabular} \begin{verbatim} lowest : 2 4 10 14 16, highest: 84 85 92 93 120 \end{verbatim} } \smallskip\hrule\smallskip } }\end{spacing} \end{document} ```