使用 tabularx 环境将表格从 R 导出到 LaTeX
Export tables from R to LaTeX using tabularx environment
我想在输出中使用 tabularx 环境将长 table 从 R 导出到 LaTeX。我只知道以一种相当老套的方式来做到这一点,将 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}
由 reprex package (v2.0.1)
创建于 2021-08-21
LaTeX 输出
我相信 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)
我想在输出中使用 tabularx 环境将长 table 从 R 导出到 LaTeX。我只知道以一种相当老套的方式来做到这一点,将 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}
由 reprex package (v2.0.1)
创建于 2021-08-21LaTeX 输出
我相信 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)