2-column LaTeX environment table* in kable, kableExtra
Asked Answered
Z

3

5

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).

Zitella answered 5/5, 2018 at 14:29 Comment(0)
T
6

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*}
Trappist answered 7/8, 2018 at 9:58 Comment(0)
A
3

There is a latex_table_env in kable_styling

library(kableExtra)
kable(head(cars, 1), format = "latex") %>% 
  kable_styling(latex_table_env = "table*")
Anticoagulant answered 7/5, 2018 at 16:34 Comment(2)
This switches tabular to table*, and I am loosing the floating environment.Zitella
@Zitella Ah, I see.. Emm, unfortunately, right now there is no way to change that in kableExtra. You will have to do it through gsub.Anticoagulant
M
2

I don't know if this is possible with knitr, but it is easily achieved using xtable instead:

data(cars)
library(xtable)
library(magrittr)

xtable(head(cars, 1), align="rr|r") %>%
  print.xtable(
    floating.environment="table*", # use table* environment
    include.rownames=FALSE,
    table.placement="H"
  )

# % latex table generated in R 3.4.4 by xtable 1.8-2 package
# % Sat May  5 16:47:49 2018
# \begin{table*}[H]
# \centering
# \begin{tabular}{r|r}
#   \hline
# speed & dist \\ 
#   \hline
# 4.00 & 2.00 \\ 
#   \hline
# \end{tabular}
# \end{table*}
Matilda answered 5/5, 2018 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.