【问题标题】:Knitr inline chunk options (no evaluation) or just render highlighted codeKnitr 内联块选项(无评估)或仅呈现突出显示的代码
【发布时间】:2013-05-06 19:11:23
【问题描述】:

我找不到有关是否可以在 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,

【问题讨论】:

  • eval = FALSE, echo = TRUE 做你想做的事
  • @TylerRinker 不幸的是没有。我已经编辑了这个问题,以便更清楚地了解我需要什么。
  • partial duplicate(高亮部分)

标签: r syntax-highlighting knitr


【解决方案1】:

感谢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')) 
  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} 

【讨论】:

  • +1。你能想象这在 Markdown 中会是什么样子吗?设置块不是问题,而是函数的内容(乳胶和 hi_latex 部分)
  • 易慧recently hintedmarkdown需要一个小技巧。
  • 谢谢。我想我将不得不在没有突出显示的情况下生活。以防万一,我暂时将问题留待解决。
  • 应该是highr::hi_latex
猜你喜欢
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 2015-07-06
  • 2014-07-18
  • 2017-03-28
相关资源
最近更新 更多