# Bug : Extra characters injected when there is space in MathJax equation

Hello,

After a recent update, I noticed that '&nbsp;' without the quotes was getting injected into equations written using MathJax, or specifically using  to write math formulas. The string gets appended if there are multiple spaces in the equation and is 100% reproducible and is impacting previously written cards also. Is anyone else seeing this? Below are some examples of the original text and converted text. Replace 'space' with actual space.


$$space this space$$ => this
$$space space this space$$ => &nbsp;this
$$space space space this space$$ => &nbsp; &nbsp; this

I can survive without a fix but it gets a bit annoying sometimes. Ankiweb app details below
Version ⁨2.1.54 (b6a7760c)⁩
Python 3.9.7 Qt 5.15.2 PyQt 5.15.5

Thanks

This should be fixed in the .55 release.

Thank you for your response, will flag your solution once I get and verify the .55 release.

You can test with the beta: Anki 2.1.55 Beta

Thank you for your suggestion. I am following the issues on Anki specifically while using MathJax. I think there are still a lot of issues to be resolved so will wait a few more weeks. I aggressively use MathJax.

I can confirm that Anki 2.1.55 Beta fixes the spurious addition of non-breaking spaces within equations, but &nbsp; is still inserted after every in-inline MathJax equation

For example, if I type "(a = b) " (with a space at the end), then the resulting HTML is $$a = b$$&nbsp;.

Is this the intended behavior? The nonbreaking spaces appear to have no effect on where line breaks actually occur after equations.

Furthermore, if &nbsp; ends up inside a MathJax equation (as often happens in my notes), then the MathJax preview fails to render.

I have noticed something which may be related to this on Anki ⁨2.1.54 (b6a7760c)⁩.

When selecting text which is surrounded by a space or spaces on one or more sides, the spaces are converted to non-breaking spaces ( ), and this causes the MathJax to be displayed with extra space around it. Considering that MathJax can’t wrap round, it is extremely likely, especially on mobile devices, that the non-breaking space just wastes space on the screen.

I believe this may be something to do with how Anki checks when there are two spaces in a row, and if so converts one to a non-breaking space. This may be being triggered when converting selected text to MathJax (this happens when using either the button or the keyboard shortcuts).

I believe that this behaviour should be removed, as it has a negative effect on formatting, or alternatively an option to remove this behaviour in menu as I regularly have to “Find and Replace” the non-breaking spaces to regular spaces.

As far as I’m aware, this is being done by the web toolkit rather than Anki’s code.