Knitr内联块选项(无评估)或仅渲染突出显示的代码

时间:2022-12-29 06:13:39

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.

我找不到有关是否可以在knitr中指定内联块的选项的信息。我只是尝试指定它们,就像在常规块中一样,但这会产生错误。

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.

我需要的是在PDF中包含突出显示的R代码​​,但不进行评估。由于上下文的格式,这只能在内联块中发生。或许还有另一种方法可以包含突出显示的代码。

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 个解决方案

#1


5  

Thanks to Yihui you can do,

感谢Yihui,你可以做到,

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

#1


5  

Thanks to Yihui you can do,

感谢Yihui,你可以做到,

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