Knitr: R code within LaTeX environment in a Markdown document

二次信任 提交于 2019-12-02 21:07:54

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.

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))}

Then in your .Rmd file:

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

becomes

\mathbf{X} = \begin{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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!