nvim-neorg / neorg

Modernity meets insane extensibility. The future of organizing your life in Neovim.
GNU General Public License v3.0
6.31k stars 209 forks source link

LaTeX inline_math parsered not porperly #1562

Open dirichy opened 3 weeks ago

dirichy commented 3 weeks ago

Prerequisites

Neovim Version

NVIM v0.10.0 Build type: Release LuaJIT 2.1.1713484068 Run "nvim -V1 -v" for more info

Neorg setup

require(“neorg”).setup()

Actual behavior

I have some snip and conceal for latex only in mathmode. This code:

$|\alpha|$

is not parsed as latex mathmode. I can’t use mathmodesnip and mathmode conceal in it.

Expected behavior

Change the injection.scm, remove the line contains “offset” in latex injection, then the “$”will pass to latex parser. So that we can know it’s in mathmode form latex parser. IMG_4884

Steps to reproduce

$|\alpha|$

Put your cursor on “\alpha” and run this function(used in latex file to detect whether the cursor is in mathmode)

MATH_NODES = {
        displayed_equation = true,
        inline_formula = true,
        math_environment = true,
}

TEXT_NODES = {
        text_mode = true,
        label_definition = true,
        label_reference = true,
}
function LATEX_IN_MATH()
        local node = require(“nvim-treesitter.ts_utils”).get_node_at_cursor()
        while node do
                if TEXT_NODES[node:type()] then
                        return false
                elseif MATH_NODES[node:type()] then
                        return true
                end
                node = node:parent()
        end
        return false
end
print(LATEX_IN_MATH())

will return false, but it should return true.

Potentially conflicting plugins

No response

Other information

No response

Help

Yes

Implementation help

just remove the offset line in injection.scm for inline_math will work.

max397574 commented 3 weeks ago

I think this isn't a bug because this is latex math yes but the $ signs are part of norg syntax so it's correct that just everything inbetween is seen as latex which makes it not being recognizes as math in latex

it's not something wanted but it's the correct behavior

dirichy commented 3 weeks ago

I know $ is part of norg syntax, but it is also used in latex syntax. More exactly, the injection language is not latex, instead it’s latex_inline_math. In nvim-treesitter for markdown, they pass the $ to latex parser to specify the inner is latex_inline_math, not a normal latex file. The “$” is also part of markdown syntax, but they also pass it. So I think in norg, we can do it, too, to let some latex thing based on treesitter works in norg inline_math, too. Thank you!

dirichy commented 3 weeks ago

I am making a plug for latex and all latex-injected language, and I want it work in norg, too.

max397574 commented 3 weeks ago

well if latex_inline_math is a separate parser then it shouldn't require a $ being present at the start that's imo a flaw of the parser then

dirichy commented 3 weeks ago

Since there is no separate parser for latex_inline_math, we need to pass $ to tell latex parser it’s inline_math in it. That’s how nvim-treesitter do for markdown. I know maybe it’s hacking, but it works. If you think I am wrong, you can just close this issue. maybe I well overwrite injections.scm in my plug. Thank you!

benlubas commented 1 week ago

@dirichy how do you suggest we pass a $ when $|\alpha|$ is used? This would then parse as "the absolute value of alpha", right?

dirichy commented 1 week ago

I tried remove the #offset! line and it works will, since “|” is named node in norg parser, it will not pass to latex parser.

dirichy commented 1 week ago

BTW, if you really think the part of neorg syntax should not be passed, the offset number should be 2, not 1, since “|” is norg syntax, too, but now pass to latex parser.