rmarkdown是否允许对代码块使用标题和引用? [英] Does rmarkdown allow captions and references for code chunks?

查看:381
本文介绍了rmarkdown是否允许对代码块使用标题和引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rmarkdown中是否有技巧为字幕和引用代码块(不是运行代码的结果)?例如,如何引用此代码块:

Is there a trick for captioning and referencing a chuck of code in rmarkdown (not the result of running the code)? For example, how do I reference this block of code:

```{r blah}
blah <- "blah"
```

我知道我可以使用\ @ref(fig:theFig)或\ @ref(tab:theTable)来获取fig.cap或标题(带有kable),但是我看不到标题和引用的方法代码本身.

I know that I can use \@ref(fig:theFig) or \@ref(tab:theTable) to get at fig.cap or caption (with kable) but I don't see a way to caption and reference the code itself.

推荐答案

这将是一个很棒的功能.而且我认为它尚未集成.这是一种适用于PDF文档的方法,可让您生成标题以及用于交叉引用的标签:

This would be a great feature. And I think it is not integrated yet. Here is an approach that works for PDF documents and allows you to generate captions as well as labels for cross-referencing:

  1. 包括具有以下内容的header.tex


\usepackage{caption}
\usepackage{floatrow}

\DeclareNewFloatType{chunk}{placement=H, fileext=chk, name=}
\captionsetup{options=chunk}
\renewcommand{\thechunk}{Chunk~\thesection.\arabic{chunk}}
\makeatletter
\@addtoreset{chunk}{section}
\makeatother

由于用于块的环境不是浮动类型,因此我们声明一个新的名称为chunk的环境.选项name留为空白,因为我们已经在下一行中通过添加单词 Chunk 重新定义了\thechunk(使用name选项,看看会发生什么).

Since the environment used for chunks is not a floating type we declare a new one, named chunk. The option name is left blank since we already redefine \thechunk in the next line by prepending the word Chunk (play with the name option and see what happens).

应该按部分枚举块,因此,每次新的部分开始时,我们告诉tex重置计数器.

The chunks should be enumerated by section and so we tell tex to reset the counter each time a new section begins.

如果您不使用节编号(通过设置YAML选项),则替换行

If you do not use section numbering (by setting the YAML option), then replace the line

\renewcommand{\thechunk}{Chunk~\thesection.\arabic{chunk}}

作者

\renewcommand{\thechunk}{Chunk~\arabic{chunk}}

  1. 在rmarkdown文档中修改knitr源挂钩


library(knitr)
oldSource <- knit_hooks$get("source")
knit_hooks$set(source = function(x, options) {
  x <- oldSource(x, options)
  x <- ifelse(!is.null(options$ref), paste0("\\label{", options$ref,"}", x), x)
  ifelse(!is.null(options$codecap), paste0("\\captionof{chunk}{", options$codecap,"}", x), x)
})

在这里,我们使用了两个新的块选项refcodecap.如果不是NULL之一,则使用命令\label\captionof生成相应的标签或标题.

Here we make use of two new chunk options ref and codecap. If either one is not NULL, a corresponding label or caption is generated using the the commands \label or \captionof.

MWE:

---
title: "Cross-referencing Code Chunks"
output: 
  pdf_document: 
    includes:
      in_header: header.tex
    number_sections: true
---

```{r, echo=FALSE}
library(knitr)
oldSource <- knit_hooks$get("source")
knit_hooks$set(source = function(x, options) {
  x <- oldSource(x, options)
  x <- ifelse(!is.null(options$ref), paste0("\\label{", options$ref,"}", x), x)
  ifelse(!is.null(options$codecap), paste0("\\captionof{chunk}{", options$codecap,"}", x), x)
})
```

# Foo

Jump to \ref{TheBarChunk}

```{r Foo, ref = "TheFooChunk", codecap = "My Chunk"}
print("Foo!")
```

\newpage

# Bar

```{r Bar, ref = "TheBarChunk", codecap = "My second chunk"}
print("Bar!")
```

Head back to \ref{TheFooChunk}

以下是(两个页面的)输出:

Here is the output (of both pages):

如果您希望在代码块的下方下方添加标题,而不是上面的 ,则可以通过将上面的knitr钩子更改为以下内容来实现:

If you'd prefer to add the caption below the code chunk, instead of above, this can be achieved by altering the above knitr hook to read:

oldSource <- knit_hooks$get("source")
knit_hooks$set(source = function(x, options) {
  x <- oldSource(x, options)
  x <- ifelse(!is.null(options$codecap), paste0(x, "\\captionof{chunk}{", options$codecap,"}"), x)
  ifelse(!is.null(options$ref), paste0(x, "\\label{", options$ref,"}"), x)
})

评论:

您可以通过检查所需的输出类型来改进代码,以便新的源代码挂钩仅用于pdf输出(由于是午餐时间,因此已跳过).

You could improve the code by checking what output type is desired so that the new source hook is only used for pdf output (skipped since it is lunch time).

这篇关于rmarkdown是否允许对代码块使用标题和引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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