• R语言
  • knitr中rnw转tex时“missing }”

<br />
\documentclass{article}</p>
<p>\begin{document}</p>
<p><<rgl-demo>>=<br />
knit_hooks$set(rgl = hook_rgl)<br />
head(hook_rgl)  # the hook function is defined as this<br />
@</p>
<p>Then we only have to set the chunk option \texttt{rgl=TRUE}:</p>
<p><<fancy-rgl, rgl=TRUE, dev='png', fig.width=5, fig.height=5, out.width='2in', message=FALSE, cache=TRUE, fig.align='center', fig.show='hold'>>=<br />
library(rgl)<br />
demo('bivar', package='rgl', echo=FALSE)<br />
par3d(zoom=.7)<br />
@</p>
<p>\end{document}<br />
</p>

编译成tex的结果为:

<br />
\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}<br />
%% maxwidth is the original width if it is less than linewidth<br />
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)<br />
\makeatletter<br />
\def\maxwidth{ %<br />
  \ifdim\Gin@nat@width>\linewidth<br />
    \linewidth<br />
  \else<br />
    \Gin@nat@width<br />
  \fi<br />
}<br />
\makeatother</p>
<p>\definecolor{fgcolor}{rgb}{0.2, 0.2, 0.2}<br />
\newcommand{\hlnumber}[1]{\textcolor[rgb]{0,0,0}{#1}}%<br />
\newcommand{\hlfunctioncall}[1]{\textcolor[rgb]{0.501960784313725,0,0.329411764705882}{\textbf{#1}}}%<br />
\newcommand{\hlstring}[1]{\textcolor[rgb]{0.6,0.6,1}{#1}}%<br />
\newcommand{\hlkeyword}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%<br />
\newcommand{\hlargument}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%<br />
\newcommand{\hlcomment}[1]{\textcolor[rgb]{0.180392156862745,0.6,0.341176470588235}{#1}}%<br />
\newcommand{\hlroxygencomment}[1]{\textcolor[rgb]{0.43921568627451,0.47843137254902,0.701960784313725}{#1}}%<br />
\newcommand{\hlformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%<br />
\newcommand{\hleqformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%<br />
\newcommand{\hlassignement}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%<br />
\newcommand{\hlpackage}[1]{\textcolor[rgb]{0.588235294117647,0.709803921568627,0.145098039215686}{#1}}%<br />
\newcommand{\hlslot}[1]{\textit{#1}}%<br />
\newcommand{\hlsymbol}[1]{\textcolor[rgb]{0,0,0}{#1}}%<br />
\newcommand{\hlprompt}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}%</p>
<p>\usepackage{framed}<br />
\makeatletter<br />
\newenvironment{kframe}{%<br />
 \def\at@end@of@kframe{}%<br />
 \ifinner\ifhmode%<br />
  \def\at@end@of@kframe{\end{minipage}}%<br />
  \begin{minipage}{\columnwidth}%<br />
 \fi\fi%<br />
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep<br />
 \colorbox{shadecolor}{##1}\hskip-\fboxsep<br />
     % There is no \\@totalrightmargin, so:<br />
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%<br />
 \MakeFramed {\advance\hsize-\width<br />
   \@totalleftmargin\z@ \linewidth\hsize<br />
   \@setminipage}}%<br />
 {\par\unskip\endMakeFramed%<br />
 \at@end@of@kframe}<br />
\makeatother</p>
<p>\definecolor{shadecolor}{rgb}{.97, .97, .97}<br />
\definecolor{messagecolor}{rgb}{0, 0, 0}<br />
\definecolor{warningcolor}{rgb}{1, 0, 1}<br />
\definecolor{errorcolor}{rgb}{1, 0, 0}<br />
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX</p>
<p>\usepackage{alltt}<br />
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}</p>
<p>\begin{document}</p>
<p>\begin{knitrout}<br />
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}<br />
\begin{alltt}<br />
knit_hooks$\hlfunctioncall{set}(rgl = hook_rgl)<br />
\hlfunctioncall{head}(hook_rgl)  \hlcomment{# the hook function is defined as this}<br />
\end{alltt}<br />
\begin{verbatim}<br />
##<br />
## 1 function (before, options, envir)<br />
## 2 {<br />
## 3     library(rgl)<br />
## 4     if (before || rgl.cur() == 0)<br />
## 5         return()<br />
## 6     name = fig_path("", options)<br />
\end{verbatim}<br />
\end{kframe}<br />
\end{knitrout}</p>
<p>Then we only have to set the chunk option \texttt{rgl=TRUE}:</p>
<p>\begin{knitrout}<br />
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}<br />
\begin{alltt}<br />
\hlfunctioncall{library}(rgl)<br />
\hlfunctioncall{demo}(\hlstring{"bivar"}, package = \hlstring{"rgl"}, echo = FALSE)<br />
\hlfunctioncall{par3d}(zoom = 0.7)<br />
\end{alltt}</p>
<p>{\centering \includegraphics[width=2in]{figure/fancy-rgl} \end{kframe}<br />
\end{knitrout}</p>
<p>\end{document}<br />
</p>

明显倒数第3行{\centering \includegraphics[width=2in]{figure/fancy-rgl} \end{kframe}少了一个“}”,如果去掉“fig.show='hold'”就没有问题了。是bug么?

应该是bug,待我看看,谢谢!

5 天 后

开发版本中已经修正 https://github.com/yihui/knitr#readme