Sweave 输出中的语法高亮显示
有没有人设法在 Sweave 文档的输出中使用颜色语法突出显示?我已经能够通过在 Sweave.sty
文件中添加框等来自定义输出样式,如下所示:
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontseries=bc,frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=leftline}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontseries=bc}
并且我可以获取 minted
包来进行语法突出显示我的文档中的逐字代码块如下所示:
\begin{minted}{perl}
use Foo::Bar;
...
\end{minted}
但我不确定如何将两者组合起来用于 R 输入部分。我尝试了以下操作:
\DefineVerbatimEnvironment{Sinput}{minted}{r}
\DefineVerbatimEnvironment{Scode}{minted}{r}
有什么建议吗?
Has anyone managed to get color syntax-highlighting working in the output of Sweave documents? I've been able to customize the output style by adding boxes, etc. in the Sweave.sty
file as follows:
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontseries=bc,frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=leftline}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontseries=bc}
And I can get the minted
package to do syntax highlighting of verbatim-code blocks in my document like so:
\begin{minted}{perl}
use Foo::Bar;
...
\end{minted}
but I'm not sure how to combine the two for R input sections. I tried the following:
\DefineVerbatimEnvironment{Sinput}{minted}{r}
\DefineVerbatimEnvironment{Scode}{minted}{r}
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,请查看 Rcpp 的一些小插图(仅选择一个) Rcpp-FAQ pdf。
我们使用 Romain 的 highlight,它本身可以外包给 hightlight 二进制文件,作者:Andre Simon。它使一切变得更加复杂——用于小插图等的 Makefiles——但是我们从 R 和 C/C++ 代码中获得了丰富多彩的输出。这使得它值得。
Yes, look at some of the vignettes for Rcpp as for example (to pick just one) the Rcpp-FAQ pdf.
We use the highlight by Romain which itself can farm out to the hightlight binary by Andre Simon. It makes everything a little more involved---Makefiles for the vignettes etc pp---but we get colourful output from R and C/C++ code. Which makes it worth it.
我有一个对我有用的解决方案,但我还没有在任何其他系统上尝试过它,所以事情可能不适合你。我在 https://gist.github.com/797478 发布了一些代码,它们是一组修改后的 Rweave 驱动程序函数使用铸造块而不是逐字块。
要使用此驱动程序,只需在使用 driver=RweaveLatexMinted() 选项调用 Sweave 函数时指定它即可。
I have a solution that has worked for me, I have not tried it on any other systems though so things may not work out of the box for you. I've posted some code at https://gist.github.com/797478 that is a set of modified Rweave driver functions that make use of minted blocks instead of verbatim blocks.
To use this driver just specify it when calling the Sweave function with the driver=RweaveLatexMinted() option.
以下是我从 @daroczig 的建议开始解决这个问题的方法。
当我这样做时,我需要让缓存正常工作,因为我使用的是大型数据集,并且一个数据块大约需要 3 分钟才能完成。因此,我编写了这个
zsh
shell 函数来处理带有缓存的.Rnw
文件:现在我只需执行
sweaveCache myFile
即可得到打开的结果预览(在 OS X 上)。Here's how I've ended up solving it, starting from @daroczig's suggestion.
While I was at it, I needed to get caching working because I'm using large data sets and one chunk was taking around 3 minutes to complete. So I wrote this
zsh
shell function to process an.Rnw
file with caching:Now I just do
sweaveCache myFile
and I get the result opened in Preview (on OS X).您可能会对 tex.StackExchange 上的这个主题感兴趣,因为它建议在 R 中加载 SweaveListingUtils 包 以方便解决方案。
This topic on tex.StackExchange might be interesting for you, as it suggest loading the SweaveListingUtils package in R for easy solution.