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

Fix LaTeX error caused by \midrule interference #1390

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 20 additions & 43 deletions R/utils_render_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ latex_heading_row <- function(content) {

paste0(
paste(paste(content, collapse = " & "), "\\\\ \n"),
"\\midrule\n",
"\\midrule\\addlinespace[2.5pt]\n",
collapse = ""
)
}
Expand All @@ -96,10 +96,10 @@ latex_group_row <- function(
) {

paste0(
ifelse(top_border, "\\midrule\n", ""),
ifelse(top_border, "\\midrule\\addlinespace[2.5pt]\n", ""),
"\\multicolumn{", n_cols, "}{l}{", group_name,
"} \\\\ \n",
ifelse(bottom_border, "\\midrule\n", ""),
ifelse(bottom_border, "\\midrule\\addlinespace[2.5pt]\n", ""),
collapse = ""
)
}
Expand Down Expand Up @@ -133,11 +133,15 @@ create_table_start_l <- function(data) {

# Obtain widths for each visible column label
col_widths <-
boxh_df %>%
dplyr::filter(type %in% c("default", "stub")) %>%
dplyr::arrange(dplyr::desc(type)) %>%
dplyr::pull(column_width) %>%
unlist()
unlist(
dplyr::pull(
dplyr::arrange(
dplyr::filter(boxh_df, type %in% c("default", "stub")),
dplyr::desc(type)
),
column_width
)
)

# Generate the column definitions for visible columns
# these can either be simple `l`, `c`, `r` directive if a width isn't set
Expand Down Expand Up @@ -536,7 +540,8 @@ create_body_component_l <- function(data) {
# Insert a horizontal line if this is the beginning of a new row
# group and there is a two-column stub
if (group_start && has_two_col_stub && i != 1) {
body_section <- append(body_section, "\\midrule\n")
body_section <-
append(body_section, "\\midrule\\addlinespace[2.5pt]\n")
}

#
Expand All @@ -550,7 +555,7 @@ create_body_component_l <- function(data) {
group_name = group_label,
n_cols = n_cols,
top_border = i != 1,
bottom_border = i != n_rows
bottom_border = TRUE
)

body_section <- append(body_section, list(group_heading_row))
Expand Down Expand Up @@ -822,10 +827,11 @@ create_footer_component_l <- function(data) {
)

if (footnotes_multiline) {
footnotes <- paste(footnotes, collapse = "\\\\\n") %>% paste_right("\\\\\n")
footnotes <- paste_right(paste(footnotes, collapse = "\\\\\n"), "\\\\\n")
} else {
footnotes <- paste(footnotes, collapse = footnotes_sep) %>% paste_right("\\\\\n")
footnotes <- paste_right(paste(footnotes, collapse = footnotes_sep), "\\\\\n")
}

} else {
footnotes <- ""
}
Expand All @@ -834,9 +840,9 @@ create_footer_component_l <- function(data) {
if (length(source_notes_vec) > 0) {

if (source_notes_multiline) {
source_notes <- paste(source_notes_vec, collapse = "\\\\\n") %>% paste_right("\\\\\n")
source_notes <- paste_right(paste(source_notes_vec, collapse = "\\\\\n"), "\\\\\n")
} else {
source_notes <- paste(source_notes_vec, collapse = source_notes_sep) %>% paste_right("\\\\\n")
source_notes <- paste_right(paste(source_notes_vec, collapse = source_notes_sep), "\\\\\n")
}

} else {
Expand All @@ -852,35 +858,6 @@ create_footer_component_l <- function(data) {
)
}

# Function to build a vector of `group` rows in the table body
create_group_rows_l <- function(
groups_rows_df,
n_rows,
n_cols
) {

unname(
unlist(
lapply(
seq_len(n_rows),
FUN = function(x) {
if (!(x %in% groups_rows_df$row_start)) {
return("")
}

latex_group_row(
group_name = groups_rows_df[groups_rows_df$row_start == x, "group_label"][[1]],
n_cols = n_cols,
top_border = x != 1,
bottom_border = x != n_rows
)
}
)
)
)
}


# Function to build a vector of `body` rows
create_body_rows_l <- function(
data,
Expand Down
2 changes: 1 addition & 1 deletion scripts/raw_latex_testing.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
output:
pdf_document:
extra_dependencies: ["longtable", "caption", "amsmath", "booktabs"]
extra_dependencies: ["booktabs", "caption", "longtable", "colortbl", "array"]
---

```{r setup, include=FALSE}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/fmt_fraction.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/footer.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Code
.
Output
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{rlcrrrrll}\n\\toprule\nnum\\textsuperscript{\\textit{1}} & char\\textsuperscript{\\textit{2}} & fctr & date & time & datetime & currency & row & group \\\\ \n\\midrule\n0.1111 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.95 & row\\_1 & grp\\_a \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{\\textit{1}}footnote **bold** (\\#1, no `md()`)\\\\\n\\textsuperscript{\\textit{2}}footnote \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\nSource note **bold** (\\#1, no `md()`)\\\\\nSource note \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\n\\end{minipage}\n"
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{rlcrrrrll}\n\\toprule\nnum\\textsuperscript{\\textit{1}} & char\\textsuperscript{\\textit{2}} & fctr & date & time & datetime & currency & row & group \\\\ \n\\midrule\\addlinespace[2.5pt]\n0.1111 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.95 & row\\_1 & grp\\_a \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{\\textit{1}}footnote **bold** (\\#1, no `md()`)\\\\\n\\textsuperscript{\\textit{2}}footnote \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\nSource note **bold** (\\#1, no `md()`)\\\\\nSource note \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\n\\end{minipage}\n"

---

Expand Down Expand Up @@ -114,7 +114,7 @@
Code
.
Output
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{rlcrrrrll}\n\\toprule\nnum\\textsuperscript{\\textit{1}} & char\\textsuperscript{\\textit{2}} & fctr & date & time & datetime & currency & row & group \\\\ \n\\midrule\n0.1111 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.95 & row\\_1 & grp\\_a \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{\\textit{1}}footnote **bold** (\\#1, no `md()`) \\textsuperscript{\\textit{2}}footnote \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\nSource note **bold** (\\#1, no `md()`) Source note \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\n\\end{minipage}\n"
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{rlcrrrrll}\n\\toprule\nnum\\textsuperscript{\\textit{1}} & char\\textsuperscript{\\textit{2}} & fctr & date & time & datetime & currency & row & group \\\\ \n\\midrule\\addlinespace[2.5pt]\n0.1111 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.95 & row\\_1 & grp\\_a \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{\\textit{1}}footnote **bold** (\\#1, no `md()`) \\textsuperscript{\\textit{2}}footnote \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\nSource note **bold** (\\#1, no `md()`) Source note \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\n\\end{minipage}\n"

---

Expand Down Expand Up @@ -218,7 +218,7 @@
Code
.
Output
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{rlcrrrrll}\n\\toprule\nnum\\textsuperscript{\\textit{1}} & char\\textsuperscript{\\textit{2}} & fctr & date & time & datetime & currency & row & group \\\\ \n\\midrule\n0.1111 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.95 & row\\_1 & grp\\_a \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{\\textit{1}}footnote **bold** (\\#1, no `md()`) \\textit{|} \\textsuperscript{\\textit{2}}footnote \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\nSource note **bold** (\\#1, no `md()`) \\textit{|} Source note \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\n\\end{minipage}\n"
[1] "\\setlength{\\LTpost}{0mm}\n\\begin{longtable}{rlcrrrrll}\n\\toprule\nnum\\textsuperscript{\\textit{1}} & char\\textsuperscript{\\textit{2}} & fctr & date & time & datetime & currency & row & group \\\\ \n\\midrule\\addlinespace[2.5pt]\n0.1111 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.95 & row\\_1 & grp\\_a \\\\ \n\\bottomrule\n\\end{longtable}\n\\begin{minipage}{\\linewidth}\n\\textsuperscript{\\textit{1}}footnote **bold** (\\#1, no `md()`) \\textit{|} \\textsuperscript{\\textit{2}}footnote \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\nSource note **bold** (\\#1, no `md()`) \\textit{|} Source note \\textbf{bold} (\\#2, uses \\texttt{md()})\\\\\n\\end{minipage}\n"

---

Expand Down
Loading
Loading