rmodelsummary

`modelsummary` caption is at the top


I have the opposite problem to the one discussed here, in that for me the caption consistently appears above the table and not underneath. I have used the MWE provided here and below, but the caption remains on top.

---
title: Cross-references with `Rmarkdown`, `bookdown` and `modelsummary`
output: 
  bookdown::pdf_document2:
    toc: false
---

The `bookdown` package allows you to compile PDF documents with table numbers and cross references. To do this, you must include a title in your table, use the `\@ref{tab:name}` construct, and give a name to the `R` chunk where `modelsummary` is called. 

For example, see Table \@ref(tab:test).

```{r}
library(modelsummary)
mod <- lm(hp ~ mpg, mtcars)
modelsummary(mod, title = '(#tab:test) This title describes the content of this table.')
```

I prefer captions underneath - is there a way to change this? The bootstrap package does not seem to have an option for this.


Solution

  • Drawing on this conversation, I was able to resolve the issue on the author's GitHub page as follows:

        ---
        output: 
            pdf_document:
        header-includes:
          - \usepackage{tabularray}
        ---
        
        ```{=latex}
        \DefTblrTemplate{firsthead,middlehead,lasthead}{default}{}
        \DefTblrTemplate{firstfoot,middlefoot}{default}{}
        \DefTblrTemplate{lastfoot}{default}% 
        {
          \UseTblrTemplate{caption}{default}
        }
        ```
        
        ```{r, echo=FALSE}
        library(modelsummary)
        library(tinytable)
        mod <- list()
        mod[['One variable']] <- lm(mpg ~ hp, mtcars)
        mod[['Two variables']] <- lm(mpg ~ hp + drat, mtcars)
        
        modelsummary(mod,
                      title = "Regression Models")|>
          style_tt(tabularray_outer = "label={tblr:test}")
        ```
        
        Table \ref{tblr:test}