Search code examples
rlatexgtsummarygt

How to translate SE below coefficient table to latex in gtsummary


In the below example, the html carriage returns stick around when converting to LaTeX. Is there a way in gt/gtsummary to get return characters that are correct in LaTeX?

library(gtsummary)
tbl <-
  lm(time ~ ph.ecog + sex, survival::lung) %>%
  tbl_regression(label = list(ph.ecog = "ECOG Score", sex = "Sex"))

add_significance_stars_ex3 <-
  tbl %>%
  add_significance_stars(
    hide_se = TRUE,
    pattern = "{estimate}{stars}<br>({std.error})"
  ) %>%
  modify_header(estimate ~ "**Beta (SE)**") %>%
  modify_footnote(estimate ~ "SE = Standard Error", abbreviation = TRUE) %>%
  as_gt() %>%
  gt::tab_style(
    style = "vertical-align:top",
    locations = gt::cells_body(columns = label)
  )
add_significance_stars_ex3 %>% 
  gt::as_latex() %>% 
  cat()
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \setlength{\LTpost}{0mm}
#> \begin{longtable}{lc}
#> \toprule
#> \textbf{Characteristic} & \textbf{Beta (SE)}\textsuperscript{1,2} \\ 
#> \midrule
#> ECOG Score & -58**<br>(19.1) \\ 
#> Sex & 52<br>(27.9) \\ 
#> \bottomrule
#> \end{longtable}
#> \begin{minipage}{\linewidth}
#> \textsuperscript{1}*p\textless{}0.05; **p\textless{}0.01; ***p\textless{}0.001\\
#> \textsuperscript{2}SE = Standard Error\\
#> \end{minipage}

Created on 2022-08-15 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.1 (2022-06-23)
#>  os       Ubuntu 22.04.1 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en_NZ:en
#>  collate  en_NZ.UTF-8
#>  ctype    en_NZ.UTF-8
#>  tz       Pacific/Auckland
#>  date     2022-08-15
#>  pandoc   2.18 @ /usr/lib/rstudio/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package       * version date (UTC) lib source
#>  assertthat      0.2.1   2019-03-21 [3] RSPM (R 4.2.0)
#>  backports       1.4.1   2021-12-13 [3] RSPM (R 4.2.0)
#>  broom           1.0.0   2022-07-01 [3] RSPM (R 4.2.0)
#>  broom.helpers   1.8.0   2022-07-05 [3] RSPM (R 4.2.0)
#>  checkmate       2.1.0   2022-04-21 [3] RSPM (R 4.2.0)
#>  cli             3.3.0   2022-04-25 [3] RSPM (R 4.2.0)
#>  colorspace      2.0-3   2022-02-21 [3] RSPM (R 4.2.0)
#>  commonmark      1.8.0   2022-03-09 [3] RSPM (R 4.2.0)
#>  crayon          1.5.1   2022-03-26 [3] RSPM (R 4.2.0)
#>  DBI             1.1.3   2022-06-18 [3] RSPM (R 4.2.0)
#>  digest          0.6.29  2021-12-01 [3] RSPM (R 4.2.0)
#>  dplyr           1.0.9   2022-04-28 [3] RSPM (R 4.2.0)
#>  ellipsis        0.3.2   2021-04-29 [3] RSPM (R 4.2.0)
#>  evaluate        0.16    2022-08-09 [3] RSPM (R 4.2.0)
#>  fansi           1.0.3   2022-03-24 [3] RSPM (R 4.2.0)
#>  fastmap         1.1.0   2021-01-25 [3] RSPM (R 4.2.0)
#>  forcats         0.5.1   2021-01-27 [3] RSPM (R 4.2.0)
#>  fs              1.5.2   2021-12-08 [3] RSPM (R 4.2.0)
#>  generics        0.1.3   2022-07-05 [3] RSPM (R 4.2.0)
#>  ggplot2         3.3.6   2022-05-03 [3] RSPM (R 4.2.0)
#>  glue            1.6.2   2022-02-24 [3] RSPM (R 4.2.0)
#>  gt              0.6.0   2022-05-24 [3] RSPM (R 4.2.0)
#>  gtable          0.3.0   2019-03-25 [3] RSPM (R 4.2.0)
#>  gtsummary     * 1.6.1   2022-06-22 [3] RSPM (R 4.2.0)
#>  haven           2.5.0   2022-04-15 [3] RSPM (R 4.2.0)
#>  highr           0.9     2021-04-16 [3] RSPM (R 4.2.0)
#>  hms             1.1.1   2021-09-26 [3] RSPM (R 4.2.0)
#>  htmltools       0.5.3   2022-07-18 [3] RSPM (R 4.2.0)
#>  knitr           1.39    2022-04-26 [3] RSPM (R 4.2.0)
#>  labelled        2.9.1   2022-05-05 [3] RSPM (R 4.2.0)
#>  lattice         0.20-45 2021-09-22 [4] CRAN (R 4.2.0)
#>  lifecycle       1.0.1   2021-09-24 [3] RSPM (R 4.2.0)
#>  magrittr        2.0.3   2022-03-30 [3] RSPM (R 4.2.0)
#>  Matrix          1.4-1   2022-03-23 [3] RSPM (R 4.2.0)
#>  munsell         0.5.0   2018-06-12 [3] RSPM (R 4.2.0)
#>  pillar          1.8.0   2022-07-18 [3] RSPM (R 4.2.0)
#>  pkgconfig       2.0.3   2019-09-22 [3] RSPM (R 4.2.0)
#>  purrr           0.3.4   2020-04-17 [3] RSPM (R 4.2.0)
#>  R.cache         0.16.0  2022-07-21 [3] RSPM (R 4.2.0)
#>  R.methodsS3     1.8.2   2022-06-13 [3] RSPM (R 4.2.0)
#>  R.oo            1.25.0  2022-06-12 [3] RSPM (R 4.2.0)
#>  R.utils         2.12.0  2022-06-28 [3] RSPM (R 4.2.0)
#>  R6              2.5.1   2021-08-19 [3] RSPM (R 4.2.0)
#>  reprex          2.0.1   2021-08-05 [3] RSPM (R 4.2.0)
#>  rlang           1.0.4   2022-07-12 [3] RSPM (R 4.2.0)
#>  rmarkdown       2.14    2022-04-25 [3] RSPM (R 4.2.0)
#>  rstudioapi      0.13    2020-11-12 [3] RSPM (R 4.2.0)
#>  scales          1.2.0   2022-04-13 [3] RSPM (R 4.2.0)
#>  sessioninfo     1.2.2   2021-12-06 [3] RSPM (R 4.2.0)
#>  stringi         1.7.8   2022-07-11 [3] RSPM (R 4.2.0)
#>  stringr         1.4.0   2019-02-10 [3] RSPM (R 4.2.0)
#>  styler          1.7.0   2022-03-13 [3] RSPM (R 4.2.0)
#>  survival        3.4-0   2022-08-09 [3] RSPM (R 4.2.0)
#>  tibble          3.1.8   2022-07-22 [3] RSPM (R 4.2.0)
#>  tidyr           1.2.0   2022-02-01 [3] RSPM (R 4.2.0)
#>  tidyselect      1.1.2   2022-02-21 [3] RSPM (R 4.2.0)
#>  utf8            1.2.2   2021-07-24 [3] RSPM (R 4.2.0)
#>  vctrs           0.4.1   2022-04-13 [3] RSPM (R 4.2.0)
#>  withr           2.5.0   2022-03-03 [3] RSPM (R 4.2.0)
#>  xfun            0.32    2022-08-10 [3] RSPM (R 4.2.0)
#>  yaml            2.3.5   2022-02-21 [3] RSPM (R 4.2.0)
#> 
#>  [1] /home/kendonb/R/x86_64-pc-linux-gnu-library/4.2
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Solution

  • You have specified a pattern with an HTML line break hard-coded into the cell: pattern = "{estimate}{stars}<br>({std.error})". Use a latex linebreaker here instead.

    However, I recommend to use as_kable_extra() because it's bee written to support \n as a line breaker.

    library(gtsummary)
    #> #StandWithUkraine
    
    tbl <-
      lm(time ~ ph.ecog + sex, survival::lung) %>%
      tbl_regression(label = list(ph.ecog = "ECOG Score", sex = "Sex"))
    
    add_significance_stars_ex3 <-
      tbl %>%
      add_significance_stars(
        hide_se = TRUE,
        pattern = "{estimate}{stars}  \n({std.error})"
      ) %>%
      modify_header(estimate ~ "**Beta (SE)**") %>%
      modify_footnote(estimate ~ "SE = Standard Error", abbreviation = TRUE) 
    
    add_significance_stars_ex3 %>%
      as_kable_extra(escape = FALSE, format = 'latex') %>%
      cat()
    #> 
    #> \begin{tabular}{l|c}
    #> \hline
    #> \textbf{Characteristic} & \textbf{Beta (SE)}\\
    #> \hline
    #> ECOG Score & \makecell[c]{-58**\ \ \\(19.1)}\\
    #> \hline
    #> Sex & \makecell[c]{52\ \ \\(27.9)}\\
    #> \hline
    #> \multicolumn{2}{l}{\rule{0pt}{1em}\textsuperscript{1} \textit{p<0.05; \textbf{p<0.01; }}p<0.001}\\
    #> \multicolumn{2}{l}{\rule{0pt}{1em}\textsuperscript{2} SE = Standard Error}\\
    #> \end{tabular}
    

    Created on 2022-08-15 by the reprex package (v2.0.1)