![如何将 R 代码放入新命令的参数中?](https://linux22.com/image/262612/%E5%A6%82%E4%BD%95%E5%B0%86%20R%20%E4%BB%A3%E7%A0%81%E6%94%BE%E5%85%A5%E6%96%B0%E5%91%BD%E4%BB%A4%E7%9A%84%E5%8F%82%E6%95%B0%E4%B8%AD%EF%BC%9F.png)
我想将 R 代码放入 LaTeX 中新命令的参数中。为此,我做了以下操作:
%Define the newcommand:
\newcommand\solution[1]{\sf #1}
%Then, after the \begin{document}, I write:
\solution{
<<echo=T>>=
x=rnorm(100)
@
}
在我的.Rnw
文件中。在我执行 R CMD Sweave file.Rnw 之后,一切正常,但是当我执行 时
pdflatex file.tex
,响应是:
! FancyVerb Error:
Extraneous input `> x=rnorm(100) \end {Sinput} \end {Schunk} ' between \begin
{Sinput}[<key=value>] and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}
l.12 }
有人能帮助我吗?
答案1
块verbatim
类似于结构,因此不能在内部使用\macro{....}
,而是创建一个solution
环境(顺便说一下,使用\sfseries
,而不是\sf
)
你可能只需要
\newenvironment{solution}{\sffamily}{}
\begin{solution}
<<echo=T>>=
x=rnorm(100)
@
\end{solution}