Skip to content

Commit

Permalink
Merge pull request #1603 from kbrevoort/bug1580
Browse files Browse the repository at this point in the history
Fix Issue #1580
  • Loading branch information
rich-iannone authored Mar 9, 2024
2 parents b8d9cf8 + 9c33f67 commit 411328c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 66 deletions.
12 changes: 6 additions & 6 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -725,25 +725,25 @@ as_latex <- function(data) {
latex_packages <- NULL
}

table_width_bookends <- derive_table_width_bookends(data = data)
table_width_statement <- derive_table_width_statement_l(data = data)

# Allow user to set a font-size
font_size_bookends <- create_font_size_bookends_l(data = data)
fontsize_statement <- create_fontsize_statement_l(data = data)


# Compose the LaTeX table
knitr::asis_output(
paste0(
table_width_bookends[1L],
font_size_bookends[1L],
"\\begingroup\n",
table_width_statement,
fontsize_statement,
table_start,
heading_component,
columns_component,
body_component,
table_end,
footer_component,
font_size_bookends[2L],
table_width_bookends[2L],
"\\endgroup\n",
collapse = ""
),
meta = latex_packages
Expand Down
66 changes: 23 additions & 43 deletions R/utils_render_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -1282,14 +1282,14 @@ split_row_content <- function(x) {
split(row_content, ceiling(seq_along(row_content) / ncol(x)))
}

derive_table_width_bookends <- function(data) {
derive_table_width_statement_l <- function(data) {

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

# Bookends are not required if a table width is not specified
if (table_width == 'auto') {

bookends <- c('', '')
statement <- ''

} else if (endsWith(table_width, "%")) {

Expand All @@ -1299,53 +1299,33 @@ derive_table_width_bookends <- function(data) {
((100 - tw) / 200) %>%
format(scientific = FALSE, trim = TRUE)

bookends <-
c(
paste0(
"\\newlength\\holdLTleft",
"\\newlength\\holdLTright",
"\\setlength\\holdLTleft{\\LTleft}\\relax",
"\\setlength\\holdLTright{\\LTright}\\relax",
sprintf(
'\\setlength\\LTleft{%s\\linewidth}\n\\setlength\\LTright{%s\\linewidth}',
side_width,
side_width
),
collapse = "\n"
),
"\\setlength\\LTleft{\\holdLTleft}\n\\setlength\\LTright{\\holdLTright}"
)
statement <- paste0(
"\\setlength\\",
c("LTleft", "LTright"),
"{",
side_width,
"\\linewidth}",
collapse = "\n"
)

} else {

width_in_pt <- 0.75 * convert_to_px(table_width)

halfwidth_in_pt <- format(width_in_pt / 2, scientific = FALSE, trim = TRUE)

bookends <-
c(
paste0(
"\\newlength\\holdLTleft",
"\\newlength\\holdLTright",
"\\setlength\\holdLTleft{\\LTleft}\\relax",
"\\setlength\\holdLTright{\\LTright}\\relax",
sprintf(
"\\setlength\\LTleft{\\dimexpr(0.5\\linewidth - %spt)}\n\\setlength\\LTright{\\dimexpr(0.5\\linewidth - %spt)}",
halfwidth_in_pt,
halfwidth_in_pt
),
collapse = '\n'
),
paste0(
"\\setlength\\LTleft{\\holdLTleft}",
"\\setlength\\LTright{\\holdLTright}",
collapse = "\n"
)
)
statement <- paste0(
"\\setlength\\",
c("LTleft", "LTright"),
"{\\dimexpr(0.5\\linewidth - ",
halfwidth_in_pt,
"pt)}",
collapse = "\n"
)

}

bookends
statement

}

Expand Down Expand Up @@ -1580,12 +1560,12 @@ apply_spanner_styles_l <- function(spanners_rle, styles_tbl) {
spanners_rle
}

create_font_size_bookends_l <- function(data) {
create_fontsize_statement_l <- function(data) {

size_options <- dplyr::filter(dt_options_get(data), parameter == 'table_font_size')
size <- unlist(size_options$value)[1L]

fs_fmt <- "\\begingroup\n\\fontsize{%3.1fpt}{%3.1fpt}\\selectfont\n"
fs_fmt <- "\\fontsize{%3.1fpt}{%3.1fpt}\\selectfont\n"
if (grepl(pattern = "^[[:digit:]]+(\\%|in|cm|emu|em|pt|px)$", size)) {

if (endsWith("%", x = size)) {
Expand All @@ -1605,9 +1585,9 @@ create_font_size_bookends_l <- function(data) {

}

} else return(c("", ""))
} else return("")

c(fs_statement, "\\endgroup\n")
fs_statement

}

17 changes: 0 additions & 17 deletions tests/testthat/test-as_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,13 @@ test_that("Table width correctly output in LaTeX", {

expect_gt(end_pt, 0)

# Verify that the holdLTleft and holdLTright variables are defined and set
latex_prefix <- substr(gt_latex_width_1, 1L, start_pt)

expect_match(latex_prefix, "\\\\newlength\\\\holdLTleft")

expect_match(latex_prefix, "\\\\newlength\\\\holdLTright")

expect_match(latex_prefix, "\\\\setlength\\\\holdLTleft\\{\\\\LTleft\\}\\\\relax")

expect_match(latex_prefix, "\\\\setlength\\\\holdLTright\\{\\\\LTright\\}\\\\relax")

# Verify that LTleft and LTright are correctly specified
expect_match(latex_prefix, "\\\\setlength\\\\LTleft\\{0.05\\\\linewidth\\}")

expect_match(latex_prefix, "\\\\setlength\\\\LTright\\{0.05\\\\linewidth\\}")

# Verify that after the longtable environment, the LTleft and LT right are
# changed back to their previous values
latex_suffix <- substr(gt_latex_width_1, end_pt, nchar(gt_latex_width_1))

expect_match(latex_suffix, "\\\\setlength\\\\LTleft\\{\\\\holdLTleft\\}")

expect_match(latex_suffix, "\\\\setlength\\\\LTright\\{\\\\holdLTright\\}")

# Test specification of a table width in pixels
gt_latex_width_2 <-
gt(exibble) %>%
Expand Down

35 comments on commit 411328c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.