Efficient LaTeX Editing With Emacs
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 can be enabled by enabling
:lang latex in your
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:
You can enable the live preview by typing C-c C-c and then selecting the
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
(setq +latex-viewers '(pdf-tools))
Here’s what it looks like:
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
Here’s what it looks like:
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
_, this is done by setting the
TeX-electric-sub-and-superscript variable, it is set to
t by default in Doom Emacs.
You can remove some of the visual noise in a LaTeX buffer by enabling
This is also enabled by default in Doom Emacs.
This is easier to show than to explain:
You can enable this by adding the
+cdlatex flag to the latex section of your
doom block, like:
CDLaTeX also has a ton of features, but here are the ones I use the most:
Abbreviation expansion is triggered with TAB, for example:
M-x cdlatex-command-help to see a full list of abbreviations.
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
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
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 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
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
After that, you can build it with
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
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 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
(setq lsp-tex-server 'digestif)
Xenops is a LaTeX editing environment for mathematical documents in Emacs.
You can install it by adding
(package! xenops) to your
To enable it in a
LaTeX buffer, run
If you would like to enable it automatically in
LaTeX buffers, add the following line to your
(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
M-x xenops-image-handle-paste to paste an image from the clipboard into your document.
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
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
(map! :map cdlatex-mode-map :i "TAB" #'cdlatex-tab)
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:
Here’s an example of me using it:
Thanks to these articles for inspiring this post: