Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for tabularx and xltabular #2138

Merged
merged 13 commits into from
Jun 29, 2022
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Authors@R: c(
person("Adam", "Vogt", role = "ctb"),
person("Alastair", "Andrew", role = "ctb"),
person("Alex", "Zvoleff", role = "ctb"),
person("Amar", "Al-Zubaidi", role = "ctb"),
person("Andre", "Simon", role = "ctb", comment = "the CSS files under inst/themes/ were derived from the Highlight package http://www.andre-simon.de"),
person("Aron", "Atkins", role = "ctb"),
person("Aaron", "Wolen", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Per suggestion of @jakubkaczor (#2116) and discussion with @pedropark99 (#2140), the chunk option `fig.sep` can also be used to add LaTeX code before the first sub-figure now. Previously this option can only be used for adding LaTeX code *after* each sub-figure.

- `knitr::kable()` supports `tabularx` and `xltabular` environments now for LaTeX tables, e.g., `knitr::kable(head(iris), format = 'latex', tabular = 'tabularx')` (thanks, @amarakon, #2138).

## MINOR CHANGES

- When the inline R code cannot be correctly parsed, the error message will show the original code in addition to the parsing error, which can make it easier to identify the code error in the source document (thanks, @AlbertLei, #2141).
Expand Down
13 changes: 7 additions & 6 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ knit_print.knitr_kable = function(x, ...) {
}

kable_latex = function(
x, booktabs = FALSE, longtable = FALSE, valign = 't', position = '', centering = TRUE,
x, booktabs = FALSE, longtable = FALSE, tabular = if (longtable) 'longtable' else 'tabular',
valign = if (tabular %in% c('tabularx', 'xltabular')) '{\\linewidth}' else '[t]',
position = '', centering = TRUE,
vline = getOption('knitr.table.vline', if (booktabs) '' else '|'),
toprule = getOption('knitr.table.toprule', if (booktabs) '\\toprule' else '\\hline'),
bottomrule = getOption('knitr.table.bottomrule', if (booktabs) '\\bottomrule' else '\\hline'),
Expand All @@ -291,7 +293,7 @@ kable_latex = function(
# vertical align only if 'caption' is not NULL (may be NA) or 'valign' has
# been explicitly specified
valign = if ((!is.null(caption) || !missing(valign)) && valign != '') {
sprintf('[%s]', valign)
if (grepl('^[[{]', valign)) valign else sprintf('[%s]', valign)
} else ''
if (identical(caption, NA)) caption = NULL
if (position != '') position = paste0('[', position, ']')
Expand All @@ -310,12 +312,11 @@ kable_latex = function(
x = escape_latex_table(x, escape, booktabs)
if (!is.character(toprule)) toprule = NULL
if (!is.character(bottomrule)) bottomrule = NULL
tabular = if (longtable) 'longtable' else 'tabular'

paste(c(
if (!longtable) c(env1, cap, centering),
if (cap_env <- !tabular %in% c('longtable', 'xltabular')) c(env1, cap, centering),
sprintf('\n\\begin{%s}%s', tabular, valign), align,
if (longtable && cap != '') c(cap, '\\\\'),
if (!cap_env && cap != '') c(cap, '\\\\'),
sprintf('\n%s', toprule), '\n',
if (!is.null(cn <- colnames(x))) {
cn = escape_latex_table(cn, escape, booktabs)
Expand All @@ -324,7 +325,7 @@ kable_latex = function(
one_string(apply(x, 1, paste, collapse = ' & '), sprintf('\\\\%s', linesep), sep = ''),
sprintf('\n%s', bottomrule),
sprintf('\n\\end{%s}', tabular),
if (!longtable) env2
if (cap_env) env2
), collapse = '')
}

Expand Down