Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completion is missing equation labels when using autonum #2976

Open
ces42 opened this issue Jul 8, 2024 · 4 comments
Open

completion is missing equation labels when using autonum #2976

ces42 opened this issue Jul 8, 2024 · 4 comments

Comments

@ces42
Copy link
Contributor

ces42 commented Jul 8, 2024

This is a rather low-priority issue.
Is your feature request related to a problem? Please describe it.
When using the autonum package1 equations that are \labeled but not \referenced don't appear in the aux file. Hence they don't appear in vimtex's completion.

Describe the solution you'd like
Maybe it would be possible to just search the current file for \labels and add those to the completion function?. This would also fix \labels not appearing in completion if the document has not been compiled since they were added (which happens frequently because most of time you add a \label you will \reference it right away.)

Footnotes

  1. The main feature of this package is that \labeled equations in the source that don't \referenced don't get labeled in the pdf

@lervag
Copy link
Owner

lervag commented Jul 8, 2024

Interesting request. I have not heard of autonum before.

Describe the solution you'd like Maybe it would be possible to just search the current file for \labels and add those to the completion function?. This would also fix \labels not appearing in completion if the document has not been compiled since they were added

Well, it's a relatively big and complex change, because we would need to merge the results. To be honest, I'm not very motivated to work on this, as I don't see myself needing or using this feature. But I won't mind considering a PR for it.

(which happens frequently because most of time you add a \label you will \reference it right away.)

For me, this never really happened frequently. Perhaps because I save more often than you?

@shivangp76
Copy link
Contributor

I don't use the autonum package, but I've also wanted \labels to be used for completion without compiling the document first. I've been using the following code as a workaround. Instead of adding it to the completion function, it creates a new keybinding. Hope this somewhat helps with your issue. Note that the fzf package used here is ibhagwan/fzf-lua.

vim.cmd([[
function! OrderLabels(l1, l2)
  return a:l1[1] == a:l2[1] ? 0 : a:l1[1] > a:l2[1] ? 1 : -1
endfunction
" Sorts labels by proximity to the cursor position, since recently created labels are likely to be references. Also removes VimTeX's dependence on LaTeX rendering to get labels.
function! GetLabels()
  let l:result = []
  let l:regex_pattern = '\\label{\([^}]*\)}'
  let l:cursor_line = line('.')
  for line_number in range(1, line('$'))
    let l:line = getline(l:line_number)
    let l:distance = abs(l:cursor_line - l:line_number)
    call substitute(l:line, l:regex_pattern, '\=add(l:result, [submatch(1), distance])', 'g')
  endfor
  return sort(l:result, "OrderLabels")
endfunction
function! PasteLabel(label)
  execute "normal! a" . a:label
  call feedkeys('a')
endfunction
function! FzfLabels()
  call fzf#run({
      \'source': map(GetLabels(), 'v:val[0]'),
      \'sink': function('PasteLabel'),
      \'window': {'width': 0.9, 'height': 0.6},
      \})
endfunction
]])
vim.keymap.set('i', '<C-f>', '<Esc>:call FzfLabels()<CR>', { silent = true, noremap = true, buffer = true })

@lervag
Copy link
Owner

lervag commented Aug 14, 2024

Ok, let's say I were to consider implementing label completion based on manual parsing of the LaTeX file. How should it behave with respect to the current completion parser? For instance, this might work:

  1. If the document is not compiled, then we fallback to a manual parsing that will only show the available labels without any additional metadata.
  2. If the document is compiled, then use the current parser that brings more metadata.
  3. Same as 2, but combine the results to include labels from the manual parsers (1) if they are not present in the "fancy" parser.

Thoughts?

@shivangp76
Copy link
Contributor

That sounds good to me, assuming "compiled" means "compiled with the latest changes." In other words, if we can ensure manual parsing will add no benefit, then we will skip it and just do fancy parsing. Otherwise, do manual and fancy parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants