4

我正在尝试准备好文档发布,并在 LaTeX 中做所有事情。但是,我刚刚注意到gtsummary它的表格还不支持 LaTeX 输出。我想知道将 HTML 表格转换为 LaTeX 的最佳方法是什么?

一个想法是到像https://tableconvert.com/这样的网站并从 HTML 转换。

两个问题:

  1. 我如何从gt对象中实际输出原始 HTML 代码来执行此操作?R 自动格式化 tbl_summary 对象,我没有看到任何用于获取原始 html 的辅助函数。

  2. 对于需要在 LaTex 中提交的出版物,是否有更好的方法将表格转换为 LaTeX?

4

1 回答 1

3

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!

于 2020-05-20T19:08:23.783 回答