eip*_*i10 52 latex r cross-reference knitr r-markdown
我试图在用knitr/rmarkdown制作的PDF中交叉引用数字和表格.关于SO和tex.stackexchange(这里和这里,例如)有一些问题,建议内联的方法是添加\ref{fig:my_fig}
,my_fig
块标签在哪里.但是,当我在我的rmarkdown
文档中尝试时,我会得到??
图号应该在哪里.我想了解如何使交叉引用正常工作.
可重复的例子如下.有两个文件:rmarkdown
文件加上header.tex
我包含的文件,以防它影响答案(尽管我是否包含header.tex
文件也有同样的问题).
在该rmarkdown
文件中有三个交叉引用示例.示例1是交叉引用失败的图(??
显示而不是图号).还有第二个注释掉的尝试(基于这个SO答案),我尝试latex
在块之前和之后用标记设置图形环境,标签和标题,但是pandoc
当我尝试编织文档时这会导致错误.错误是:
Run Code Online (Sandbox Code Playgroud)! Missing $ inserted. <inserted text> $ l.108 , '%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")
```
Run Code Online (Sandbox Code Playgroud)
header.tex
文件% Caption on top
% https://tex.stackexchange.com/a/14862/4762
\usepackage{floatrow}
\floatsetup[figure]{capposition=top}
\floatsetup[table]{capposition=top}
Run Code Online (Sandbox Code Playgroud)
Yih*_*Xie 36
您可以使用输出格式bookdown::pdf_document2
代替pdf_document
,并且引用图形的语法是\@ref(fig:chunk-label)
; 有关详细信息,请参阅文档:https://bookdown.org/yihui/bookdown/figures.html
Wei*_*ong 31
以下我无法使用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)
```
Run Code Online (Sandbox Code Playgroud)
和
```{r tab2, echo=FALSE}
library(knitr)
kable(mtcars[1:3,1:4], caption="\\label{tab:tab2}A `kable` table")
```
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27330 次 |
最近记录: |