Add "highlighting" to LaTeX formula based on cursor position

Hi,

Obsidian is great for LaTeX embedding in notes

Recently, I used Obsidian for making some notes on some math classes I teach. I think that Obsidian is already great on its own with LaTeX. I really like the fact to type my blocks of LaTeX and I can see the preview in the same time. Plus with the Quick LaTeX for Obsidian plugin, it’s wild how fast I can type my equation.

Main point of the idea

I have the idea of another comfortable improvement. It could be part of Obsidian or a plugin. It would be amazing to add an automatic highlighting on the LaTeX formula based on the position of the cursor in the LaTeX block code.

An example

For example let’s have some formula line for illustration:

$$
\begin{align*}
... Some other lines
& \int_{-\infty}^{+\infty} X_{n}(u) \phi(u) du = \int_{-\infty}^{+\infty} x_{n}(v) \Phi(v) dv\\
... Some other lines
\end{align*}
$$

This line is quite long and messy, It’s easy to get lost of what’s going on while editing the LaTeX code. If I want to edit a typo - or anything more complicated - I should read my line with pain.

But, if my cursor is on \phi(u) for example, it would be nice to have an highlight on LaTeX preview.

Plus, I use the vim mode in Obsidian. It would be so nice as I am browsing the text in normal mode, I could see my position on the LaTeX formula preview.

Some details and thoughts

I would like to discuss two things here :

  • “The cursor is on something”, the cursor is considered to be on something when it is somewhere between the beginning and the end of something. For example, with \phi(u) as something and | is the cursor location:
    • the cursor is considered to be on \phi(u)
      • |\phi(u)
      • \phi|(u)
      • \phi(u)|
    • the cursor is NOT considered to be on \phi(u)
      • | \phi(u)
      • \phi(u) |
  • The highlight should be light, it should have enough opacity to be seen but not too much to keep the formula preview readable.

I do not know if this feature would be welcomed by anyone. IMHO, it would be very comfortable.

Plus, I know that I only scratched the surface of the problem. Namely, with my small and easy example, we could argue that if the cursor is on u then the \phi should not be highlighted. There are several other cases - sometimes more complex - that we should deal with: curly braces syntax, environment, \left and \right and so on. I think this feature would need a tree-sitter-like library in order to highlight things correctly. Actually using tree-sitter code for LaTeX would be very handy.

I would be interested to know your thoughts about the idea :grin:. Is it an interesting feature in your opinion? What do you think of the highlighting a LaTeX formula preview should look like? How could we lazily detect what we should highlight?

See you, thanks for your reading if you get this far :wink: