Knitr内联块选项(无评估)或仅呈现突出显示的代码 [英] Knitr inline chunk options (no evaluation) or just render highlighted code

查看:61
本文介绍了Knitr内联块选项(无评估)或仅呈现突出显示的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到有关是否可以为knitr中的内联块指定选项的信息.我只是尝试指定它们,就像在常规块中一样,但这给出了一个错误.

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.

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

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. 

此特定语法提供:

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

推荐答案

感谢 Yihui you可以做

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

这篇关于Knitr内联块选项(无评估)或仅呈现突出显示的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆