6

I'm trying to get a document publication-ready and am doing everything in LaTeX. However, I just now noticed that gtsummary doesn't yet support LaTeX output for its tables. I'm wondering what the best way is to nevertheless convert the HTML tables into LaTeX?

One idea is to a website like https://tableconvert.com/ and convert from HTML.

Two questions:

  1. How do I actually output the raw HTML code from a gt object to do this? R automatically formats the tbl_summary object, and I don't see any helper functions for getting the raw html.

  2. Is there a better way to transform the tables to LaTeX for publications that require submissions in LaTex?

Parseltongue
  • 11,157
  • 30
  • 95
  • 160

1 Answers1

6

The answer depends somewhat on whether you are using R markdown to create entire LaTeX documents, or whether you're grabbing the LaTeX code for a single table and adding it manually to a larger document.

R Markdown: {gtsummary} supports output to PDF/LaTeX via a few different engines--gt (although, technically still under development for LaTeX. for simple tables I have had no problems with gt and Latex working nicely together), flextable, huxtable, kable, and kableExtra. http://www.danieldsjoberg.com/gtsummary/dev/articles/rmarkdown.html Using one of these output options for the gtsummary table should work for you.

If you need to convert a single gtsummary table to LaTeX or HTML, you will first want to convert is to {gt} then use the functions gt::as_latex() or gt::as_raw_html(). These will aid in getting the raw HTML or Latex code you're after.

library(gtsummary)
library(tidyverse)

trial %>%
  select(age, trt) %>%
  tbl_summary() %>%
  as_gt() %>%
  gt::as_latex()

\captionsetup[table]{labelformat=empty,skip=1pt}
\begin{longtable}{lc}
\toprule
\textbf{Characteristic} & \textbf{N = 200}\textsuperscript{1} \\ 
\midrule
Age, yrs & 47 (38, 57) \\ 
Unknown & 11 \\ 
Chemotherapy Treatment &  \\ 
Drug A & 98 (49\%) \\ 
Drug B & 102 (51\%) \\ 
\bottomrule
\end{longtable}
\vspace{-5mm}
\begin{minipage}{\linewidth}
\textsuperscript{1}Statistics presented: median (IQR); n (\%) \\ 
\end{minipage}

I hope this is close to what you need/are looking for! Happy Coding!

Parseltongue
  • 11,157
  • 30
  • 95
  • 160
Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28