rmarkdown 中未创建灵活标签

Flextable label not created in rmarkdown

我正在使用 flextable 为使用 rmarkdwown 呈现的 pdf 文档生成表格。我想要 cross-reference 表格,但它们不生成交叉引用工作所需的标签。

我的最小可重现示例

下面是文件的内容test.Rmd:

---
title: "document title"
author: "author here"
date: "2022-01-20"
output: 
  pdf_document: 
    fig_caption: yes
    includes:
      in_header: header.tex
    number_sections: yes
    toc: yes
    toc_depth: 4
    latex_engine: xelatex
    keep_tex: true
---

```{r, include = FALSE}
library(flextable)
library(bookdown)


opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width = "85%", fig.align = "center")

```

```{r, tab.id = "anyTable", tab.cap = "Invented data", tab.lp = "tab:"}

a = data.frame(id = LETTERS[1:3], x = 1:3)

flextable(a) |> theme_vanilla()
```

```{r secondTable}

b = data.frame(id = LETTERS[4:6], x = 4:6)

flextable(b) |> theme_vanilla() |> set_caption(caption = "This is secondTable")
```

# Standard rmarkdown crossreference

Trying anyTable: \ref{tab:anyTable}.

Trying secondTable: \ref{tab:secondTable}.

# Bookdown crossreference

Trying anyTable: \@ref(tab:anyTable).

Trying secondTable: \@ref(tab:secondTable).

header.tex 的内容:

\usepackage{caption}
\renewcommand{\contentsname}{Contenidos}
\captionsetup[table]{name=Tabla}
\captionsetup[figure]{name=Figura}
%\usepackage{float} #use the 'float' package
%\floatplacement{figure}{H}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{xcolor}
\usepackage{multicol}
\usepackage{pdflscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

当我执行 rmarkdown::render("test.Rmd", output_file = "test.pdf") 时,文件以 pdf 格式呈现,但出现以下警告:

Output created: test.pdf
Warning message:
LaTeX Warning: Reference `tab:anyTable' on page 1 undefined on input line 198.
LaTeX Warning: Reference `tab:secondTable' on page 1 undefined on input line 20
0.
LaTeX Warning: There were undefined references. 

如果我检查 .tex 文件,我可以看到标题在那里,但没有标签。中间test.tex文件的相关行:

第 106 至 109 行

\begin{longtable}[c]{|p{0.75in}|p{0.75in}}

\caption{Invented data
}\

第 153 至 156 行

\begin{longtable}[c]{|p{0.75in}|p{0.75in}}

\caption{This is secondTable
}\

我希望 tex 文件中的标题行类似于 \caption{This is secondTable}\label{tab:secondTable}\

我试过的

我做错了什么?

如何cross-reference flextable 生成的表格? 或者,我对您可能建议的其他软件包持开放态度。由于我真实数据中的一些headers有些复杂(multi-span headers等),我想避开kablekableExtra如果可能的话。

您需要使用 bookdown 中的格式来获取交叉引用,请参阅 https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html

---
title: "document title"
author: "author here"
date: "2022-01-20"
output: 
  bookdown::pdf_document2: 
    fig_caption: yes
    number_sections: yes
    toc: yes
    toc_depth: 4
    latex_engine: xelatex
    keep_tex: true
---

```{r, include = FALSE}
library(flextable)
library(knitr)
library(bookdown)

opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width = "85%", fig.align = "center")
```

```{r, tab.id = "anyTable", tab.cap = "Invented data"}
a = data.frame(id = LETTERS[1:3], x = 1:3)
flextable(a) |> theme_vanilla()
```

```{r secondTable}
b = data.frame(id = LETTERS[4:6], x = 4:6)
flextable(b) |> theme_vanilla() |> set_caption(caption = "This is secondTable")
```


# Bookdown crossreference

Trying anyTable: \@ref(tab:anyTable).

Trying secondTable: \@ref(tab:secondTable).