Knitr inline chunk options (no evaluation) or just render highlighted code

蓝咒 提交于 2019-12-18 04:01:10

问题


I cannot find information on whether it is possible to specify options for inline chunks in knitr. I've just tried specifying them, as in the regular chunk, but this gives an error.

What I need is to include R code with highlighting in a PDF, but without evaluating it. This can only happen with inline chunks due to the format of the context. Or perhaps there is another way to include highlighted code.

To provide an example, I need something in the lines of:

Some text about something with `r eval=FALSE 1+1` inside the sentence. 

This particular syntax gives:

Error in parse(text = code, keep.source = FALSE) :
<text>:1:11: unexpected ','
1: eval=FALSE,

回答1:


Thanks to Yihui you can do,

\documentclass{article} 
<<setup, include=FALSE>>= 
knit_hooks$set(inline = function(x) { 
  if (is.numeric(x)) return(knitr:::format_sci(x, 'latex')) 
  knitr:::hi_latex(x) 
}) 
@ 
\begin{document} 

the value of $\pi$ is \Sexpr{pi}, and the function to read a table is 
\Sexpr{'read.table()'}. 

<<test2>>= 
rnorm(10) 
@ 
\end{document} 


来源:https://stackoverflow.com/questions/16405536/knitr-inline-chunk-options-no-evaluation-or-just-render-highlighted-code

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