HermanMartinus / bearblog

Free, no-nonsense, super fast blogging.
MIT License
2.36k stars 73 forks source link

How to write matrices in bear markdown LaTeX? #285

Closed ferntheplant closed 1 month ago

ferntheplant commented 1 month ago

I'm trying to include matrices in my blog content but can't get them working right. I've tried a few different LaTeX style syntaxes for rendering matrices but it seems like bear always renders it on a single line and parses the ampersands & weirdly. So far I've tried the following:

Plain Latex style
$$
  \left[ {\begin{array}{cccc}
    a_{11} & a_{12} & \cdots & a_{1n}\\
    a_{21} & a_{22} & \cdots & a_{2n}\\
    \vdots & \vdots & \ddots & \vdots\\
    a_{m1} & a_{m2} & \cdots & a_{mn}\\
  \end{array} } \right]
$$

Upmath style
$$
T^{\mu\nu}=\begin{pmatrix}
\varepsilon&0&0&0\\
0&\varepsilon/3&0&0\\
0&0&\varepsilon/3&0\\
0&0&0&\varepsilon/3
\end{pmatrix}
$$

Overleaf style
$$
\begin{vmatrix}
1 & 2 & 3\\
a & b & c
\end{vmatrix}
$$

I think there might be a bug in the parser converting the strings between $$ into MathML. This was tested on Firefox 125.0.3 on macOS. The matrices render on a single line like so:

image

ferntheplant commented 1 month ago

I tested a little more and found that I could get the matrix to span multiple lines by replacing the \\ with \\\\ in the math block. The following math block renders as such

$$
A_G = \begin{pmatrix} 1 & 2 & 3 \\\\ a & b & c \end{pmatrix}
$$

image

I didn't dig deep into mistune or beautiful soup but my theory is that either one of these packages are replacing the ampersands and double backslashes with their escaped versions. Since the math block rendering occurs last in the rendering chain (see custom_tags.py:117) the provided content no longer has the correct backslash and ampersand characters due to mistune or beautiful soup escaping them.

ferntheplant commented 1 month ago

Finally had some time to look into mistune. Fairly certain all that's needed to fix this is to register the custom math renderer (see render_latex()) as a mistune HTML renderer (similar to this example here).

I understand bear isn't open to contributions right now but I wouldn't mind taking a crack at this. @HermanMartinus let me know if you'd be open to a PR if you don't have the capacity to fix this right now

HermanMartinus commented 1 month ago

@ferntheplant Thanks for reporting this. I have some free time today so can give it a go :)

HermanMartinus commented 1 month ago

I didn't expect to rewrite the entire markdown renderer today, but it was well needed. Your examples are working now, and I managed to remove Beautiful Soup and leverage Mistune's custom renderer. Let me know if you have any other issues with LaTeX :)

HermanMartinus commented 1 month ago

One note, mistune uses $ for inline and $$ for block. Because Bear has previously supported $$ for both and determines whether it is inline by the context, I've written a basic pre-parser that turns inline $$ into single $ for backward compatibility.

I've updated the docs to use mistune's way going forward.

ferntheplant commented 1 month ago

Awesome thanks a ton!