lzanini / mdbook-katex

A preprocessor for mdBook, rendering LaTex equations to HTML at build time.
MIT License
195 stars 34 forks source link

Less than symbol not rendered correctly #114

Closed mmicu closed 2 months ago

mmicu commented 2 months ago

The < symbol is not rendered correctly. This is an example:

hello

$$ a < 1 $$

world

$$ a > 1 $$
image

That's what I am using:

mdbook v0.4.37:
    mdbook
mdbook-graphviz v0.2.0:
    mdbook-graphviz
mdbook-katex v0.8.1:
    mdbook-katex

With the following TOML configuration:

[output.html]
additional-css = ["src/css/cookieconsent.css"]
mathjax-support = true

[preprocessor.katex]
after = ["links"]
throw-on-error = true

[preprocessor.graphviz]
command = "mdbook-graphviz"
output-to-file = false
after = ["links"]

[output.html.code.hidelines]
python = "~"
SichangHe commented 2 months ago

Unfortunately, I cannot reproduce this. Could you

  1. tell me how you installed mdBook-KaTeX (especially if you are on Windows), and
  2. remove the [output.html] entry from your book.toml and try again?
mmicu commented 2 months ago
  1. I have used cargo install mdbook-katex.
  2. After commenting the [preprocessor.graphviz] entry it is working.

Any suggestions to make them work together?

mmicu commented 2 months ago

I have solved it by using the following configuration. It is the same that I wrote above, but I have changed the after parameter for preprocessor.katex:

[output.html]
additional-css = ["src/css/cookieconsent.css"]
mathjax-support = true

[preprocessor.katex]
after = ["preprocessor.graphviz"]
throw-on-error = true

[preprocessor.graphviz]
command = "mdbook-graphviz"
output-to-file = false
after = ["links"]

[output.html.code.hidelines]
python = "~"
SichangHe commented 2 months ago

Ah, I see what is going on. Code intended for mdbook-graphviz got picked up by mdBook-KaTeX (probably it contains $?). This workaround is quite clever.

I am curious what your full input looks like.

mmicu commented 2 months ago

I have tried with an empty book and with the configuration I have posted in the first message. Also with this minimal input, I am getting the wrong rendering (that disappears after applying the fix I suggested).

For example, with a SUMMARY.md like:

# Summary
[Introduction](./introduction.md)

And an introduction.md like:

$$ a = 1 $$

$$ a < 1 $$

I get the same problem I posted.

SichangHe commented 2 months ago

After running the book through mdbook-graphviz with [output.markdown], it gives:

# Chapter 1

$$ a = 1 $$

$$ a \< 1 $$

This backslash somehow choked mdBook-KaTeX. This is a bug.

The reason why there is a backslash, though, is that mdbook-graphviz uses pulldown-cmark-to-cmark to convert parsed Markdown back to text.

SichangHe commented 2 months ago

What happened above was KaTeX catching the invalid \< and throwing an exception. When the exception is returned to Rust, mdBook-KaTeX simply ignored it and sticked in the content within the delimiters. This confused me.

I have changed the implementation to log a warning of the exception when this happens, and put the delimiters back for sanity.

A nicer option when editing, though, is to set throw-on-error to false, which results in red error messages baked into the rendering output. @mmicu, maybe you could consider that.