[英]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.