Image preview for flowing inline LaTeX math
Does this feature exist in Emacs orgmode core?
Yes
Orgmode link
https://orgmode.org/manual/Previewing-LaTeX-fragments.html
Feature value
There is already partial support for rendering a LaTeX preview as an image of block environments, e.g.
\[
a + b
\]
and even the inline (the newlines here are important)
\(
a + b
\)
will conceal the text in the buffer and replace it with an image of the rendered LaTeX, using snacks.nvim.
However, it's really unnatural to write inline LaTeX in such a way; really, you want to be able to write it like:
Consider \( a = b + c \), blah blah blah; then we can conclude \( d \) ...
without newlines between the inline math delimiters \( and \).
Currently, orgmode is able to recognise this markup and highlight the inline math fragments (#428), but not present the contents to snacks.nvim in order to render a LaTeX preview.
Additional context
The existing image preview functionality was introduced in a series of PRs:
- #945
- #935
- #907
Unfortunately, I think it's not possible to merely tweak the tree-sitter queries in https://github.com/nvim-orgmode/orgmode/blob/master/queries/org/images.scm to achieve the desired functionality.
This is because the current tree-sitter parser will parse \( a + b \) into (paragraph (expr) (expr) (expr) (expr) (expr)), where each (expr) corresponds to each space-delimited word.
In contrast,
\(
a + b
\)
parses into (latex_env contents: (contents (…))), where the contents node represents the parse of a + b.
In other words, we would need some kind of latex_inline node (the target of a tree-sitter query) which contains only the expr nodes within \( and \), which requires changes to the tree-sitter parser.
In principle, the changes to the tree-sitter parser aren't going to be too hard to implement; but it feels like there would be other changes within the plugin to take into account such a new latex_inline node in the grammar, and I don't really know where to start with that.
Thanks for the PR on tree-sitter parser! Since you are familiar with LaTeX, does it support syntax like this?
\{ something \}
That type of parsing was added some time ago when we added basic LaTeX support, and I assume it exists in the latex syntax, but I can't find anything about it now.
I'll see to handle the changes in here with the new tree-sitter changes.
@NickHu can you test PR https://github.com/nvim-orgmode/orgmode/pull/1059 and see how it works for you with the latest changes to tree-sitter-org?
Thanks for the PR on tree-sitter parser! Since you are familiar with LaTeX, does it support syntax like this?
\{ something \}That type of parsing was added some time ago when we added basic LaTeX support, and I assume it exists in the latex syntax, but I can't find anything about it now.
This would be used for something like literal curly brackets within math mode (extremely common for writing down sets etc.), for example:
\( X = \{ x \in \mathbb{N} \mid x > 0 \} \)
which is
$$ X = \{ x \in \mathbb{N} \mid x > 0 \} $$
because unescaped {} are used to denote grouped pieces of math (e.g. a superscript/subscript group in ( x^{i+1} ).
Other delimiters in math mode look like: (), [], \left( \right), \left{ \right} etc. (\left and \right here trigger special behaviour allowing the delimiters to track their nesting and adjust their height in the output).
But I don't think you need to care about it here.
In LaTeX, basically you can think of it as being in text mode, and within that dropping in to either inline math mode with $x$ or \( x \) or display math mode with $$x$$ or \[ x \] (whitespace is not important).
In the case of display math, I feel like the square bracket syntax is generally preferred, but for inline math both options are common. (Although, I did have to use $$ to get the math to render in this comment, it's MathJax default behaviour iirc.)
I wouldn't really consider this feature complete if it only supported \( x \) and not $ x $ for what it's worth, but it's a start.
There are other ways to get into display math mode too, e.g. with \begin{equation} ... \end{equation} or one of its many many variants.
There's also an antiquoting mechanism with the \text (and variants) command which is commonly used to add text in the middle of mathematics: e.g. \[ X = \{ x \in \mathbb{N} \mid \text{$x$ is even} \} \] which renders like:
$$ X = \{ x \in \mathbb{N} \mid \text{$x$ is even} \}. $$
Here, there is some text mode and nested within that some more inline math mode. You probably don't want to deal with nested environments like this in this plugin though; if you gave the contents of the outer environment to Snacks it should do the right thing.
Ok, thanks for the feedback! If that's the case, then I believe I can remove the custom markup highlighting for those.Then I'm only left with highlighting these macro commands (I think they are called like that):
\something
\something{value}
Those might be trickier to add to tree-sitter parser, but we will see.
Once you confirm that https://github.com/nvim-orgmode/orgmode/pull/1059 is ok, I'll go ahead and release it.