Inline Math: Allow white spaces before closing dollar signs

Use case or problem

Currently, Obsidian recognizes $...$ as inline math only if there is no whitespace before the closing dollar sign. For example,

  • $S_n$ is inline math
    • editing mode: image
    • reading mode: image
  • but $S_n $ is not.
    • editing mode: image
    • reading mode: image

This causes user-UNfriendly behavior when we type inline math equations.
That is, every time we type a whitespace, the font changes from the math font (colored monospace in italics) to the normal text font, like this:
ezgif-3-76ababde5e

I personally feel this behavior is problematic because it distracts me a little and prevents me from focusing on typing.

Moreover, other markdown editors (MarkText, VSCode, etc.) allow us to use white spaces before closing dollar signs with no problem. In other words, we don’t see the flickering problem in other editors.

In summary, we can avoid this flickering problem and improve our math typing experience by allowing white spaces before closing dollar signs.

Proposed solution

I’m sorry that I don’t have any specific implementation ideas, but it seems that we have to modify the current mechanism of how Obsidian’s markdown parser works.

Current workaround (optional)

It’s rather a dirty workaround, but we can avoid the font change white typing inline math by using $(...) \hspace{0em}$.

Related feature requests (optional)

I couldn’t find related FRs, but this FR was originally motivated by discussions in two posts in this forum:

5 Likes

I agree with you

My Workaround

  • $(…){}$
  • ${(…)}$

Wow, your workarounds look much better than mine, especially ${(...)}$!
Thank you a lot!

Maybe they could simulate ${ }$ when pairs of $$ are detected. Anyway I would like to see this feature implemented in 2 years.

I’ve just learned why whitespaces before closing $s are not allowed.

The MathJax Document says:

The default math delimiters are $...$ and \[...\] for displayed mathematics, and \(...\) for in-line mathematics. Note in particular that the $...$ in-line delimiters are not used by default. That is because dollar signs appear too often in non-mathematical settings, which could cause some text to be treated as mathematics unexpectedly. For example, with single-dollar delimiters, “… the cost is $2.50 for the first one, and $2.00 for each additional one …” would cause the phrase “2.50 for the first one, and” to be treated as mathematics since it falls between dollar signs. See the section on TeX and LaTeX Math Delimiters for more information on using dollar signs as delimiters.

Given this fact, it would be difficult to simulate ${}$ by $$ without causing any unexpected behavior. I personally recommend using the Latex Suite plugin to enter ${ }$ quickly. See the linked post below for the details:

P.S.
Alternatively, you can easily implement a similar feature if you make your own plugin; just paste the following code inside the onload() method of your plugin.


this.addCommand({
    id: 'insert-inline-math',
    name: 'Insert inline math',
    editorCallback: (editor) => {
        let cursorPos = editor.getCursor();
        editor.replaceRange('${  }$', cursorPos);
        cursorPos.ch += 3;
        editor.setCursor(cursorPos);
    }
});
1 Like

Espanso can help too!

So what you think about adopting \( \) for inline math? I personally find that more elegant given that \( has no any meaning in natural language. What comes to typing efficiency, one has to deal that problem separately. But typing efficiency is not only limited to how to write \( quickly and conveniently, it’s much broader topic of interest.

I had never heard of Espanso before. Really looks great, thank you!

Typing efficiency will not be a big problem if we use some appropriate tools.

However, I think \( )\ has other limitations too.

First of all, Obsidian doesn’t support it out of the box.

Moreover, using \( )\ instead of $ $ will cause compatibility issues for several community plugins that assume $ $ only, such as MathLinks (+ maybe Latex Suite, Quick Latex, and so on).

If Obsidian officially supports \( )\ in the future, that would be a different story.

Hi everyone @AdrianoCzelentano @blue_emperor @anon63144152 @Mara-Li ,
thank you so much for sharing your ideas.

I’ve finally made a plugin to solve this problem: No More Flickering Inline Math!

I couldn’t make it without your help. I truly appreciate it.
If you’re interested, please follow the installation instructions in README.

Thank you!

4 Likes

The plugin is nice to use. Hope the Obsidian official can solve this problem.

1 Like