knitr / rmarkdown / Latex:如何交叉引用数字和表格?

时间:2022-03-29 06:18:50

I'm trying to cross-reference figures and tables in a PDF produced with knitr/rmarkdown. There are some questions on SO and tex.stackexchange (here and here, for example), that suggest the way to do this inline is to add \ref{fig:my_fig}, where my_fig is the chunk label. However, when I try that in my rmarkdown document, I get ?? where the figure number should be. I'd like to find out how to get cross-referencing to work properly.

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

A reproducible example is below. There are two files: the rmarkdown file plus a header.tex file that I've included just in case it affects the answer (though I have the same problem whether I include the header.tex file or not).

可重现的例子如下。有两个文件:rmarkdown文件加上我已经包含的header.tex文件,以防它影响答案(尽管我是否包含header.tex文件我也有同样的问题)。

In the rmarkdown file there are three cross-reference examples. Example 1 is a figure for which cross-referencing fails (?? is displayed instead of the figure number). There's also a second, commented-out attempt (based on this SO answer), where I try setting the figure environment, label, and caption with latex markup before and after the chunk, but this results in a pandoc error when I try to knit the document. The error is:

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

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

Example 2 uses xtable and cross-referencing works. Example 3 uses kable and cross-referencing fails.

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

A screenshot of the PDF output is included at the bottom of this post.

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

rmarkdown file

---
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 file

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

PDF output

knitr / rmarkdown / Latex:如何交叉引用数字和表格?

2 个解决方案

#1


18  

You can use the output format bookdown::pdf_document2 instead of pdf_document, and the syntax for referencing a figure is \@ref(fig:chunk-label); see the documentation for details: https://bookdown.org/yihui/bookdown/figures.html

您可以使用输出格式bookdown :: pdf_document2而不是pdf_document,引用图形的语法是\ @ref(图:chunk-label);有关详细信息,请参阅文档:https://bookdown.org/yihui/bookdown/figures.html

#2


15  

Following I can't generate \label{fig:mwe-plot} with knitr, adding \label{...} to the caption arguments will produce labels in the underlying tex file, i.e.

以下我无法使用knitr生成\ label {fig:mwe-plot},在标题参数中添加\ label {...}将在底层tex文件中生成标签,即

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

and

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

#1


18  

You can use the output format bookdown::pdf_document2 instead of pdf_document, and the syntax for referencing a figure is \@ref(fig:chunk-label); see the documentation for details: https://bookdown.org/yihui/bookdown/figures.html

您可以使用输出格式bookdown :: pdf_document2而不是pdf_document,引用图形的语法是\ @ref(图:chunk-label);有关详细信息,请参阅文档:https://bookdown.org/yihui/bookdown/figures.html

#2


15  

Following I can't generate \label{fig:mwe-plot} with knitr, adding \label{...} to the caption arguments will produce labels in the underlying tex file, i.e.

以下我无法使用knitr生成\ label {fig:mwe-plot},在标题参数中添加\ label {...}将在底层tex文件中生成标签,即

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

and

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