rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.76k stars 1.27k forks source link

Katex introduces errors in footnotes #1470

Closed pbreheny closed 3 months ago

pbreheny commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

I think our regex in https://github.com/rstudio/bookdown/blob/f244cf12bf2c2d7106ac6322b2b2a5796d4ef0c8/R/html.R#L1027-L1037

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

cderv commented 3 months ago

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 commented 3 months ago

@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 commented 3 months ago

Works for me! :tada: