Sweave 输出中的语法高亮显示

发布于 2024-10-14 13:38:32 字数 646 浏览 7 评论 0原文

有没有人设法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

比忠 2024-10-21 13:38:32

是的,请查看 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.

留蓝 2024-10-21 13:38:32

我有一个对我有用的解决方案,但我还没有在任何其他系统上尝试过它,所以事情可能不适合你。我在 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.

女中豪杰 2024-10-21 13:38:32

以下是我从 @daroczig 的建议开始解决这个问题的方法。

\usepackage{minted}

\renewenvironment{Sinput}{\minted[frame=single]{r}}{\endminted}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=leftline}
\DefineVerbatimEnvironment{Scode}{Verbatim}{}

当我这样做时,我需要让缓存正常工作,因为我使用的是大型数据集,并且一个数据块大约需要 3 分钟才能完成。因此,我编写了这个 zsh shell 函数来处理带有缓存的 .Rnw 文件:

function sweaveCache() {
    Rscript -e "library(cacheSweave); setCacheDir(getwd()); Sweave('$1.Rnw', driver = cacheSweaveDriver)" &&
    pdflatex --shell-escape $1.tex &&
    open $1.pdf
}

现在我只需执行 sweaveCache myFile 即可得到打开的结果预览(在 OS X 上)。

Here's how I've ended up solving it, starting from @daroczig's suggestion.

\usepackage{minted}

\renewenvironment{Sinput}{\minted[frame=single]{r}}{\endminted}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=leftline}
\DefineVerbatimEnvironment{Scode}{Verbatim}{}

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:

function sweaveCache() {
    Rscript -e "library(cacheSweave); setCacheDir(getwd()); Sweave('$1.Rnw', driver = cacheSweaveDriver)" &&
    pdflatex --shell-escape $1.tex &&
    open $1.pdf
}

Now I just do sweaveCache myFile and I get the result opened in Preview (on OS X).

甜是你 2024-10-21 13:38:32

This topic on tex.StackExchange might be interesting for you, as it suggest loading the SweaveListingUtils package in R for easy solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文