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

longtable + booktabs breaks on row starting with ( or [ #145

Closed
vincentarelbundock opened this issue Jan 22, 2019 · 4 comments · Fixed by #1390
Closed

longtable + booktabs breaks on row starting with ( or [ #145

vincentarelbundock opened this issue Jan 22, 2019 · 4 comments · Fixed by #1390

Comments

@vincentarelbundock
Copy link

vincentarelbundock commented Jan 22, 2019

Apparently, when using both booktabs and longtable, if a row starts with the characters ( or [, latex compilation doesn't work. This happened to me when attempting to compile a notebook where a table had "(Intercept)" as a cell in the left-column.

This weird behavior is not caused by gt per se. One solution is documented here: https://tex.stackexchange.com/questions/228755/undefined-control-sequence-on-left-parenthesis-after-midrule-in-longtable/228756#228756

That is to use \midrule\relax instead of just \midrule.

I could probably hack around this ingtsummary, but this problem may arise for other gt users as well.

What do you think?



---
title: "R Notebook"
output: pdf_document
---

```{r}
library(gt)
data.frame('a' = c('OK', 'OK'), 'b' = 1:2) %>% gt()

data.frame('a' = c('(BAD)', 'OK'), 'b' = 1:2) %>% gt()
@vincentarelbundock vincentarelbundock changed the title edge case: longtable + booktabs + row starting with ( or [ longtable + booktabs breaks on row starting with ( or [ Jan 23, 2019
@rich-iannone
Copy link
Member

I've tried out the code and indeed ran into the LaTeX error because the compiler thinks that ( or [ continues from \midrule. I tried to apply a fix by adding \relax after every \midrule but I ran into problems there as well. This will unfortunately require some more experimentation on my part to get to a solution.

@vincentarelbundock
Copy link
Author

vincentarelbundock commented Jan 31, 2019

I'm curious to know what other issues you encountered. No pressure though, i know time is scarce. In modelsummary, I implemented a workaround function called knit_latex to use until things are fixed here. The second table compiles fine with \midrule\relax. I imagine the problem comes with more complicated examples.

library(gt)
library(gtsummary)

data.frame('a' = c('(BAD)', 'OK'), 'b' = 1:2) %>% gt() %>% as_latex() %>% cat
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \begin{longtable}{cr}
#> \toprule
#> a & b \\ 
#> \midrule
#> (BAD) & 1 \\ 
#> OK & 2 \\ 
#> \bottomrule
#> \end{longtable}

data.frame('a' = c('(BAD)', 'OK'), 'b' = 1:2) %>% gt() %>% gtsummary::knit_latex() %>% cat
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \begin{longtable}{cr}
#> \toprule
#> a & b \\ 
#> \midrule\relax
#> (BAD) & 1 \\ 
#> OK & 2 \\ 
#> \bottomrule
#> \end{longtable}

Created on 2019-01-31 by the reprex package (v0.2.1)

@steveputman
Copy link
Contributor

Let me know if this belongs in a separate issue, but I wonder if this bug is related (which is also not limited to gt): it's another \midrule issue appearing in longtable. When rows are grouped and the last group in the table has only one row, the \midrule before that last row gets dropped. This compiles, but the output obviously has a \midrule missing so the group label looks like a row.

library(tidyverse)
library(gt)

tribble(
  ~groupname, ~Subtype, ~Units,
  "Group1", "A", 100,
  "Group2", "B", 200) %>%
  gt() %>% 
  as_latex() %>% 
  cat
#> \captionsetup[table]{labelformat=empty,skip=1pt}
#> \begin{longtable}{lr}
#> \toprule
#> Subtype & Units \\ 
#> \midrule
#> \multicolumn{1}{l}{Group1} \\ 
#> \midrule
#> A & 100 \\ 
#> \midrule
#> \multicolumn{1}{l}{Group2} \\ 
#> B & 200 \\ 
#> \bottomrule
#> \end{longtable}

Created on 2019-02-05 by the reprex package (v0.2.1)

image

@gorkang
Copy link

gorkang commented Jul 14, 2023

Just encountered this when using gtsummary with a quarto pdf.

Any regression table including an intercept "(Intercept)" will fail because the top leftmost cell starts with a parenthesis.

I've been testing what works and what fails, and wanted to leave it here in case is useful

# Works
gt::gt(data.frame(A = c("\u00A0(OK)", "sdf")))
gt::gt(data.frame('a' = c('OK', '(OK)'), 'b' = 1:2))
gt::gt(data.frame(B = "OK", A = "(OK)"))


# Fails
gt::gt(data.frame(A = "(BAD)"))
gt::gt(data.frame(A = paste0(knitr::asis_output("\u0028"), "BAD)")))
gt::gt(data.frame(A = "\u0028 BAD)"))
gt::gt(data.frame('a' = c('(BAD)', 'OK'), 'b' = 1:2))
gt::gt(data.frame(B = "(BAD)", A = "OK"))
gt::gt(data.frame(A = c("  (BAD)", "sdf")))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment