A Quick Guide on Writing LaTeX in Neovim

Series - Editors

In last post, I shared a minimal configuration of LaTeX Workshop.

Now, I would like to share something about setting Neovim for VimTeX, texlab, LuaSnip and auto-completion.

This may not be suitable for everyone, especially for those who are not familiar with TUI.

I am using Neovim on macOS, and these configurations should also work well on Linux.

For Windows user, I recommend to install WSL.

Use a modern terminal, such as kitty, alacritty and iTerm2.

On macOS, the next thing that I recommend you to do is installing Homebrew. Here is the installation guide.

If you are using an M1 Mac, please check here for more details, especially export /opt/homebrew/ to your $PATH.

As in the last post, we need to install a TeX distribution.

For macOS users, you may download mactex-no-gui via Homebrew:

sh

brew install mactex-no-gui

Here is the official instructions for installing Neovim on different platforms.

For macOS users, you can install Neovim with Homebrew:

sh

brew install neovim

Note that Neovim 0.7 was released on April 15, 2022. Please ensure that you have the correct version (>= 0.7).

Since Neovim is a fork of Vim, the basic usage is the same.

Follow this repo to learn how to use Vim and Neovim.

If you are new to Neovim, I suggest you to start with this configuration.

Here is my current configuration.

Since Lua is the first-class language in Neovim, some basic knowledge of Lua will be better. If you want to costuming your own Neovim, you may want to learn nvim-lua-guide.

VimTeX is a powerful tool to

LuaSnip is a snippet engine, which provides the actual snippets for various file types.

I will use the following examples to explain how to use LuaSnip.

lua

s({ trig = 'mk', name = 'inline math', dscr = 'Insert inline Math Environment.' }, {
        t '\\(',
        i(1),
        t '\\)',
    }, {
        condition = context.in_text,
        show_condition = context.in_text,
        callbacks = {
            [-1] = { [events.leave] = appended_space_after_insert },
        },
    })

I set mk as the trigger for inline math environments. Also, this is an auto-sinppet, which means that you can just type mk and it will be replaced with \(|\) automatically. Note that | above is used to indicate the position of the cursor. After the mk is typed, you can type the math formulas you need and then press Ctrl + j to escape the inline math environment. Now the real magic is here. After escaping the inline math environment, you will see something like this: \(formula\)|, where | is the cursor as before. Then LusSnip with insert space when the next character after snippet is a letter. Note that for non-letter characters, LusSnip will not insert space.

lua

s({ trig = 'cite', name = 'cross refrence' }, {
        t '\\cite[',
        i(1),
        t ']{',
        i(2),
        t '}',
    }, {
        condition = context.in_text,
        show_condition = context.in_text,
        callbacks = {
            [2] = {
                [events.enter] = function()
                    require('telescope').extensions.bibtex.bibtex(
                        require('telescope.themes').get_dropdown { previewer = false }
                    )
                end,
            },
        },
    })

I use cite as the trigger for cross-references snippet. Because cross-references are not often used in the text, I set cite as a normal snippet: after typing cite, you need to press Ctrl + j to expand the snippet and get something like \cite[|]{}. Then you can input some text in the square brackets, and then press Ctrl + j to jump to the curly braces. According to the settings, you will see a dropdown with the list of all the references in the document. This is a powerful feature, thanks to LuaSnip and telescope-bibtex.

When writing LaTeX source files, auto-completion is crucial for fast editing and improve our efficiency dramatically. VimTeX and LSP support auto-completion.

  • finish Preparations
  • finish VimTeX
  • finish LuaSnip
  • finish cmp
  1. A Complete Guide on Writing LaTeX with Vimtex in Neovim

  2. How I’m able to take notes in mathematics lectures using LaTeX and Vim

  3. config.nvim

  4. nvim-cmp

  5. Introduction to LuaSnip

  6. LuaSnips - Advanced Configuration

Related Content