lzanini / mdbook-katex

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

Backslashes process Error(?), like $$ \\\\begin #100

Closed Demian101 closed 9 months ago

Demian101 commented 9 months ago

I wonder if mdbook-katexcan support:

$$ \\\\begin{array}{|c|c|c|c|c|} \\\\hline 1 & x_1 & x_2 & x_3 & out \\\\ \\"
"\\hline 0 & 1 & 1 & 0 & 0 \\\\ \\\\hline \\\\end{array} $$

The question is from : https://github.com/google/mdbook-i18n-helpers/issues/105

Thanks a lot if any reply!! 🦀

important for me 🤣

mgeisler commented 9 months ago

Hi @Demian101, actually, looking at this again, I don't think the output would ever have four backslashes.

The code above is what you see in the Gettext PO file: it backslash-escapes the \\ once more and so you end up with \\\\. However, when we generate Markdown using this translation, we remove one layer of backslashes.

So we should ultimately be converting

$$
\begin{array}{|c|c|c|c|c|}
\hline
1 & x_1 & x_2 & x_3 & out \\
\hline
0 & 1 & 1 & 0 & 0 \\
\hline
\end{array}
$$

into

$$ \\begin{array}{|c|c|c|c|c|} \\hline 1 & x_1 & x_2 & x_3 & out \\ \\hline 0 & 1 & 1 & 0 & 0 \\ \\hline \\end{array} $$

with the single backslashes turned into escaped backslashes.

Note: \\ becomes \\ after the conversion since \\ is already an escaped backslash according to the normal CommonMark rules for Backslash escapes. I don't know how mdbook-katex works, but I think this could be an important point: how does it distinguish between the escaped backslash in \\begin and in \\ where the latter is supposed to create a line break?

SichangHe commented 9 months ago
mgeisler commented 9 months ago
  • You should feed in mdBook-KaTeX normal math expressions, i.e., without escaping backslashes. Because, it runs before the markdown processor.

Oh, thank you for that, this clears up my questions! I somehow expected it to work with the parsed Markdown, but doing this on the raw input is of course much simpler :slightly_smiling_face:

It does make the task of mdbook-i18n-helpers a bit more complex since the tools there work on the Markdown AST. We now somehow have to emit Markdown as-if we were working on the raw input.

I think we can close the issue/question here — making this work is a matter between https://github.com/google/mdbook-i18n-helpers/issues/105 and possibly https://github.com/Byron/pulldown-cmark-to-cmark/issues/60.

Thanks for the help!

SichangHe commented 9 months ago

You're welcome. mdBook-KaTeX is designed for the user to have simple normal experience, not to work in a pipeline. For that purpose, it would probably be easier to use mdBook-KaTeX as a library. This is the function you want:

https://github.com/lzanini/mdbook-katex/blob/5302e9611b40e8248fa6c5d73fc48c5805258921/src/preprocess.rs#L78-L84