Reputation: 597
I want to export a long table from R to LaTeX using the tabularx environment in the output. I only know to do this in a rather hacky manner, combining the kableExtra package with gsub(), to address the width of the X column. If I don't use gsub(),. kableExtra doesn't define the width of the X column and the LaTeX code doesn't run. Is there any package to do this more seamlessly? (Incidentally, I also would like to move the footnotes before "\endfoot", rather than before "\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}
Created on 2021-08-21 by the reprex package (v2.0.1)
Upvotes: 4
Views: 418
Reputation: 1289
I believe that the huxtable
package can get you most of the way there:
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)
Upvotes: 4