【问题标题】:knitr/rmarkdown/Latex: How to cross-reference figures and tables?knitr/rmarkdown/Latex:如何交叉引用图形和表格?
【发布时间】:2016-12-16 02:35:17
【问题描述】:

我正在尝试在使用 knitr/rmarkdown 生成的 PDF 中交叉引用图形和表格。在 SO 和 tex.stackexchange 上有一些问题(例如herehere),建议内联执行此操作的方法是添加 \ref{fig:my_fig},其中 my_fig 是块标签。但是,当我在我的 rmarkdown 文档中尝试这样做时,我得到了 ?? 数字应该在哪里。我想了解如何让交叉引用正常工作。

下面是一个可重现的示例。有两个文件:rmarkdown 文件加上一个 header.tex 文件,我已经包括在内,以防它影响答案(尽管无论我是否包括 header.tex 文件,我都有同样的问题)。

rmarkdown 文件中有三个交叉引用示例。示例 1 是交叉引用失败的图形(显示?? 而不是图形编号)。还有第二次注释掉的尝试(基于this SO answer),我尝试在块之前和之后使用latex 标记设置图形环境、标签和标题,但这会导致pandoc 错误时我试着编织文件。错误是:

! Missing $ inserted.
<inserted text> 
                $
l.108 ![](testCrossRef_

示例 2 使用 xtable 和交叉引用作品。示例 3 使用 kable 并且交叉引用失败。

本文底部包含 PDF 输出的屏幕截图。

rmarkdown文件

---
title: | 
  | My Title  
author: | 
  | eipi10  
  | Department of Redundancy Department  
date: "`r format(Sys.time(), '%B %e, %Y')`"
output: 
  pdf_document:
    fig_caption: yes
    includes:
      in_header: header.tex
    keep_tex: yes
fontsize: 11pt
geometry: margin=1in
graphics: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE, fig.height=2, fig.width=4)
```

# Example 1. Figure

This is a report. Take a look at Figure \ref{fig:fig1}.  

```{r fig1, echo=FALSE, fig.cap="This is a caption"}
plot(mtcars$wt, mtcars$mpg)
```

<!-- Now, let's take a look at this other plot in Figure \ref{fig:fig2}. -->

<!-- \begin{figure} -->
<!-- ```{r fig2, echo=FALSE} -->
<!-- plot(mtcars$cyl, mtcars$mpg) -->
<!-- ``` -->
<!-- \caption{This is another caption} -->
<!-- \label{fig:fig2} -->
<!-- \end{figure} -->

# Example 2: `xtable`

Some more text. See Table \ref{tab:tab1} below. 

```{r echo=FALSE, results="asis"}
library(xtable)
print.xtable(
  xtable(mtcars[1:3,1:4], label="tab:tab1", caption="An xtable table"), 
  comment=FALSE)
```

# Example 3: `kable`

Some more text. See Table \ref{tab:tab2} below. 

```{r tab2, echo=FALSE}
library(knitr)
kable(mtcars[1:3,1:4], caption="A `kable` table")
```

header.tex文件

% Caption on top
% https://tex.stackexchange.com/a/14862/4762
\usepackage{floatrow}
\floatsetup[figure]{capposition=top}
\floatsetup[table]{capposition=top}

PDF 输出

【问题讨论】:

标签: r latex knitr r-markdown cross-reference


【解决方案1】:

你可以试试captioner 包。您可以在this link 中找到示例。

在我的例子中,我包含了一个代码块:

table_captions <- captioner::captioner(prefix="Tab.")
figure_captions <- captioner::captioner(prefix="Fig.")

t.ref <- function(label){
  stringr::str_extract(table_captions(label), "[^:]*")
}

f.ref <- function(label){
  stringr::str_extract(figure_captions(label), "[^:]*")
}

在定义代码块时,我在表格和图形中包含标题,如下所示:

```{r chunk_creating_one_figure, echo=FALSE, fig.cap=figure_captions("one_figure", "figure label")}
plot(1)
```

```{r chunk_creating_one_table, echo=FALSE, fig.cap=table_captions("one_table", "table label")}
knitr::kable(data.frame(col="something"), format="markdown")
```

引用在我的 Rmarkdown 中都包含在 inline_text 中:

As shown in figure `r f.ref("one_figure")`
Data is shown on table `r t.ref("one_table")`

【讨论】:

    【解决方案2】:

    您可以使用输出格式bookdown::pdf_document2代替pdf_document,引用图形的语法为\@ref(fig:chunk-label);详情见文档:https://bookdown.org/yihui/bookdown/figures.html

    【讨论】:

    • 有没有办法做到这一点,它与自定义 rmarkdown 格式兼容,例如来自rticles 模板?
    • @cboettig 是的。可能我说的不够清楚:bookdown.org/yihui/bookdown/latexpdf.html 你可以将base_format 改成任何包中的任何输出格式函数。
    • 易慧的answer above 似乎不适用于bookdown::tufte_book2。对于使用 pdf_document2 正确显示的图形交叉引用显示为“??”当输出格式更改为 tufte_book2 时。
    • 仅供参考,似乎无法更改 bookdown::pdf_document2 的 base_format,但应该改用 bookdown::pdf_book...stackoverflow.com/a/52480807/576684
    • 我不确定如何在rmarkdown 文件上使用pdf_document2 格式?我只知道如何与render_book 一起使用。在YAML前面添加bookdown::pdf_document2后,RStudio中出现了“knit to pdf_document2”选项,否则我不知道从命令行如何。
    【解决方案3】:

    I can't generate \label{fig:mwe-plot} with knitr 之后,将\label{...} 添加到标题参数将在基础tex 文件中生成标签,即

    ```{r fig1, echo=FALSE, fig.cap="\\label{fig:fig1}This is a caption"}
    plot(mtcars$wt, mtcars$mpg)
    ```
    

    ```{r tab2, echo=FALSE}
    library(knitr)
    kable(mtcars[1:3,1:4], caption="\\label{tab:tab2}A `kable` table")
    ```
    

    【讨论】:

    • @Yihui,因为这是基于2-year-old answer,这仍然是最先进的,还是现在有更好的方法?
    • 现在有更好的方法。简而言之,使用bookdown::pdf_document2而不是pdf_document;见bookdown.org/yihui/bookdown/figures.html
    • 这个方案的优点是不依赖bookdown。
    猜你喜欢
    • 2019-03-03
    • 2021-08-21
    • 2019-02-26
    • 1970-01-01
    • 2019-05-31
    • 2023-02-12
    • 2023-02-01
    • 2020-01-05
    • 2015-03-16
    相关资源
    最近更新 更多