Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

r - knitr/rmarkdown/Latex: How to cross-reference figures and tables?

I'm trying to cross-reference figures and tables in a PDF produced with knitr/rmarkdown. There are some questions on SO and tex.stackexchange (here and here, for example), that suggest the way to do this inline is to add ef{fig:my_fig}, where my_fig is the chunk label. However, when I try that in my rmarkdown document, I get ?? where the figure number should be. I'd like to find out how to get cross-referencing to work properly.

A reproducible example is below. There are two files: the rmarkdown file plus a header.tex file that I've included just in case it affects the answer (though I have the same problem whether I include the header.tex file or not).

In the rmarkdown file there are three cross-reference examples. Example 1 is a figure for which cross-referencing fails (?? is displayed instead of the figure number). There's also a second, commented-out attempt (based on this SO answer), where I try setting the figure environment, label, and caption with latex markup before and after the chunk, but this results in a pandoc error when I try to knit the document. The error is:

! Missing $ inserted.
<inserted text> 
                $
l.108 ![](testCrossRef_

Example 2 uses xtable and cross-referencing works. Example 3 uses kable and cross-referencing fails.

A screenshot of the PDF output is included at the bottom of this post.

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 
ef{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 
ef{fig:fig2}. -->

<!-- egin{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 
ef{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 
ef{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 output

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the output format bookdown::pdf_document2 instead of pdf_document, and the syntax for referencing a figure is @ref(fig:chunk-label); see the documentation for details: https://bookdown.org/yihui/bookdown/figures.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...