lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.5k stars 389 forks source link

Synctex doesn't work on the first file compiled in a neovim session #3018

Closed firubat closed 1 week ago

firubat commented 1 week ago

Description

If I open a neovim session, load a .tex file and compile it, (with default <localleader>ll ) it gets compiled and viewed, but synctex doesn't work (no backward or forward search).

If I run VimtexView another instance of the viewer (zathura) will open (still with no synctex). I tried closing the viewer and using VimtexView, tried stopping and resuming compilation, tried closing the buffer and reopening it - all won't get synctex to work, until I reload vimtex (VimtexReload), after which things start working as expected.

If I open a second file (before reloading vimtex) and compile it, synctex will work for that file.

Steps to reproduce

I use the LazyVim distro, a minimal configuration for vimtex is with the following lua:

return {
  {
    "lervag/vimtex",
    -- settings based on https://www.lazyvim.org/extras/lang/tex
    lazy = false, -- lazy-loading will disable inverse search
    config = function()
      vim.api.nvim_create_autocmd({ "FileType" }, {
        group = vim.api.nvim_create_augroup("lazyvim_vimtex_conceal", { clear = true }),
        pattern = { "bib", "tex", "sty" },
        callback = function()
          vim.g.vimtex_quickfix_method = vim.fn.executable("pplatex") == 1 and "pplatex" or "latexlog"
          vim.g.vimtex_view_method = "zathura"
          vim.cmd([[setl iskeyword+=\]])
        end,
      })
    end,
  },
}

(I don't use the lang.tex LazyVim Extra)

Expected behavior

No response

Actual behavior

No response

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: Linux Mint 21.2
  Vim version: NVIM v0.10.0
  Has clientserver: true
  Servername: /run/user/1000/nvim.11131.0

VimTeX project: MW pdflatex
  base: MW pdflatex.tex
  root: /home/ur/Dropbox/LyX and LaTeX
  tex: /home/ur/Dropbox/LyX and LaTeX/MW pdflatex.tex
  main parser: current file verified
  document class: article
  packages: atbegshi atbegshi-ltx atveryend atveryend-ltx auxhook bigintcalc bitset etexcmds gettitlestring hycolor hyperref iftex infwarerr intcalc keyval kvdefinekeys kvoptions kvsetkeys letltxmacro ltxcmds nameref pdfescape pdftexcmds refcount rerunfilecheck uniquecounter url
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
    job: 
      jobid: 6
      output: /tmp/nvim.ur/Xtk5io/0
      cmd: max_print_line=2000 latexmk -verbose -file-line-error -synctex=1 -interaction=nonstopmode  -pdf -pvc -pvctimeout- -view=none -e '$compiling_cmd = ($compiling_cmd ? $compiling_cmd . " ; " : "") . "echo vimtex_compiler_callback_compiling"' -e '$success_cmd = ($success_cmd ? $success_cmd . " ; " : "") . "echo vimtex_compiler_callback_success"' -e '$failure_cmd = ($failure_cmd ? $failure_cmd . " ; " : "") . "echo vimtex_compiler_callback_failure"' 'MW pdflatex.tex'
      pid: 11225
  viewer: General
    job: 
      pid: -
      cmd: xdg-open '/home/ur/Dropbox/LyX and LaTeX/MW pdflatex.pdf'
  qf method: LaTeX logfile
lervag commented 1 week ago

If I open a neovim session, load a .tex file and compile it, (with default <localleader>ll ) it gets compiled and viewed, but synctex doesn't work (no backward or forward search).

Ok, that's strange.

I use the LazyVim distro, a minimal configuration for vimtex is with the following lua: …

Well, this feels like a bad suggested configuration. And I believe it may be the reason things don't work well for you.

First, why did you add the FileType autocommand? It is not suggested in the LazyVim config and it does not make sense. I also notice that LazyVim suggests the config function, but you should use the init function.

So, please try the following config instead:

return {
  {
    "lervag/vimtex",
    lazy = false,
    init = function()
      vim.g.vimtex_view_method = "zathura"
    end,
  },
}
firubat commented 1 week ago

Well seems I was basing my config on an old version of the LazyVim config, which have since then changed without me noticing.

Anyway your config works as expected, thanks!

lervag commented 1 week ago

Great; glad to hear it works now! I made a PR to the LazyVim docs to fix the config vs init thing as well. Let's hope that helps other people avoid issues like this in the future.

firubat commented 1 week ago

Actually it works with either config or init, but with init some commands didn't take effect. For example I have this command:

vim.cmd([[highlight Conceal guifg=#9ccfd8]]) 

which doesn't work with init but does work with config. Don't ask me why.

firubat commented 1 week ago

my hypothesis is that with the filetype autocommand, there was some delay in loading the viewer configuration for the first time, so it didn't work.

lervag commented 1 week ago

I can tell you why: With init, the command will run very early and the highlight will likely be overwritten by your colorscheme. But: You should not add custom highlights like this. I've written about this stuff in :help vimtex-syntax-core, so I recommend your read that and one or more of the links provided there.

firubat commented 1 week ago

Thanks for the tip, seems I have a lot to read :)

lervag commented 1 week ago

No problem, and yes, unfortunately, there's a lot to read. But I think you can get far by skimming much of it. 😅