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

Floating table #1588

Merged
merged 49 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0b6f183
Add ifelse statements to choose a floating table environment rather t…
AaronGullickson Feb 25, 2024
454da10
Add latex.use.longtable option to tab_options
AaronGullickson Feb 25, 2024
c8265f7
Fix bug with floating table and no table notes
AaronGullickson Feb 25, 2024
08b3b18
Adjust testing examples that fail
AaronGullickson Feb 25, 2024
e6aecb0
Add documentation for latex.use.longtable table option
AaronGullickson Feb 26, 2024
50c18d4
Add missing newline after \\end{tabular}
AaronGullickson Feb 26, 2024
b97b2d6
Replace tabular environment with tabular* environment
AaronGullickson Feb 26, 2024
2d54626
Use correct table with in tabular* environment with pixel option
AaronGullickson Feb 26, 2024
51da31e
Add latex widths tests for tabular* environment
AaronGullickson Feb 26, 2024
ca24b2c
Correct tests by adding latex.use.longtable = TRUE to options
AaronGullickson Feb 26, 2024
693f1db
Merge branch 'master' into floating-table
rich-iannone Feb 27, 2024
a22ad9a
Correct error with missing documentation for latex.use.longtable argu…
AaronGullickson Feb 27, 2024
86467f1
Fix conflict
AaronGullickson Mar 9, 2024
0e4503d
Creat wrap environment functions
AaronGullickson Mar 13, 2024
4b7f852
Fix tests
AaronGullickson Mar 13, 2024
0f4b301
Add latex styling tests for longtable and tabular*
AaronGullickson Mar 13, 2024
61b87f5
Add news item
AaronGullickson Mar 13, 2024
78596b9
Add latex.tbl.pos option
AaronGullickson Apr 5, 2024
a3bdd9d
Update news on floating environment with information about positioning
AaronGullickson Apr 5, 2024
f0db183
Fix param indentation
AaronGullickson Apr 5, 2024
0691e72
Fix documentation style
AaronGullickson Apr 5, 2024
c6eeaa1
Correct expect_tab for new option
AaronGullickson Apr 5, 2024
0736fdf
Merge branch 'master' into floating-table
AaronGullickson May 8, 2024
e6f3c4d
Merge branch 'master' into floating-table
AaronGullickson May 9, 2024
c601a34
Merge branch 'master' into floating-table
AaronGullickson May 24, 2024
84770e0
Fix conflict
AaronGullickson May 24, 2024
b3bf595
Merge branch 'floating-table' of https://github.com/AaronGullickson/g…
AaronGullickson May 24, 2024
45e5e69
Updated knitr helper functions
nielsbock Jun 8, 2024
ca0637e
added caption component to latex table and made it work in both longt…
nielsbock Jun 8, 2024
804a089
Added option to use sidewaystable + documentation
nielsbock Jun 8, 2024
fd0c99c
added caption compoment to latex table (same template as for html tab…
nielsbock Jun 8, 2024
f43eec0
revert to fix heading component as only change
nielsbock Jun 22, 2024
f1db9f4
minor edits
nielsbock Jun 22, 2024
3db2264
minor edits
nielsbock Jun 22, 2024
9b2de48
Fix conflicts
AaronGullickson Jul 10, 2024
09c5208
Merge pull request #3 from nielsbock/floating-table-fix-heading-compo…
AaronGullickson Jul 10, 2024
1a74b16
Merge branch 'floating-table' of https://github.com/AaronGullickson/g…
AaronGullickson Jul 10, 2024
18892db
Update test
AaronGullickson Jul 10, 2024
2b6ce8b
Use double quotes
rich-iannone Jul 11, 2024
535fa84
Use `||` instead of `|` in condition
rich-iannone Jul 11, 2024
7faf1de
Merge branch 'master' into floating-table
rich-iannone Jul 11, 2024
f1edfc0
Merge main branch
olivroy Jul 12, 2024
d9dadcf
Use double quotes
olivroy Jul 12, 2024
c331d3b
Merge pull request #4 from olivroy/AaronGullickson-floating-table
AaronGullickson Jul 15, 2024
0b9800a
Uncomment tab_header line
AaronGullickson Jul 15, 2024
0d0ec74
Merge branch 'floating-table' of https://github.com/AaronGullickson/g…
AaronGullickson Jul 15, 2024
ae0ada3
Change latex.use.longtable to latex.use_longtable
AaronGullickson Jul 15, 2024
2288cda
Merge branch 'master' into floating-table
AaronGullickson Jul 15, 2024
2f9c663
Merge branch 'master' into floating-table
rich-iannone Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gt (development version)

* PDF output now defaults to a full-width floating environment using `tabular*` (@AronGullickson, #1588). Float position can be controlled by the `latex.tbl.pos` argument in `tab_options`. Quarto users can alternatively use the `tbl-pos` argument to control positioning. To use a `longtable` environment instead, use `tab_option(latex.use.longtable = TRUE)`.

# gt 0.11.0

## New features
Expand Down
2 changes: 2 additions & 0 deletions R/dt_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,6 @@ dt_options_tbl <-
"page_footer_height", FALSE, "page", "value", "0.5in",
"quarto_disable_processing", FALSE, "quarto", "logical", FALSE,
"quarto_use_bootstrap", FALSE, "quarto", "logical", FALSE,
"latex_use_longtable", FALSE, "latex", "logical", FALSE,
"latex_tbl_pos", FALSE, "latex", "value", "!t",
)[-1, ]
59 changes: 41 additions & 18 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ as_latex <- function(data) {
footer_component <- create_footer_component_l(data = data)

# Create a LaTeX fragment for the ending tabular statement
table_end <- create_table_end_l()
table_end <- create_table_end_l(data = data)

# If the `rmarkdown` package is available, use the
# `latex_dependency()` function to load latex packages
Expand All @@ -718,25 +718,48 @@ 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",
table_width_statement,
fontsize_statement,
table_start,
heading_component,
columns_component,
body_component,
table_end,
footer_component,
"\\endgroup\n",
collapse = ""
),
meta = latex_packages
)
}
if (dt_options_get_value(data = data, option = "latex_use_longtable")) {
knitr::asis_output(
paste0(
wrap_start_statement,
table_width_statement,
fontsize_statement,
table_start,
heading_component,
columns_component,
body_component,
table_end,
footer_component,
wrap_end_statement,
collapse = ""
),
meta = latex_packages
)
} else {
knitr::asis_output(
paste0(
wrap_start_statement,
heading_component,
table_width_statement,
fontsize_statement,
table_start,
columns_component,
body_component,
table_end,
footer_component,
wrap_end_statement,
collapse = ""
),
meta = latex_packages
)
}
}

#' Output a **gt** object as RTF
#'
Expand Down
22 changes: 21 additions & 1 deletion R/tab_create_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -4409,6 +4409,24 @@ set_style.cells_source_notes <- function(loc, data, style) {
#' deactivated with `quarto.disable_processing = TRUE` (this option is `FALSE`
#' by default).
#'
#' @param latex.use.longtable
#'
#' *Use longtable latex environment*
#'
#' By default, tables produced using latex code will be embedded in the
#' `table` floating environment. Setting this parameter to `TRUE` will instead
#' use the `longtable` environment which will not float and may span multiple
#' pages.
#'
#' @param latex.tbl.pos
#'
#' *Specify latex floating position*
#'
#' The latex position indicator for a floating environment (e.g. "!t", "H"). It
#' should be specified without square brackets. Quarto users should instead
#' set the floating position within the code chunk argument `tbl-pos`. Table
#' will only float if longtable environment is set to `FALSE`.
#'
#' @return An object of class `gt_tbl`.
#'
#' @section Examples:
Expand Down Expand Up @@ -4726,7 +4744,9 @@ tab_options <- function(
page.header.height = NULL,
page.footer.height = NULL,
quarto.use_bootstrap = NULL,
quarto.disable_processing = NULL
quarto.disable_processing = NULL,
latex.use.longtable = NULL,
latex.tbl.pos = NULL
) {

# Perform input object validation
Expand Down
76 changes: 68 additions & 8 deletions R/utils_render_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ latex_group_row <- function(
)
}

#' @noRd
create_wrap_start_l <- function(data) {
tbl_pos = ifelse(check_quarto(),
"",
paste0("[",
dt_options_get_value(data = data,
option = "latex_tbl_pos"),
"]"))

ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"\\begingroup\n",
paste0("\\begin{table}", tbl_pos, "\n"))
}

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

Expand Down Expand Up @@ -213,11 +227,41 @@ create_table_start_l <- function(data, colwidth_df) {
if (dt_options_get_value(data = data, option = "table_width") != "auto")
extra_sep <- "@{\\extracolsep{\\fill}}"

# determine string for table width if using tabular* environment
hdr_tabular <- ""
if(!dt_options_get_value(data = data, option = "latex_use_longtable")) {

# we need to use the extracolsep here for tabular* regardless of width
extra_sep <- '@{\\extracolsep{\\fill}}'
table_width <- dt_options_get_value(data = data, 'table_width')

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

tw <- as.numeric(gsub('%', '', table_width))
hdr_tabular <- paste0("\\begin{tabular*}{", tw/100, "\\linewidth}{")

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

width_in_pt <- 0.75 * convert_to_px(table_width)
hdr_tabular <- paste0("\\begin{tabular*}{", width_in_pt, "pt}{")

} else {

hdr_tabular <- "\\begin{tabular*}{\\linewidth}{"

}

}

# Generate setup statements for table including default left
# alignments and vertical lines for any stub columns
paste0(
longtable_post_length,
"\\begin{longtable}{",
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
longtable_post_length,
""),
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"\\begin{longtable}{",
hdr_tabular),
extra_sep,
paste(col_defs, collapse = ""),
"}\n",
Expand Down Expand Up @@ -295,9 +339,14 @@ create_heading_component_l <- function(data) {
subtitle_row <- ""
}

paste_between(
paste0(title_row, subtitle_row),
x_2 = c("\\caption*{\n", "\n} \\\\ \n")
paste0(
paste_between(
paste0(title_row, subtitle_row),
x_2 = c("\\caption*{\n", "\n}")
),
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
" \\\\ \n",
" \n")
)
}

Expand Down Expand Up @@ -922,15 +971,24 @@ summary_rows_for_group_l <- function(
}

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

paste0(
"\\bottomrule\n",
"\\end{longtable}\n",
ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"),
"\\end{longtable}\n",
"\\end{tabular*}\n"),
collapse = ""
)
}

#' @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 Down Expand Up @@ -1275,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
18 changes: 17 additions & 1 deletion man/tab_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/gt-examples/03-latex/latex-01-iris.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ gt(iris) %>%
) %>%
tab_source_note(
source_note = md("The data were collected by *Anderson* (1935).")
)
) %>%
tab_options(latex.use.longtable = TRUE)
```
3 changes: 2 additions & 1 deletion tests/gt-examples/03-latex/latex-06-mtcars.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,6 @@ gt(mtcars, rownames_to_stub = TRUE) %>%
cols_label(
hp = md("*HP*"),
qsec = "QMT, seconds"
)
) %>%
tab_options(latex.use.longtable = TRUE)
```
5 changes: 3 additions & 2 deletions tests/gt-examples/03-latex/latex-13-adding_footnotes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gt(
data = tbl,
groupname_col = "date"
) %>%
tab_header(title = "The Table Title", subtitle = "The subtitle.") %>%
#tab_header(title = "The Table Title", subtitle = "The subtitle.") %>%
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you comment that out?

Copy link
Member

Choose a reason for hiding this comment

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

I looked into this and if the line is uncommented and tab_options(latex.use.longtable = TRUE) is removed, we get a LaTeX error when rendering the .Rmd file:

! You can't use `\hrule' here except with leaders.
\caption@hrule ->\hrule 
                        \@height \z@ 
l.199 }
        \\ 

Error: LaTeX failed to compile latex-13-adding_footnotes.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See latex-13-adding_footnotes.log for more info.
Execution halted

Copy link
Collaborator

Choose a reason for hiding this comment

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

@AaronGullickson Are you able to fix this? It is important that LaTeX tables don't get compilation issues if they have a header

Copy link
Contributor

@nielsbock nielsbock Jul 11, 2024

Choose a reason for hiding this comment

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

The error is due to the unnumbered caption being placed within the tabular environment when tab_options(latex.use.longtable = FALSE). I'm not exactly sure how why the error occurs in the test because this specific problem is solved with the code in lines 727-762 in R/export.R.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I must have commented that out early on and forgot about it. However, when I uncomment the line, the Rmd file renders fine for me with the latest changes from the PR. @rich-iannone Can you please confirm it gives you that error with the latest?

Copy link
Member

Choose a reason for hiding this comment

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

@AaronGullickson everything is good here now!

tab_spanner(
label = "values",
columns = starts_with("value")
Expand Down Expand Up @@ -75,5 +75,6 @@ gt(
tab_footnote(
footnote = md("The `subtitle` can likewise get a footnote."),
locations = cells_title(groups = "subtitle")
)
) %>%
tab_options(latex.use.longtable = TRUE)
```
Loading
Loading