Skip to content

Commit

Permalink
Creat wrap environment functions
Browse files Browse the repository at this point in the history
Instead of trying to add \begin{table} and \end{table} within various functions, I moved this step to the export.R as_latex function to create a wrapping environment that is either \begingroup-\endgroup for longtables or \begin{table}-\end{table} for floating tables. This will allow floating tables to be able to use fontsize changes and any other future changes that are meant to be added below the enclosing environment.
  • Loading branch information
AaronGullickson committed Mar 13, 2024
1 parent 86467f1 commit 0e4503d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
8 changes: 6 additions & 2 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,15 @@ as_latex <- function(data) {
# Allow user to set a font-size
fontsize_statement <- create_fontsize_statement_l(data = data)

# create wrapping environment
wrap_start_statement <- create_wrap_start_l(data = data)
wrap_end_statement <- create_wrap_end_l(data = data)


# Compose the LaTeX table
knitr::asis_output(
paste0(
"\\begingroup\n",
wrap_start_statement,
table_width_statement,
fontsize_statement,
table_start,
Expand All @@ -743,7 +747,7 @@ as_latex <- function(data) {
body_component,
table_end,
footer_component,
"\\endgroup\n",
wrap_end_statement,
collapse = ""
),
meta = latex_packages
Expand Down
28 changes: 19 additions & 9 deletions R/utils_render_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ latex_group_row <- function(
)
}

#' @noRd
create_wrap_start_l <- function(data) {
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"\\begingroup\n",
"\\begin{table}\n")
}

#' @noRd
create_table_start_l <- function(data) {

Expand Down Expand Up @@ -306,7 +313,7 @@ create_table_start_l <- function(data) {
paste0(
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
longtable_post_length,
"\\begin{table}\n"),
""),
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"\\begin{longtable}{",
hdr_tabular),
Expand Down Expand Up @@ -973,6 +980,13 @@ create_table_end_l <- function(data) {
)
}

#' @noRd
create_wrap_end_l <- function(data) {
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"\\endgroup\n",
"\\end{table}\n")
}

#' @noRd
create_footer_component_l <- function(data) {

Expand All @@ -981,10 +995,7 @@ create_footer_component_l <- function(data) {

# If there are no footnotes or source notes, return an empty string
if (nrow(footnotes_tbl) == 0 && length(source_notes_vec) == 0) {
return(ifelse(dt_options_get_value(data = data,
option = "latex_use_longtable"),
"",
"\\end{table}\n"))
return("")
}

# Get the multiline and separator options for footnotes and source notes
Expand Down Expand Up @@ -1060,9 +1071,6 @@ create_footer_component_l <- function(data) {
"\\begin{minipage}{\\linewidth}\n",
paste0(footnotes, source_notes),
"\\end{minipage}\n",
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"",
"\\end{table}\n"),
collapse = ""
)
}
Expand Down Expand Up @@ -1325,7 +1333,9 @@ 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') {
# of if using floating table
if (table_width == 'auto' |
!dt_options_get_value(data = data, option = "latex_use_longtable")) {

statement <- ''

Expand Down

0 comments on commit 0e4503d

Please sign in to comment.