nvim-neorg / neorg

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

latex-renderer throws an error due to width missing field when an equation outside of the visible range is conceal #1379

Open jonboh opened 5 months ago

jonboh commented 5 months ago

Prerequisites

Neovim Version

NVIM v0.10.0-dev-00e71d3 Build type: Release LuaJIT 2.1.1693350652

Neorg setup

require('neorg').setup({["load"] = {["core.concealer"] = { },["core.defaults"] = { },["core.dirman"] = {["config"] = {["workspaces"] = {["default"] = "~/doc/vault",["vault"] = "~/doc/vault"}}},["core.integrations.image"] = { },["core.integrations.telescope"] = { },["core.integrations.treesitter"] = { },["core.keybinds"] = {["config"] = {["hook"] = function(keybinds)
  --keybinds.unmap('norg', 'n', '<C-s>')

  keybinds.map(
    'norg',
    'n',
    '<localleader>o',
    ':Neorg return<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader>j',
    ':Neorg journal today<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader>c',
    ':Neorg toggle-concealer<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader><C-l>',
    ':Neorg render-latex<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader>f',
    ':Telescope neorg find_norg_files<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader>s',
    ':Telescope neorg find_linkable<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader>lf',
    ':Telescope neorg insert_file_link<CR>',
    {silent=true}
  )
  keybinds.map(
    'norg',
    'n',
    '<localleader>ls',
    ':Telescope neorg insert_link<CR>',
    {silent=true}
  )
end
}},["core.latex.renderer"] = {["config"] = {["bounded_geometry"] = false,["conceal"] = true,["dpi"] = 300,["render_on_enter"] = false}}}})

Actual behavior

When an equation is outside of the displayed lines of the buffer and you run Neorg render-latex, the latex-renderer will fail to conceal the equation because the image.rendered_geometry == {}, and so image.rendered_geometry.width will be nil, here's the relevant code.

Expected behavior

No error should be thrown. This could be fixed by getting the width of the image in a different way that does not relay on the image being currently rendered. A potential solution would be to not conceal equations when width==nil, however in that case the inline_math call should be added to the OnCursorMove event. Currently that change has an unacceptable performance hit.

Steps to reproduce

  $\min_\theta \mathbb{E} -\log p_\theta(x)$

  $\theta$ and $\tau$ and $\pi$ and $\gamma$ and $\epsilon$

  $\min_\theta \mathbb{E} -\log p_\theta(x)$

  $e^{i\pi} + 1 = 0$
  $a^2 + b^2 = c^2$
  $E = mc^2$
  $F = ma$
  $\hat{H}\psi = E\psi$
  $\nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_0}$
  $\nabla \cdot \mathbf{B} = 0$
  $dS \geq \frac{dQ}{T}$
                          $\gamma = \frac{1}{\sqrt{1 - \frac{v^2}{c^2}}}$
  $(i\gamma^\mu\partial_\mu - m)\psi = 0$
  $\e$

<add as many lines as needed to push the upper equations outside of the display>

Potentially conflicting plugins

No response

Other information

No response

Help

None

Implementation help

In the branch I'm currently using in my fork I've removed this error, here's the fix, but it relays in the user continuously re-rendering the equations to get the concealment. I can confirm, that with that change the error no longer appears.

jonboh commented 5 months ago

I'll try to test the change with the addition of render_inline_math inOnCursorMove with the changes that @benlubas has made to image.nvim to improved performance and see if that would be sufficient to solve the issue. In that case I'll send a PR.