![](/img/trans.png)
[英]Cross-referencing tables {SEQ Table \\* arabic} and figures {SEQ Figure \\* arabic} with officedown [i.e. block_caption() and run_autonum()]
[英]Cross-referencing a table or figure in rmarkdown in the caption of another figure or table
我正在制作一个 rmarkdown 文档,编织到 PDF 并有一个图(图 1)和一个表(表 1),其中表更详细地解释了该图。 我没有问题给他们中的任何一个提供标准标题,但我想将表格标题更改为“图 1 的解释”。 有什么办法吗?
下面列出了代码块,如果我需要提供更多信息,请告诉我:
YAML:
- \usepackage{caption} #and several others
output:
bookdown::pdf_document2:
keep_tex: no
latex_engine: xelatex
代码块:图 1:
```{r figure-1, fig.cap="Figure"}
ggplot()
```
表格1:
```{r table, fig.cap="Explanation of Figure \@ref(fig:figure-1)"}
knitr
kableExtra::kable(caption = "Explanation of Figure \@ref(fig:figure-1)")
```
带有一个反斜杠的主要错误消息是“错误:‘@’是字符串中无法识别的转义符”,并暗示我忘记引用字符选项,这是不正确的。
使用两个反斜杠,文档编织但会生成标题“图 reffig 的解释:表”
3 个反斜杠:与 1 相同的错误。
4 个反斜杠:错误是“pandoc-citeproc:未找到引用 ref。:Package 标题错误。\caption outside float。”
感谢任何建议!
只是一种解决方法,但可能会有所帮助。 \\@ref(fig:xxx)
选项在编织到html_document2
时效果很好。 对我来说 pdf - 在终端中使用pandoc
时创建工作正常。 例如:
---
title: "Cross ref"
output:
bookdown::html_document2:
collapsed: no
theme: readable
toc: yes
link-citations: yes
---
```{r firstplot, fig.cap = "A plot with points." }
library(ggplot2)
plot_A = ggplot(data = data.frame(x = c(1:10),
y = seq(3, 8, length.out = 10)),
aes(x = x, y =y))+
geom_point()
plot_A
```
Now a second plot with a reference to Fig.: \@ref(fig:firstplot).
```{r secondplot, fig.cap = "This is the same as Fig.: \\@ref(fig:firstplot)
but now with a red line." }
library(ggplot2)
plot_A + geom_line(alpha = .75,col = "red")
```
编织后只需移动到包含 html 并使用pandoc
的文件夹
pandoc mini_ex-crossref.html -o mini_ex.pdf
我尝试了许多不同的方法文本引用、块标题、kable function 中的标题参数,我确信某处有一个聪明的解决方案,所以这里只是一个纯 Latex 的解决方法。
在带有图的块之前添加一个带有label
的 latex 块:
```{=latex}
\begin{figure}
\caption{Figure 1}
\label{Fig-1}
```
```{r figure-1, echo = FALSE}
ggplot(mtcars) +
geom_point(aes(cyl, gear))
```
```{=latex}
\end{figure}
```
现在您可以参考您的乳胶标题中的Fig-1
以获取具有正常 latex 代码\ref{Fig-1}
的表格:
```{=latex}
\begin{table}
\caption{Explanation of Figure \ref{Fig-1}}
```
```{r table}
kableExtra::kable(x = mtcars)
```
```{=latex}
\end{table}
```
注意: * 在我看来,这只是一种解决方法。 * 不能同时使用块选项fig.cap = ""
和 latex 代码
J_F 引用了 Yihui Xie 关于在 RMarkdown 中使用文本引用的精彩解释( https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#text-references ),您可以将其用于图形和表格标题需要比纯文本更复杂的东西(例如,格式化、交叉引用等)。 这可能是一个比记住在 Robert 的回答中转义反斜杠更灵活的解决方案,并且不需要使用 LaTeX 的解决方法。
正如 Yihui 解释的那样,您需要做的就是在 markdown 中的一行中定义一个文本引用,并在knitr::kable()
的块选项“fig.cap”或“caption”参数中引用它。 请注意确保每个文本引用都是一个不以空格结尾的段落。
这是一个基本示例。
---
title: "Cross-referencing figures and tables within captions."
output: bookdown::pdf_document2
editor_options:
chunk_output_type: console
---
```{r load-packages}
library(knitr)
library(flextable)
```
(ref:first-fig-caption) Here's a complicated figure caption for the first figure, which can include complicated text styling like $m^2$ and references to other elements in the document, like Table \@ref(tab:mtcars) or Fig. \@ref(fig:cars).
```{r pressure, fig.cap = '(ref:first-fig-caption)'}
plot(pressure)
```
(ref:second-fig-caption) Here's a second complicated figure caption, also referencing Table \@ref(tab:mtcars).
```{r cars, fig.cap = '(ref:second-fig-caption)'}
plot(cars)
```
(ref:caption-table1) A caption for the first table. Check out this cross reference to Fig. \@ref(fig:pressure).
```{r mtcars}
mtcars |>
head() |>
kable(caption = '(ref:caption-table1)')
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.