# Remove HTML tags when wrapping with MathJax tags

When you have long latex expressions (like with \begin{align*}...\end{align*}), you usually want to write them on multiple lines. If you do so, the editor will add annoying <div> or <br> HTML tags, which will make the LaTeX go wrong. Currently, the only way to patch this is to open the HTML editor and to patch it by hand.

To prevent this behavior, it would be nice if:

• the editor actually made the linefeeds visually break to new line (just for editing purposes), unlike HTML does;
• the editor added linefeeds when your press enter if you are between MathJax tags;
• the editor replaces <br> and <div>...</div> tags with linefeeds in the selected region when wrap it with MathJax tags (by clicking on the MathJax button or by using the key binding).

It would still be possible to have <div> and <br> tags (if you wrote by hand the MathJax tags, for example), but it would solve the issue for 90% of the use cases.

Also, when the Anki renderer detects HTML tags within MathJax tags, it could prompt a warning message, because otherwise it could hard to find out the reason why LaTeX doesn’t render.

Actually, you can already use Shift+Enter to insert newlines in MathJax expressions in a way that doesn’t break them.

https://docs.ankiweb.net/math.html#mathjax

If you want to use newlines in a MathJax expression, please use Shift+Enter instead of just Enter, as a normal newline will prevent MathJax from working correctly.

2 Likes

In fact, I believe Anki is using <br> by default as of 2.1.41

1 Like

@abdo
This is not about <br> vs <div>, but rather about the literal linefeed &#10;. @BlackBeans wants to use linefeeds for structuring the MathJax and writing it in multiple lines, not for actual linebreaks in the output.

If those linefeeds are expressed as <br> in HTML, these will be translated to Mathjax \\.

@BlackBeans
I intend to tackle the MathJax issue soon. The default editing mode (contenteditable) does not work well for MathJax. Editing in a mode that disregards HTML syntax (like a textarea) would work much better here. There are other issues as well, for example it would be nice to easily preview the output MathJax directly in the editor, without having to use the previewer. I’ll try to tackle them for the next major Anki version

3 Likes

This is not about <br> vs <div> , but rather about the literal linefeed &#10; . @BlackBeans wants to use linefeeds for structuring the MathJax and writing it in multiple lines, not for actual linebreaks in the output.

Exactly

Editing in a mode that disregards HTML syntax (like a textarea) would work much better here.

The best would be something like org-mode with babel, in which certain regions of text can be parsed as an arbitrary language. An other way to do that would be to have an “AST editor”, like a projectional editor, although both of these solutions are quite hard to come up with in a satisfying way.

Going for a simple textarea is probably the best way, since Anki is not an editor in itself (although adding and editing notes is quite a big part of the workflow with Anki)

it would be nice to easily preview the output MathJax directly in the editor, without having to use the previewer.

Indeed, this would be wonderful!

I’ll try to tackle them for the next major Anki version

@BlackBeans
I’ve worked on an in-line Mathjax editor over the past few days, and you can see the result here. It even has syntax highlighting

3 Likes
• Hides some implementation details of Mathjax from user

This is major. Thanks for yet another awesome improvement to the editor - you’re doing god’s work, Henrik!

1 Like

This is really awesome. I think that the final step of improving the editor would be to launch an emacs server, and to create an emacsclient frame for each editing field, with an Anki mode… This seems crazy, but I think this would require overall less work than building a full-featured editor.