2013-08-21 35 views
4

当我创建一个文件describe.Rnw如下:乳胶(描述(...))与MiKTeX一起在knitr下工作吗?

\documentclass{article} 

\begin{document} 

<<results='asis'>>= 
require(Hmisc) 
latex(describe(cars), file="") 
@ 


\end{document} 

,并尝试与

library(knitr) 
knit2pdf("describe.Rnw") 

我得到的建议由乳胶创建的乳胶的错误信息进行编译(形容())组合有语法错误:

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. 

我可以看到它已经产生的乳胶,粘贴以下 - 这与行距混淆也许错了解析?我的整个设置是新的(实际上是在测试模式下),尽管我已经成功编译了文档,但我还是新手,但我不知道我的设置是否可能有问题。我的第一个问题是,我做错了什么或者这是一个更普遍的问题?

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

这里是我的会话信息:

sessionInfo() [R版本3.0.1(2013年5月16日) 平台:x86_64的-W64-的mingw32/64(64位)

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   

编辑 - 这里是由knitr创建的describe.tex文档。

\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\[email protected]@width>\linewidth 
    \linewidth 
    \else 
    \[email protected]@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\[email protected]@[email protected]{}% 
\ifinner\ifhmode% 
    \def\[email protected]@[email protected]{\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\[email protected] \linewidth\hsize 
    \@setminipage}}% 
{\par\unskip\endMakeFramed% 
\[email protected]@[email protected]} 
\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} 
+0

你的LaTeX文件需要一个'\ end {document}',但是这个错误很明显。 – Werner

+0

谢谢@Werner,但在原始describe.Rnw中有一个\ end {document},并且它存活到describe.tex。我编辑了这个问题以包含由knitr创建的完整describe.tex文件。这个问题似乎与\ begin {...}命令有关,因为某些原因它不能被识别,因此\ begin {}和end {}命令不同步。 –

+0

如果未定义'spacing',则应该将['setspace'](http://ctan.org/pkg/setspace)包添加到您的序言中。 – Werner

回答

4

由于spacing环境是不确定的,加

\usepackage{setspace} 

你的序言,因为setspace定义spacing