1

How do I execute :VimtexCompile automatically whenever I open a .tex file? As mentioned in this GitHub thread, this is done in Vimscript via:

augroup vimtex_config
  autocmd User VimtexEventInitPost VimtexCompile
augroup END

I've tried a bunch of variations of the following:

-- Example 1
local VimtexGroup = vim.api.nvim_create_augroup("VimtexGroup", { clear = true })
vim.api.nvim_create_autocmd({ "User" }, {
  pattern = "VimtexEventInitPost",
  group = VimtexGroup,
  command = ":VimtexCompile",
})

-- Example 2
local VimtexGroup = vim.api.nvim_create_augroup("VimtexGroup", { clear = true })
vim.api.nvim_create_autocmd({ "User" }, {
  pattern = "VimtexEventInitPost",
  group = VimtexGroup,
  callback = function()
    [[VimtexEventInitPre VimtexCompile]]
  end,
})

Honestly, the lua version of vimscript really confuses me, so I'm just guessing here.

Of course, I can just map something like <leader>c to ":VimtexCompile<CR>, but I'm specifically trying to do this with autocmd to better understand the lua version.

Thanks

1
  • VimTeX already has a lot of default mappings, e.g. \ll to start compilation. Thus, you don't need to make mappings yourself unless you want to remap. You should probably read the docs. :) Mar 20 at 11:15

2 Answers 2

1

This should work:

local augroup = vim.api.nvim_create_augroup("VimtexGroup", { clear = true })
vim.api.nvim_create_autocmd("User", {
  pattern = "VimtexEventInitPost",
  group = augroup,
  command = "VimtexCompile",
})

Notice you don't need the : here. The command is executed as a Vim command, which is what you type when you do :....

If this doesn't work, then you should open an issue on GitHub because it is likely a bug or that something is misconfigured on your end.

5
  • I think it's bugged because your snippet still isn't working for me. For context, I've been testing these in nvim/after/ftplugin/tex.lua. What's weird is it works when either (i) I change "VimtexEventInitPost" to "VimtexEventQuit", or (ii) I move the command to where I store all my other autocommands, e.g., lua/config/autocmds.lua. This is complicated by building off of the starter files from LazyVim, so there may be some side effects there. I'll file an issue if I don't see a similar one there already. Thanks. 2 days ago
  • 1
    Ah, no. If you put this under after/ftplugin/tex.lua you are creating the autocommand after the event VimtexEventInitPost is fired. You need to create the autocommand earlier, preferably during the sourcing of init.lua (either explicitly or implicitly from some file that is sourced by it). yesterday
  • 1
    Notice that VimTeX does not need any lazy loading! In fact, if you force lazy loading of VimTeX, you risk breaking expected behaviour. So make sure to not lazy load anything. yesterday
  • Your explanations really helped me understand how these config pieces are interacting. Thanks! yesterday
  • No problem; glad to hear it was helpful! yesterday
-1

The following ended up working for me. The issue was that I needed to also execute VimtexView.

local vimtex = vim.api.nvim_create_augroup("vimtex", { clear = true })
vim.api.nvim_create_autocmd("User", {
  pattern = "VimtexEventInitPost",
  command = ":VimtexCompile",
  group = vimtex,
})
vim.api.nvim_create_autocmd("User", {
  pattern = "VimtexEventInitPost",
  command = ":VimtexView",
  group = vimtex,
})
1
  • I downvoted this, because it is not true that you need to also execute :VimtexView. If you do, then what you want is not what you've asked for in your question. Mar 20 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.