Leading space in gt::as_latex

gtsummary is a great package for creating summary tables for HTML. I find myself needing to use LaTeX though, which is supported through gt::as_latex(). It seems to work, but it removes leading space from the rows.

For instance, if you have a factor, the gtsummary table will have a row per factor level with the level label indented so that it's easily distinguished from numerical variables. When you then convert the table to latex, it removes the leading spaces. Is there a way to stop it doing that?

library(gtsummary)
#> Warning: Paket 'gtsummary' wurde unter R Version 3.6.3 erstellt
library(gt)
#> Warning: Paket 'gt' wurde unter R Version 3.6.3 erstellt

data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)

tab <- mtcars %>%
  select(cyl) %>%
  tbl_summary() %>%
  as_gt() 

tab

image


tab %>%
  as_latex() %>%
  as.character() %>%
  cat()
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \begin{longtable}{lc}
#> \toprule
#> \textbf{Characteristic} & \textbf{N = 32}\textsuperscript{1} \\ 
#> \midrule
#> cyl &  \\ 
#> 4 & 11 (34\%) \\ 
#> 6 & 7 (22\%) \\ 
#> 8 & 14 (44\%) \\ 
#> \bottomrule
#> \end{longtable}
#> \vspace{-5mm}
#> \begin{minipage}{\linewidth}
#> \textsuperscript{1}Statistics presented: n (\%) \\ 
#> \end{minipage}

Created on 2020-11-06 by the reprex package (v0.3.0)

Support for latex and PDF output in the gt package is currently under development. I think this would be resolved when it's done.

In the meantime, you could check out the other latex printers available from gtsummary. http://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html#latex-1

# build gtsummary table
tbl <- tbl_summary(trial)

# using the {gt} package
as_gt(tbl) %>% gt::as_latex()

# using the {huxtable} package
as_hux_table(tbl) %>% huxtable::to_latex()

# using the {kableExtra} package
as_kable_extra(tbl, format = "latex")

# using the knitr::kable function
as_kable(tbl, format = "latex")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.