我正在尝试交叉引用使用knitr/rmarkdown 生成的PDF 中的图形和表格。关于 SO 和 tex.stackexchange 有一些问题(here and here,例如),建议内联执行此操作的方法是添加\ref{fig:my_fig}
, where my_fig
是块标签。然而,当我在我的rmarkdown
文档,我得到??
图号应该在哪里。我想了解如何使交叉引用正常工作。
下面是一个可重现的示例。有两个文件:rmarkdown
文件加一个header.tex
我包含的文件以防万一它影响答案(尽管我是否包含相同的问题header.tex
文件或不)。
In the rmarkdown
文件中有三个交叉引用示例。示例 1 是交叉引用失败的图(??
显示而不是图号)。还有第二个已注释掉的尝试(基于这个答案),我尝试设置图形环境、标签和标题latex
块之前和之后的标记,但这会导致pandoc
当我尝试编织文档时出错。错误是:
! Missing $ inserted.
<inserted text>
$
l.108 ![](testCrossRef_
实施例2使用xtable
和交叉引用作品。实施例3用途kable
并且交叉引用失败。
本文底部包含 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输出