gpt4 book ai didi

r - 使用 tabularx 环境将表格从 R 导出到 LaTeX

转载 作者:行者123 更新时间:2023-12-04 12:21:38 32 4
gpt4 key购买 nike

我想使用输出中的 tabularx 环境将长表从 R 导出到 LaTeX。我只知道以一种相当hacky 的方式执行此操作,将 kableExtra 包与 gsub() 相结合,以解决 X 列的宽度。如果我不使用 gsub(),. kableExtra不定义 X 列的宽度并且 LaTeX 代码不运行。是否有任何软件包可以更无缝地执行此操作? (顺便说一句,我还想将脚注移动到“\endfoot”之前,而不是“\endlastfoot”之前。)
最小的例子

library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#>
#> group_rows

islands %>%
as_tibble(rownames = "Landmass") %>%
rename(Area = value) %>%
kbl("latex", align = c("X", "l"),
caption = "Area of World's major landmasses", label = "islands",
booktabs = TRUE, longtable = TRUE) %>%
kable_styling(latex_options = "repeat_header",
latex_table_env = "tabularx") %>%
footnote(c("Area shown in thousands of sq mi.",
"Includes landmasses with areas larger than 10,000 sq mi."),
general_title = "Notes:") %>%
gsub("\\{Xl\\}", "\\{\\\\linewidth\\}\\{Xl\\}", .) %>%
print()
#>
#> \begin{tabularx}{\linewidth}{Xl}
#> \caption{\label{tab:islands}Area of World's major landmasses}\\
#> \toprule
#> Landmass & Area\\
#> \midrule
#> \endfirsthead
#> \caption[]{Area of World's major landmasses \textit{(continued)}}\\
#> \toprule
#> Landmass & Area\\
#> \midrule
#> \endhead
#>
#> \endfoot
#> \bottomrule
#> \multicolumn{2}{l}{\rule{0pt}{1em}\textit{Notes:}}\\
#> \multicolumn{2}{l}{\rule{0pt}{1em}Area shown in thousands of sq mi.}\\
#> \multicolumn{2}{l}{\rule{0pt}{1em}Includes landmasses with areas larger than 10,000 sq mi.}\\
#> \endlastfoot
#> Africa & 11506\\
#> Antarctica & 5500\\
#> Asia & 16988\\
#> Australia & 2968\\
#> Axel Heiberg & 16\\
#> \addlinespace
#> Baffin & 184\\
#> Banks & 23\\
#> Borneo & 280\\
#> Britain & 84\\
#> Celebes & 73\\
#> \addlinespace
#> Celon & 25\\
#> Cuba & 43\\
#> Devon & 21\\
#> Ellesmere & 82\\
#> Europe & 3745\\
#> \addlinespace
#> Greenland & 840\\
#> Hainan & 13\\
#> Hispaniola & 30\\
#> Hokkaido & 30\\
#> Honshu & 89\\
#> \addlinespace
#> Iceland & 40\\
#> Ireland & 33\\
#> Java & 49\\
#> Kyushu & 14\\
#> Luzon & 42\\
#> \addlinespace
#> Madagascar & 227\\
#> Melville & 16\\
#> Mindanao & 36\\
#> Moluccas & 29\\
#> New Britain & 15\\
#> \addlinespace
#> New Guinea & 306\\
#> New Zealand (N) & 44\\
#> New Zealand (S) & 58\\
#> Newfoundland & 43\\
#> North America & 9390\\
#> \addlinespace
#> Novaya Zemlya & 32\\
#> Prince of Wales & 13\\
#> Sakhalin & 29\\
#> South America & 6795\\
#> Southampton & 16\\
#> \addlinespace
#> Spitsbergen & 15\\
#> Sumatra & 183\\
#> Taiwan & 14\\
#> Tasmania & 26\\
#> Tierra del Fuego & 19\\
#> \addlinespace
#> Timor & 13\\
#> Vancouver & 12\\
#> Victoria & 82\\*
#> \end{tabularx}
创建于 2021-08-21 由 reprex package (v2.0.1)
latex 输出
LaTeX table

最佳答案

相信huxtable包可以让你在那里的大部分方式:

library(dplyr)
library(huxtable)

wd <- "1in"
df <- islands %>%
as_tibble(rownames = "Landmass") %>%
rename(Area = value)
ht <- huxtable(df) %>%
set_table_environment("tabularx") %>%
set_col_width(2, wd) %>%
add_footnote(paste(
"Area shown in thousands of sq mi.",
"Includes landmasses with areas larger than 10,000 sq mi."
)) %>%
set_caption("Area of World's major landmasses") %>%
set_label("islands")
print_latex(ht)

关于r - 使用 tabularx 环境将表格从 R 导出到 LaTeX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68875970/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com