13

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

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

举个例子,我需要以下内容:

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

这种特殊的语法给出:

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

1 回答 1

5

谢一辉,你可以做到

\documentclass{article} 
<<setup, include=FALSE>>= 
knit_hooks$set(inline = function(x) { 
  if (is.numeric(x)) return(knitr:::format_sci(x, 'latex')) 
  highr::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} 
于 2013-05-06T19:49:10.413 回答