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

cols_align_decimal with Latex fails #1951

Open
dropsplash opened this issue Feb 20, 2025 · 3 comments
Open

cols_align_decimal with Latex fails #1951

dropsplash opened this issue Feb 20, 2025 · 3 comments

Comments

@dropsplash
Copy link

hello,

thanks for your work.

cols_align_decimal fails with latex

output
LaTeX Error: Unicode character   (U+2007)
not set up for use with LaTeX.

Any clue how to solve it?

Thanks

@olivroy
Copy link
Collaborator

olivroy commented Feb 20, 2025

Hi @dropsplash ! Thanks for the report.

Could you share a minimal reproducible example? I can't reproduce this.

I tried this with gt 0.11.1 and Quarto 1.6.40 and it works as expected

Image

---
title: test
format: pdf
---

```{r}
library(gt)
```

```{r}
dplyr::tibble(
  char = LETTERS[1:9],
  num = c(1.2, -33.52, 9023.2, -283.527, NA, 0.401, -123.1, NA, 41)
) |>
  gt() |>
  fmt_number(
    columns = num,
    decimals = 3,
    drop_trailing_zeros = TRUE
  ) |>
  cols_align_decimal()
```

@dropsplash
Copy link
Author

dropsplash commented Feb 20, 2025

Hello,

version

  • R version 4.4.2 (2024-10-31)
  • Platform: aarch64-apple-darwin20
  • Running under: macOS Sequoia 15.3.1
  • gt_0.11.1
  • quarto 1.6.40
  • pdfTeX 3.141592653-2.6-1.40.26 (TeX Live 2024)
  • kpathsea version 6.4.0

here the example


title: test
format: pdf
pdf-engine: pdflatex

library(gt)
dplyr::tibble(
  char = LETTERS[1:9],
  num = c(1.2, -33.52, 9023.2, -283.527, NA, 0.401, -123.1, NA, 41)
) |>
  gt() |>
  fmt_number(
    columns = num,
    decimals = 3,
    drop_trailing_zeros = TRUE
  ) |>
 cols_align_decimal(dec_mark = ".")

The issue is about unicode char for aligning around decimal.
Xetex engine for latex seems to solve it, but unfortunately, I can't use it because of some Latex packages that do not work with it, and I must stick with pdflatex engine.

Here the output

compilation failed- error
LaTeX Error: Unicode character   (U+2007)
not set up for use with LaTeX.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.237 A &  
   1.2   \

If I remove '|>cols_align_decimal(dec_mark = ".")' it works but not exactly as I would

Thanks again for your help and your great work.

@olivroy
Copy link
Collaborator

olivroy commented Feb 20, 2025

Ok, I can reproduce this now.

It seems that the engine has trouble dealing with the \U02007 character (a space)
This is probably an issue that occurs with older versions of gt as well as nothing significant seems to have changed since

cols_align_decimal() was added in this PR #1058

It uses \U02007 for padding and pdflatex doesn't seem to like it.

https://bookdown.org/yihui/rmarkdown-cookbook/latex-unicode.html

This is the internal function where this is added. Unfortunately, I don't think this will receive a quick fix on the gt side..

gt/R/cols_align_decimal.R

Lines 174 to 257 in d99a57e

align_to_char <- function(x, align_at = ".") {
na_x_vals <- x == "NA"
no_a_char <- !grepl(align_at, x, fixed = TRUE) & !grepl("[0-9]", x)
has_t_dec <- grepl("[0-9]\\.$", x)
x_no_align <- na_x_vals | no_a_char
x_str <- as.character(x)
split_x <- strsplit(x[!x_no_align], align_at, fixed = TRUE)
x_lhs <-
unlist(
lapply(
split_x,
FUN = function(x) x[1]
)
)
x_rhs <-
unlist(
lapply(
split_x,
FUN = function(x) paste0(x[-1], collapse = align_at)
)
)
x_piece_lhs <-
paste0(
strrep("\U02007", max(nchar(x_lhs)) - nchar(x_lhs)),
x_lhs
)
x_piece_rhs <-
paste0(
x_rhs,
strrep("\U02007", max(nchar(x_rhs)) - nchar(x_rhs))
)
for (i in seq_along(x_piece_lhs)) {
if (grepl("[^0-9]$", x_piece_lhs[i])) {
extracted <- str_single_extract(x_piece_lhs[i], "[^0-9]+$")
n_char_extracted <- nchar(extracted)
x_piece_lhs[i] <- gsub(extracted, "", x_piece_lhs[i], fixed = TRUE)
x_piece_rhs[i] <- paste0(extracted, x_piece_rhs[i])
x_piece_rhs[i] <-
gsub(
paste0(strrep("\U02007", n_char_extracted), "$"),
"",
x_piece_rhs[i]
)
}
}
x_align <- paste(x_piece_lhs, x_piece_rhs, sep = align_at)
x_align_parens <- grepl("\\(.+?\\)", x_align)
if (grepl(align_at, paste(x[!x_no_align], collapse = "|"), fixed = TRUE)) {
x_align[nchar(x_rhs) == 0 & !grepl(align_at, x[!x_no_align], fixed = TRUE)] <-
sub(align_at, " ", x_align[nchar(x_rhs) == 0], fixed = TRUE)
x_align[x_align_parens] <- paste0(x_align[x_align_parens], "\U000A0")
} else {
x_align[nchar(x_rhs) == 0 & !grepl(align_at, x[!x_no_align], fixed = TRUE)] <-
sub(align_at, "", x_align[nchar(x_rhs) == 0], fixed = TRUE)
x_align[!x_align_parens] <- paste0(x_align[!x_align_parens], "\U000A0")
}
x_str[!x_no_align] <- x_align
x_str
}

@olivroy olivroy added this to the FUTURE milestone Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants