knitr/rmarkdown/Latex: How to cross-reference figures and tables?
<p>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 (<a href="https://tex.stackexchange.com/a/296010/4762">here</a> and <a href="https://stackoverflow.com/questions/31064353/how-to-make-a-figure-caption-in-rmarkdown/31071430#31071430">here</a>, for example), that suggest the way to do this inline is to add <code>\ref{fig:my_fig}</code>, where <code>my_fig</code> is the chunk label. However, when I try that in my <code>rmarkdown</code> document, I get <code>??</code> where the figure number should be. I'd like to find out how to get cross-referencing to work properly.  </p>

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

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

<blockquote>
<pre><code>! Missing $ inserted.
&lt;inserted text&gt; 
                $
l.108 ![](testCrossRef_
</code></pre>
</blockquote>

<p>Example 2 uses <code>xtable</code> and cross-referencing works. Example 3 uses <code>kable</code> and cross-referencing fails.</p>

<p>A screenshot of the PDF output is included at the bottom of this post. </p>

<h3><code>rmarkdown</code> file</h3>

<pre><code>---
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)
```

&lt;!-- Now, let's take a look at this other plot in Figure \ref{fig:fig2}. --&gt;

&lt;!-- \begin{figure} --&gt;
&lt;!-- ```{r fig2, echo=FALSE} --&gt;
&lt;!-- plot(mtcars$cyl, mtcars$mpg) --&gt;
&lt;!-- ``` --&gt;
&lt;!-- \caption{This is another caption} --&gt;
&lt;!-- \label{fig:fig2} --&gt;
&lt;!-- \end{figure} --&gt;

# 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")
```
</code></pre>

<h3><code>header.tex</code> file</h3>

<pre><code>% Caption on top
% https://tex.stackexchange.com/a/14862/4762
\usepackage{floatrow}
\floatsetup[figure]{capposition=top}
\floatsetup[table]{capposition=top}
</code></pre>

<h3>PDF output</h3>

<p><a href="https://i.stack.imgur.com/bIhhT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/bIhhT.png" alt="enter image description here"></a></p>