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

Edge case where regex in row_spec_latex fails #881

Open
majazaloznik opened this issue Dec 30, 2024 · 4 comments
Open

Edge case where regex in row_spec_latex fails #881

majazaloznik opened this issue Dec 30, 2024 · 4 comments

Comments

@majazaloznik
Copy link

There is a regex in the row_spec_latex function, which fails if two rows differ only in the last column, where one is empty, and the other has a value.

In that case no duplicated rows are registered, so the regex below replaces the wrong row.

    regex <- paste0("\\Q", target_row, "\\E")
            if (grepl(regex, out)) {
                out <- temp_sub(regex, new_row, out, perl = TRUE)
            }

The two examples below first show an example of the function working fine, because row 3 only matches row 3.

In the second example however, row 3 without the "extra" is also matched by row 2, and the striping gets misplaced..

df1  <- data.frame(var1 = c("a", "b", "b", "c"),
                   var2 = c("", "", "extra", ""))

kable(df1, format = "latex") |> 
        kable_styling(latex_options = c( "striped")) 

df2  <- data.frame(var1 = c("a", "b", "b", "c"),
                   var2 = c("", "extra", "", ""))

kable(df2, format = "latex") |> 
        kable_styling(latex_options = c( "striped")) 

image
image

@dmurdoch
Copy link
Collaborator

I can confirm the bug in the latest devel version. I'll take a look...

@dmurdoch
Copy link
Collaborator

This is a tricky issue to fix. The problem is that at this point the code is working with a single long character string containing the latex for the whole table, and it needs to make changes to some of the lines within the table. It is using regular expressions to try to identify the substring corresponding to each line and then substituting the modified version of that substring, and as you saw, sometimes it gets it wrong.

I think I'm unlikely to be able to fix it without messing up something else, so all I can offer is a workaround to make sure this never happens. If you replace "" with "\\vphantom{.}" in the last column of your table and use escape = FALSE in the call to kbl() or kable(), it should avoid the problem, e.g.

df2  <- data.frame(var1 = c("a", "b", "b", "c"),to 
                   var2 = c("", "extra", "", ""))

n <- ncol(df2)
fixed <- df2[[n]]
fixed[fixed == ""] <- "\\vphantom{.}"
df2[[n]] <- fixed

kbl(df2, format = "latex", escape = FALSE) |> 
        kable_styling(latex_options = c( "striped")) 

I think someone else will have to put together a PR that doesn't require the workaround.

@majazaloznik
Copy link
Author

yeah, that makes sense. I'll try your solution, but by the looks of it it solves my immediate problem, cheers!

btw, do you think maybe a solution might be to add $ and ^ to the regex and thereby make sure it's matching the whole string? (I'm not at my computer to test this now, but I'm now thinking the same issue might occur in the first column as well? or maybe not because of the extra &.. 🐤)

@dmurdoch
Copy link
Collaborator

No, you don't want to match the whole string. The whole table is in one string, with each line just being a substring in it.

It would probably work to look for the new line marker ("\" in LaTeX), but that's tricky because the table might contain escaped backslashes, and can contain both escaped and unescaped LaTeX macros, so it has to be done very carefully.

I concluded that a bigger change is needed: maybe split the table into individual rows so your suggestion would work, or go even further and use the tools::parseLatex function to interpret all of the Latex and work on the parse code. Both of those would need careful work and a lot of testing, and I'm not going to be able to start on it for at least a couple of weeks. Maybe someone else will have fixed it by then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants