Efficient LaTeX Editing With Emacs

#Emacs #LaTeX

Table of Contents

The aim of this post is to show my customised LaTeX setup with Emacs and the packages I used to achieve it. The configurations I’ll be showing will be tailored to Doom Emacs, as that’s what I’m using. However, all of this is possible in vanilla Emacs. This guide assumes basic knowledge of both Emacs and LaTeX.

AUCTeX

AUCTeX can be enabled by enabling :lang latex in your doom block.

It has a lot of features, however, many of these features are replaced by other packages mentioned later in this guide. Here are some of the notable features that I use:

Live Preview

You can enable the live preview by typing C-c C-c and then selecting the View option. This will open the file in your preferred pdf-viewer and automatically update when you recompile your document (more on compiling later in the guide).

Since I prefer to stay inside emacs, I set my pdf viewer to pdf-tools by adding the following to my config.org:

(setq +latex-viewers '(pdf-tools))

Here’s what it looks like:

Pretty Symbols

This is a minor mode that displays text like \gamma with their unicode equivalents (γ) in math mode. The underlying text does not change, only the way they are displayed in the editor changes to make equations more readable. It is enabled by default in Doom Emacs, otherwise you can run M-x prettify-symbols-mode.

Here’s what it looks like:

Symbols

You’ll notice that the superscripts and subscripts are also displayed as such in the editor, this is also done by AUCTeX. AUCTeX can also automatically insert braces when you type ^ or _, this is done by setting the TeX-electric-sub-and-superscript variable, it is set to t by default in Doom Emacs.

Text Folding

You can remove some of the visual noise in a LaTeX buffer by enabling TeX-fold-mode. This is also enabled by default in Doom Emacs.

This is easier to show than to explain:

CDLaTeX

CDLaTeX is a minor mode supporting fast insertion of environment templates and math stuff in LaTeX.1

You can enable this by adding the +cdlatex flag to the latex section of your doom block, like:

(latex +cdlatex)

CDLaTeX also has a ton of features, but here are the ones I use the most:

Abbreviations

Abbreviation expansion is triggered with TAB, for example:

Use M-x cdlatex-command-help to see a full list of abbreviations.

Mathematical Symbols

Backquote followed by any character inserts a LaTeX math macro into the buffer. If necessary, a pair of \( is inserted to switch to math mode. For example, typing `a inserts \(\alpha\).

You can wait a second after pressing ` and a list of all the available macros will pop up. The macros can also be customised with the cdlatex-math-symbol-alist variable.

Accents and Fonts

Putting accents on mathematical characters and/or changing the font of a character uses key combinations with the quote character ' as a prefix. The accent or font change is applied to the character or LaTeX macro before point.

LatexMk

Latexmk automates the process of compiling a LaTeX document. Essentially, it is like a specialized relative of the general make utility, but one which determines dependencies automatically and has some other very useful features.2

You can enable it by adding the +latexmk flag to latex in your doom block. So it should look something like this if you’re using cdlatex as well:

(latex +cdlatex +latexmk)

When you build your LaTeX project for the first time using LatexMk, press C-c C-c and select the LatexMk option. After that, you can build it with C-c C-a.

LSP

LSP is a communication protocol used between an integrated development environment (IDE) or code editor and a language server that provides language-specific analysis, autocompletion, diagnostics, and other language-related features for programming.

You can enable lsp-mode for Doom Emacs by enabling the :tools lsp module in your doom block.

You can view a list of useful lsp-mode features here.

To enable lsp-mode for LaTeX, add the +lsp flag to latex in your doom block. Your doom block will look like this by now:

(latex +cdlatex +latexmk +lsp)

Additionally, you’ll also need a language server for LaTeX installed. The two most popular ones are TexLab and Digestif. The choice between the two ultimately comes down to personal preference, and you’ll have to look at their features and decide for yourself which one you prefer. I prefer TexLab, but it you want to use Digestif instead, you can add the following line to your config.el:

(setq lsp-tex-server 'digestif)

Xenops

Xenops is a LaTeX editing environment for mathematical documents in Emacs.

You can install it by adding (package! xenops) to your packages.el. To enable it in a LaTeX buffer, run M-x xenops-mode. If you would like to enable it automatically in LaTeX buffers, add the following line to your config.el:

(add-hook LaTeX-mode-hook #'xenops-mode)

Here are some the notable features of Xenops:

Render Maths, Tables and TikZ Diagrams

Xenops replaces the preview-latex functionality in AUCTeX, which does not seem to support rendering images as SVGs.

Paste Images from the Clipboard

Simply run M-x xenops-image-handle-paste to paste an image from the clipboard into your document.

YASnippet

YASnippet is a template system for Emacs. It allows you to type an abbreviation and automatically expand it into function templates when pressing TAB.

You can install YASnippet by enabling :editor snippets in your doom block.

Some useful snippets can be found here.

Note that CDLaTeX also has snippet functionaly for LaTeX. If the same snippet is defined in both CDLaTeX and YASnippet, you’ll probably want to use the CDLaTeX snippet, so add the following line to your config.el:

(map! :map cdlatex-mode-map
      :i "TAB" #'cdlatex-tab)

GNU Calc

Calc is a calculator that’s part of Emacs. It can do symbolic differentiation, linear algebra, taylor series expansions, among many other things.

You can start calc in embedded mode by running M-x calc-embedded and perform operations on LaTeX equations. Here’s an example of me finding the taylor series expansion of ex to 6 terms:

evil-tex

evil-tex provides a number of useful features for evil users. This should also be installed by default in Doom Emacs.

Here’s an example of me using it:

Ackowledgement

Thanks to these articles for inspiring this post: