rstudio / bookdown

Authoring Books and Technical Documents with R Markdown

Home Page:https://pkgs.rstudio.com/bookdown/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Katex introduces errors in footnotes

pbreheny opened this issue · comments

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.

Thanks for the report! I can reproduce.

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

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 🤔

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" target="_blank" rel="nofollow"[^>]*?>\\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!

@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')

Works for me! 🎉