Grammar and Spell Checker in Neovim with LTeX Language Server

LTeX Language Server is an LSP for LanguageTool that can be embedded into Nvim LSP protocol to provide real time grammar and spell checking for most commonly used text formats such as Markdown, LaTeX, and reStructuredText. In this guide, I will show how to add LTeX Language Server to an existing LSP setup with nvim-lspconfig and Mason, and how to add a custom dictionary for words that are not recognized by the LanguageTool proofreading service.

Custom Dictionary Using Built-in Vim Spell Checker

Vim and Neovim come with a built-in spell checker tool spell that can generate a spell file, which we can use to give LTeX Language Server an additional dictionary.

First, we define the spell file location as follows:

vim.opt.spellfile = vim.fn.stdpath("config") .. "/spell/en.utf-8.add"

This will tell Neovim the location of the spell file dictionary at ~/.config/nvim/spell/en.utf-8.add. We need to make sure this file exists in our system. We can create it by doing the following:

mkdir -p ~/.config/nvim/spell
touch ~/.config/nvim/spell/en.utf-8.add

To add words to this dictionary in Neovim you can add the word under the cursor by typing zg.

Adding the Custom Dictionary to LTeX Language Server

Next, we create a Lua function that loops all the words in the spell file and creates a table of words from it.

local words = {}
for word in io.open(vim.fn.stdpath("config") .. "/spell/en.utf-8.add", "r"):lines() do
	table.insert(words, word)
end

Finally, in order to have LTeX Language Server use this words table as an additional dictionary we configure the following in the LTeX Language Server settings. This assumes you already have LTeX Language Server installed and configured in Neovim:

--
-- ...
--
settings = {
    ltex = {
        dictionary = {
            ["en-US"] = words,
        },
    },
},
--
-- ...
--

Conclusion

It is very straightforward to add a custom dictionary to LTeX Language Server in Neovim. This guide did not cover all the initial setup for getting an LSP up and running in Neovim, but if you need a reference for that you can check my Neovim configuration on GitHub. You can also reach out to me via email at miika@miikanissi.com.