Skip to content

Editor Configuration

Dominique edited this page Nov 19, 2023 · 7 revisions

This page explains how to configure several popular editors for an awesome LaTeX experience that will also help you write and format coauthor-friendly LateX code.

VSCode

On macOS, use brew install —cask code. On other platforms, install VSCode from https://code.visualstudio.com.

The best LaTeX editing experience with VSCode is via the LaTeX Workshop extension.

Configuring LaTeXIndent

Navigate to the LaTeX Workshop settings, type “latexindent” in the search bar, and ensure the parameters are exactly as follow:

IMG_0071

(If you are not on macOS, adjust the path to point to the most recent version of latexindent.)

Format your document by opening the palette (⌘-P) and type “format”, or use the shortcut (⌥-F). Alternatively, it is convenient to have the document formatted every time you save. In the LaTeX Workshop settings, search for “format on save” and activate the setting:

IMG_0072

Configuring ChkTeX

In the LaTeX Workshop settings, search for “chktex”. Activate the setting to lint with chktex and adjust the path to chktex if necessary:

image

Configuring LTeX

ltex should be enabled by default in LaTeX Workshop. However, make sure to adjust the following options:

  • Check “enable picky rules”.
  • Set your native language in “Ltex > Additional Rules: Mother Tongue”.
  • Set “Ltex: Check Frequency” to “save”.
  • Check “Ltex: Completion Enabled”.

Configuring TeXtidote

TeXtidote is currently not integrated with VSCode. A discussion describes how to define a build task to run TeXtidote and import its report into VSCode. A simple alternative is to just run TeXtidote from the command line on your LaTeX source files.

Where are the Diagnostics?

In VSCode, the chktex, ltex and textidote diagnostics are in the “Problems” tab of the bottom panel. Toggle the bottom panel by clicking on the button that looks like “ⓧ 2 ⚠ 3” in the bottom bar.

Lunarvim

Install Lunarvim as instructed at https://www.lunarvim.org.

Edit your Lunarvim configuration, usually stored in ~/.config/lvim/config.lua.

  • Install the nvim-texlabconfig and vim-textidote packages:
    lvim.plugins = {
      {
        "f3fora/nvim-texlabconfig",
        build = "go build", -- may have to do this manually
      },
      {
        “PatrBal/vim-textidote”,
      },
      -- your other plugins
    }
    and reload your configuration to make sure the new packages are installed. If the build action go build does not take place, you may have to go run that command in the folder where nvim-texlabconfig was installed.
  • Configure nvim-texlabconfig:
    require("texlabconfig").setup({
      cache_activate = true,
      cache_filetypes = { "tex", "bib" },
      cache_root = vim.fn.stdpath("cache"),
      reverse_search_edit_cmd = vim.cmd.edit,
      file_permission_mode = 438,
    })
  • Configure vim-textidote:
    vim.g.textidote_jar = '/opt/homebrew/opt/textidote/libexec/textidote.jar'
    vim.g.textidote_cmd = '/opt/homebrew/bin/textidote'
    vim.g.textidote_html_report = 0
  • Make sure lvim.builtin.treesitter.ensure_installed contains ”latex”:
    lvim.builtin.treesitter.ensure_installed = {
      "latex",
      -- etc.
    }
  • Make sure lvim.lsp.installer.setup.ensure_installed contains ”ltex” and ”texlab”:
    lvim.lsp.installer.setup.ensure_installed = {                                                                                                                                                
      "ltex",
      "texlab",
      -- etc.
    }
  • Configure the ltex and texlab language servers manually:
    vim.list_extend(lvim.lsp.automatic_configuration.skipped_servers, { "texlab", "ltex" })                                                                                                      
    require("lvim.lsp.manager").setup("texlab", {
      cmd = { "texlab" },
      settings = {
        texlab = {
          rootDirectory = nil,
          build = {
            executable = "latexmk",
            args = { "-shell-escape", "-pdf", "-interaction=nonstopmode", "-synctex=1", "%f" },
            onSave = true,
            forwardSearchAfter = true,
          },
          auxDirectory = ".",
          forwardSearch = {
            executable = "/Applications/Skim.app/Contents/SharedSupport/displayline", -- specific to Skim PDF viewer on macOS
            args = { "-g", "-b", "%l", "%p", "%f" },
          },
          chktex = {
            onOpenAndSave = true,
            onEdit = false,
          },
          diagnosticsDelay = 300,
          latexFormatter = "texlab", -- "texlab" is not implemented yet
          bibtexFormatter = "texlab",
          formatterLineLength = 0,
        },
      },
    })  
    Above, we are explicitly preventing texlab from using latexindent to format source. That is because we want to pass specific options to latexindent and that does not seem possible via the texlab configuration. We will configure and run latexindent ourselves below. Refer to the texlab documentation to set up a PDF viewer that supports forward and reverse sync. In the configuration above, I used Skim on macOS. Skim is probably the PDF pdf viewer on macOS, but a number of options exist on Linux and Windows.
  • Set up ltex:
    require("lvim.lsp.manager").setup("ltex", {
      settings = {
        ltex = {
          dictionary = {
            ["en-US"] = {
              -- add words to the dictionary here between double quotes, e.g., “convexity”
          }
        }
      }
    })
  • Configure latexindent:
    local null_ls = require("null-ls")                                                                                                                                                           
    null_ls.setup({
      sources = {
        null_ls.builtins.formatting.latexindent,
      },
    })
    null_ls.builtins.formatting.latexindent.with({
      debug = true,
      command = {
        "/opt/homebrew/bin/latexindent", -- specific to macOS; adjust for your system
      },
      args = {
        "-l", vim.fn.expand("%:p:h") .. "/localSettings.yaml", "-c", "/tmp", "-m", "-r",
      },
    })
  • Format using the shortcut Space-l-f, or enable formatting on save:
    lvim.format_on_save = {
      enabled = true,
      timeout = 1000,
    }

Where are the Diagnostics?

Save your LaTeX file to compile, open the PDF viewer and forward sync to the part that you were editing. Chktex and ltex diagnostics can be found by typing Space-l-d or Space-l-w. The TeXtidote diagnostics can be obtained by running the command :TeXtidoteToggle.