3

Cross-referencing a table made by texreg in bookdown halfway works. The table is numbered properly but the cross-reference ends up as '??' in the text. A MRE is below. Is there a solution to this, or is there another package that can solve this problem (stargazer has the same problem in bookdown). Using fig.cap has no effect.

Thanks for any help.

---
title: "bookdownTest"
author: "Richard Sherman"
date: "1/9/2020"
output: 
  bookdown::pdf_document2:
    fig_caption: yes
    toc: false
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r load libraries, include=FALSE}
library(texreg)
library(bookdown)
```

```{r lm, results='asis', star.symbol = "\\*", center = TRUE}
m1 <- lm(mpg ~ disp + cyl + hp, mtcars)
m2 <- lm(mpg ~ disp + hp, mtcars)
texreg(list(m1, m2), center = TRUE,
  caption="Linear model of mpg")
```

Results are in Table \@ref(tab:lm).

1 Answers1

2

texreg() has a label option that lets you set the label, so you can do:

texreg(list(m1, m2), center = TRUE,
  caption="Linear model of mpg",
  label="tab:lm")

You may have been relying on the automatic table labels described in the bookdown documentation, but that only works when using the knitr::kable() function to generate your table.

Marius
  • 58,213
  • 16
  • 107
  • 105