【问题标题】:Dynamic LaTeX references in R comment with knitrR 注释中的动态 LaTeX 引用与 knitr
【发布时间】:2012-12-27 17:41:59
【问题描述】:

我曾就SweaveDynamic references to figures in a R comment within Sweave document ) 并想看看在使用knitr 时是否有人作为类似的答案。

目标是拥有以下代码块

<<"example", fig.cap = "some figure", highlight = FALSE>>=
# the following code generated Figure \ref{fig:example}
plot(1:10, 1:10)
@

已在生成的 .pdf 中显示为

# the following code generated Figure 1.1
plot(1:10, 1:10)

到目前为止,我发现通过设置highlight = FALSE,R 代码被放置在生成的 .tex 文件中的verbatim 环境中。如果环境可以是alltt 而不是verbatim,那么我们就会得到想要的输出。是否可以通过knitr 选项将未突出显示的代码块放置在alltt 环境中?

【问题讨论】:

标签: r latex knitr


【解决方案1】:

我在knitr-examples 存储库中添加了一个示例072-latex-reference.Rnw。基本思想是恢复转义的\ref{}(在默认输出中应该是\textbackslash{}ref\{\})。

【讨论】:

  • 将正则表达式更改为gsub('\\\\textbackslash\\{\\}(\\w?)ref\\\\\\{(.+)\\\\\\}', '\\\\\\1ref{\\2}', txt) 将兼容性扩展到c/.../vref 样式引用(希望没有讨厌的副作用)。
  • 而且 - (.+) 应该是非贪婪的 (.+?),否则它仅适用于每个块的单个引用。 txt = gsub('\\\\textbackslash\\{\\}(\\w?)ref\\\\\\{(.+?)\\\\\\}', '\\\\\\1ref{\\2}', txt)(我想)
猜你喜欢
  • 1970-01-01
  • 2016-09-03
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
相关资源
最近更新 更多