如何使用Rmarkdown显示带有反引号的逐字内联r代码? [英] How to display verbatim inline r code with backticks using Rmarkdown?
问题描述
通过将Markdown中的反引号加倍,可以很容易地以代码样式呈现某些文本,包括反引号,例如:`r 2+2`
.
但是如何使用 RMarkdown 做到这一点?同样,我们可以显示`t 2+2`
,但是用r
替换t
会执行R代码2+2
.
By doubling the backticks in Markdown, it is easy to render some text in code style including the backticks, such as: `r 2+2`
.
But how to do that with RMarkdown ? By the same way we can display `t 2+2`
, but replacing t
with r
executes the R code 2+2
.
到目前为止,我发现的唯一方法是:
The only way I have found so far is:
<p><code class="r">`</code><code class="r">r 2+2`</code></p>
不是很方便.也许我应该定义一个新的CSS以便更方便地进行此操作?
Not very convenient. Maybe I should define a new css for doing that more conveniently ?
推荐答案
这是我使用的一个技巧.首先,注意\x60
是`
:
Here is a trick that I use. First, note \x60
is `
:
> cat('\x60', '\n')
`
然后您写
`r '\x60r foo+bar\x60'`
在markdown输出中将为您提供`r foo+bar`
,但在HTML输出中将变为r foo+bar
,因此您需要使用两个(或多个)反引号来保护markdown中的反引号.然后,您最终遇到了这个毛发:
which will give you `r foo+bar`
in the markdown output, but that will become r foo+bar
in the HTML output, so you need to protect the backticks in markdown, using two (or more) backticks. Then you end up with this hairball:
`` `r '\x60r foo+bar\x60'` ``
您自己的解决方案很好,但我只是定义
Your own solution is good, but I'd just define
rinline <- function(code) {
sprintf('``` `r %s` ```', code)
}
另请参见这篇文章.
这篇关于如何使用Rmarkdown显示带有反引号的逐字内联r代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!