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

Katex introduces errors in footnotes #1470

Closed
pbreheny opened this issue Jun 17, 2024 · 6 comments
Closed

Katex introduces errors in footnotes #1470

pbreheny opened this issue Jun 17, 2024 · 6 comments
Assignees
Labels
bug an unexpected problem or unintended behavior

Comments

@pbreheny
Copy link

I recently switched my book over to using Katex as the math engine by adding the following to _output.yml:

bookdown::gitbook:
  math_method: r-katex

The math looks wonderful, everything renders correctly, but it screws up the footnotes -- all footnotes now appear on the last page, as in this issue: #793.

You can reproduce this in any book (so far as I can tell), but here's an explicit example:

git clone git@github.com:rstudio/bookdown-demo.git

then add math_method: r-katex as in the above.

@cderv cderv added the bug an unexpected problem or unintended behavior label Jun 19, 2024
@cderv cderv moved this from Backlog to Next / Ready for Dev in R Markdown Team Projects Jun 19, 2024
@cderv
Copy link
Collaborator

cderv commented Jun 19, 2024

Thanks for the report! I can reproduce.

We are probably not handling something correctly when detecting footnote to move when r-katex is used.

@cderv
Copy link
Collaborator

cderv commented Jun 19, 2024

I think our regex in

bookdown/R/html.R

Lines 1027 to 1037 in f244cf1

parse_footnotes = function(x) {
i = grep('<div class="footnotes[^"]*">', x)
if (length(i) == 0) return(list(items = character(), range = integer()))
j = which(x == '</div>')
j = min(j[j > i])
n = length(x)
r = '<li id="fn([0-9]+)"><p>(?s).+?<a href="#fnref\\1"[^>]*?>\\X</a></p></li>'
s = paste(x[i:n], collapse = '\n')
items = unlist(regmatches(s, gregexpr(r, s, perl = TRUE)))
list(items = setNames(items, gsub(r, 'fn\\1', items, perl = TRUE)), range = i:j)
}

does not take into account the SVG content resulting from r-katex or something else 🤔

@cderv
Copy link
Collaborator

cderv commented Jun 19, 2024

I think the issue that with KaTeX processing we get

<a href=\"#fnref2\" class=\"footnote-back\">&#x21A9;&#xFE0E;</a></p></li>

where without we get

<a href=\"#fnref2\" class=\"footnote-back\">↩︎</a></p></li>

Not the difference between single unicode chars and several HTML sequence characters.

So <a href="#fnref\\1"[^>]*?>\\X</a></p></li> is not adapated here. Maybe using \\X+ is enough, or we need something more tailored to what we want to match.

@yihui hope it helps do the right thing. Thanks!

@yihui yihui closed this as completed in 716c4cc Jun 19, 2024
@github-project-automation github-project-automation bot moved this from Next / Ready for Dev to Done in R Markdown Team Projects Jun 19, 2024
@yihui
Copy link
Member

yihui commented Jun 19, 2024

@cderv Thanks for the investigation!

@pbreheny It should be fixed now, but I don't have time to verify it. Please let us know if the problem persists. Thanks! You can install the development version via

remotes::install_github('rstudio/bookdown')

@pbreheny
Copy link
Author

Works for me! 🎉

Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
Archived in project
Development

No branches or pull requests

3 participants