Skip to content

Commit

Permalink
Fix issue rstudio#1465
Browse files Browse the repository at this point in the history
Replaces the use of percentage symbols to express column widths, which cause an error when compiling Latex, with column widths that are expressed as a share of `\linewidth` or the user-supplied `table_width`.
  • Loading branch information
kbrevoort committed Nov 10, 2023
1 parent a345f85 commit c3bfde7
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions R/utils_render_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,45 @@ create_table_start_l <- function(data) {
">{\\raggedright\\arraybackslash}"
)

# Check if column width was set using gt::pct and
# convert to Latex friendly terminology (i.e.,
# '14.7%' becomes '0.147\\linewidth')
if (grepl('^[[:digit:].]+%$', col_widths[i])) {

table_width <- dt_options_get_value(data = data, option = 'table_width')

col_pct <- as.numeric(gsub('%$', '', col_widths[i])) / 100

if (table_width == 'auto') {

# Table width not specified, use all available space
col_scalar <- col_pct
tab_unit <- '\\linewidth'

} else if (endsWith(table_width, suffix = '%')) {

# If table width is expressed as a percentage, adjust the scaler
col_scalar <- col_pct * as.numeric(gsub('%', '', table_width)) / 100
tab_unit <- '\\linewidth'

} else {

# When table width is expressed in units, convert to points
col_scalar <- col_pct * convert_to_px(table_width) * 0.75 # 0.75 converts pixels to points
tab_unit <- 'pt'

}

col_widths[i] <-
paste0(
"\\dimexpr ",
col_scalar,
tab_unit,
"-2\\tabcolsep-1.5\\arrayrulewidth"
)

}

col_defs_i <- paste0(align, "p{", col_widths[i], "}")

} else {
Expand Down

0 comments on commit c3bfde7

Please sign in to comment.