Knitr:Markdown 文档中 LaTeX 环境中的 R 代码 [英] Knitr: R code within LaTeX environment in a Markdown document

查看:23
本文介绍了Knitr:Markdown 文档中 LaTeX 环境中的 R 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Markdown 中有一个文档,其中包含通过 Knitr 的 R 代码.对于渲染方程,我使用 LaTeX,只需在文本中写下它的命令.假设我有以下 LaTeX 代码:

I have a document in Markdown, which incorporates R code via Knitr. For rendering equations I use LaTeX, simply writing its commands in the text. Say I have the following LaTeX code:

egin{displaymath}
mathbf{X} = egin{bmatrix}
2 & 12\ 
3 & 17\ 
5 & 10\ 
7 & 18\ 
9 & 13\
end{bmatrix}
end{displaymath}

当我将所有内容转换为 PDF 时,这为我提供了一个很好的数学矩阵表示(在方括号中)(完整的工作流程是:RMD -> knitr -> MD -> pandoc -> TeX -> pandoc -> PDF).

Which renders me a nice mathematical matrix representation (in square brackets), when I convert everything to PDF (the full workflow is then: RMD -> knitr -> MD -> pandoc -> TeX -> pandoc -> PDF).

现在,假设我们希望从一个 R 对象(某个 R 矩阵 x)即时生成所述矩阵.在 这篇文章我们确定了如何生成所需的 LaTeX 代码(matrix2latex() 函数在那里定义).现在的问题是如何让 Knitr 对其进行评估.我尝试了以下代码:

Now, suppose we want the said matrix to be generated on-the-fly from an R object, some R matrix x. In this post we establish how to generate the required LaTeX code (the matrix2latex() function is defined there). The question now is how to get Knitr to evaluate it. I tried the following code:

egin{displaymath}
mathbf{X} = `r matrix2latex(x)`
end{displaymath} 

但它只是在 R 输出应位于的位置产生空格(实际上是 NULL).我很惊讶这还没有被问到(至少我自己的搜索没有产生任何结果).任何想法如何使这项工作?

but it simply produces blank space (NULL actually) where the R output should be. I am surprised this hasn't been asked already (at least my own search yielded nothing). Any ideas how to make this work?

按照建议,重写了没有 cat() 的函数.这是该函数的工作版本以供将来参考:

As suggested, rewrote the function without cat(). Here is the working version of the function for future reference:

m2l <- function(matr) {

    printmrow <- function(x) {

        ret <- paste(paste(x,collapse = " & "),"\\")
        sprintf(ret)
    }

    out <- apply(matr,1,printmrow)
    out2 <- paste("\begin{bmatrix}",paste(out,collapse=' '),"\end{bmatrix}")
    return(out2)
}

推荐答案

这是因为您的 matrix2latex 函数使用 cat 并将其输出发送到标准输出流,这不是 knitr 试图放置输出的地方.

This is because your matrix2latex function uses cat and sends its output to the standard output stream, which isn't where knitr is trying to put the output.

两种解决方案:一种是重写您的函数以使用 pastesprintf 或其他字符串格式化函数将输出构造为字符串,或者作为快速hack 换行它在 capture.output 因此:

Two solutions: one is to rewrite your function to construct the output as a string using paste and sprintf or other string formatting functions, or as a quick hack just wrap it in capture.output thus:

m2l = function(matr){capture.output(matrix2latex(matr))}

然后在你的 .Rmd 文件中:

Then in your .Rmd file:

egin{displaymath}
mathbf{X} = `r m2l(x)`
end{displaymath}

变成

mathbf{X} = egin{bmatrix} , 0.06099 & 0.768 \ , 0.6112 & 0.004696 \ , 0.02729 & 0.6198 \ , 0.8498 & 0.3308 \ , 0.6869 & 0.103 \ , end{bmatrix}

虽然不是很完美,但确实说明了这一原则.内联表达式插入的代码是它的值,而不是它打印的内容或猫.

which although isn't quite perfect does illustrate the principle. The code inserted by the inline expression is the value of it, not what it prints or cats.

这篇关于Knitr:Markdown 文档中 LaTeX 环境中的 R 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆