Search code examples
rr-markdownkablekableextra

2-column LaTeX environment table* in kable, kableExtra


I would like include a wider table in a two column LaTeX article. This could be achieved in LaTeX using \begin{table*} ... \end{table*} instead of \begin{table} ... \end{table}. How can I tell the R packages kable or kableExtra to achieve this?

The following produced the usual \begin{table}:

library(kableExtra)
kable(head(cars, 1), format = "latex") %>% 
  kable_styling()

which produces:

#> \begin{table}[H]
#> \centering
#> \begin{tabular}{r|r}
#> \hline
#> speed & dist\\
#> \hline
#> 4 & 2\\
#> \hline
#> \end{tabular}
#> \end{table}

But what I want instead is the following:

#> \begin{table*}[H]
#> \centering
#> \begin{tabular}{r|r}
#> \hline
#> speed & dist\\
#> \hline
#> 4 & 2\\
#> \hline
#> \end{tabular}
#> \end{table*}

I am aware that I could use gsub to hack the output, but wonder if there is way to do it cleanly.

Created on 2018-05-05 by the reprex package (v0.2.0).


Solution

  • I was struggling with the same issue and even added a feature request to knitr for this, before Yihui Xie told me that it is already possible to do:

    Simply add table.env='table*' as an argument to kable:

    knitr::kable(head(cars,1), format = "latex", table.env='table*')
    

    will produce

    \begin{table*}
    
      \begin{tabular}{r|r}
        \hline
        speed & dist\\
        \hline
        4 & 2\\
        \hline
      \end{tabular}
    
    \end{table*}