Understand and write LaTeX with Nvim or Zed

Pen & paper is my go-to means of working through and expressing these concepts as I learn them, but sometimes I want to print mathematical formula. I spent a couple of days exploring LaTeX. As it turns out I don’t need an entire LaTeX writing environment just to write a couple formulas in LaTeX isn’t necessary, but it’s still good to understand the heritage and context and another tool under the belt.

This guide aims to distill my last few days of research into LaTeX & finally offer recommendations into setting up a fairly minimal LaTeX writing environment.


This is a full LaTeX document that should give you a broad idea of how it works:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\title{\LaTeX\ intro}
\author{Your name here}
\maketitle
% Inline algorithm complexity (note single $)
The algorithm runs in $O(n \log n)$ time and here is the Gauss Sum:
% Gauss Sum (Note double $$ for placing on online)
$$\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$$
\begin{center} And here is \end{center}
% Logic notation (note the \implies macro needed the amsmath package above)
$$\forall x \in S : P(x) \implies Q(x)$$
\end{document}

You use macros starting with \ and group arguments with {}. You can start inline mathematical formulae with $ and put them on their own line with $$. You can define your own macros (not shown here). You can bring in an enourmous amount of packages with \usepackage{*}. You can have use different kinds of templates with \documentclass{}. You can create environments (blocks) with \begin and \end. And a lot more I haven’t discovered yet. The ecosystem is enourmous. It is a very proficient and sane typesetter out of the box.

Here is what the output looks like for the above snippet:

Zed with LaTeX on Hyprland
LaTeX output from above source

I’m not going to cover much more about how to write LaTeX because there are much better resources. Instead I’ll cover history & context - there’s unreasonable amounts of legacy and info to wade through to find respectable tooling. If you do want a good introductory resource to the markup and macros, I have found “The Not So Short Introduction to LATEX” a great resource.

History

TeX is a typesetting system by Donald Knuth, author of The Art of Computer Programming - I haven’t read it. He saw the advent of digital publishing but thought that it came with a deterioration in print quality. His goal was to reverse that. The first version was released in 1982. Back then, you wrote TeX markup and its binaries would spit out a file intended directly for your printer (with .dvi files). Today, same thing but straight to your screen, usually via a PDF.

LaTeX started as a bunch of personal macros on top of TeX made by Leslie Lamport (I know him as the Lamport Timestamp guy) in the 80s. He formalised and publicly released it in 1985. It became popular and in 1994 LaTeX2e was released; the latest stable version that is widely used today.

Alternatives to LaTeX

LaTeX is a system designed to help you use proven, sound patterns in publishing your technical book, article (or even presentation). You can leverage macros & even write your own macros to keep consistent patterns throughout your book or article. It is flexible but can take effort to wrangle compared to a WYSIWYG tool like Adobe InDesign or Microsoft Publisher.

Typst is probably the most well-known alternative, a newer, Apache-licensed typesetting system & language. Some of it looks pretty similar. People seem to like it going by Reddit & HN comments.

Some markdown editors have built-in support for these renderers, like Typora for example, but I found the snippets rendered unreasonably slowly in that software. Obsidian is better, from what I have heard. There are also lots of pure online editors like UpMath that is also pretty good and fast.

Writing LaTeX-like expressions in the browser

If you just want to publish some basic expressions on the web, look at MathJax or the allegedly faster KaTeX. Both can output expressions directly to the DOM. There is also MathML Core - an XML based mathematics markup language which is supported by major browsers. You can convert the much easier to write LaTeX expressions to MathML Core.

x = - b ± b 2 - 4 a c 2 a
A MathML Core snippet directly in the browser

LaTeX components

Here is a basic overview of components to keep you on track. Firstly it is worth mentioning CTAN - a website that tracks the entire TeX packages ecosystem.

  • Packages: a bundle of code related to LaTeX.
  • Package managers: e.g. tlmgr (TeXLive Manager), a binary tool to install LaTeX packages
  • Engines: e.g. pdftex, LuaTeX, XeTeX - these are binaries that read .tex files and packages and produce output. pdftex will get you going, but you might want to move onto the more full-featured LuaTeX or XeTeX. I will continue the guide using LuaTeX.
  • latexmk: I give this a special mention because you’ll likely see it referred to. Think of it like Make for TeX documents. It’s a perl script that’s going to take your .tex doc & drive the engines to compile intermediate outputs and eventually a PDF.
  • Distributions: e.g. Texlive, Mitex. Entire prebuilt systems comprising all of the above so you can get started. Usually broken up into a core collection + collections of extensions.
  • Document/PDF Viewers: e.g. zathura, skim, even MacOS Preview
  • .tex files: Files containing the content you’ll be working on
  • Auxiliary files: e.g. .log, .aux, .fls files. Intermediate files that are created by the engines (often via latexmk) or package specific binaries etc.

So in a nutshell, you download a distribution (TexLive is the defacto principle distro). If you download a full distribution you will get a LOT of stuff with it. So start with a basic distribution, then use the distribution’s package manager (or your OS’s package manager) to get what you might need. You may move from pdftex engine to LuaTeX or XeTeX if you find pdftex is limiting. For example, as mentioned below, the unicode math package doesn’t work with pdftex. I did find pdftex faster to render out of the box (eyeballing it).

To edit with live preview, you can use VSCode, Zed, VimTex or one of many other Tex Editors.

In the next sections I will recommend straightforward editing environments for both MacOS & Arch Linux. Personally, I use Zed. While it doesn’t have many LaTeX-specific features currently, it is good enough.

It is worth noting here that Overleaf seems to be the gold standard for online collaborative tex editing, if you want to take the easy way out. The local setup is not that hard however.

Arch Linux setup

This will install TexLive (defacto standard LaTeX distribution), Zathura PDF viewer and some extras. The extras cover latexmk as well as the luatex engine. The luatex engine is required for some basic maths packages, and the texlive-fontsrecommended includes fonts that the LuaTex engine uses by default.

Terminal window
# Install Zathura (pdf viewer)
sudo pacman -Syu zathura zathura-pdf-mupdf
# Install minimal set of recommended libraries for working with Math functions
sudo pacman -Syu texlive-basic texlive-binextra texlive-latexrecommended texlive-fontsrecommended texlive-luatex
Zed with LaTeX on Hyprland
Zed with LaTeX extension on Hyprland & Zathura running side by side

MacOS setup

This will install a stripped down version of MacTex, the Mac version of TexLive. I do not recommend the full mactex install unless you have a lot of disk space. I found (including the install files that didn’t delete) that it took up around 15GB.

Terminal window
brew install basictex # Minimalist TeXLive distribution
brew install --cask skim # PDF Viewer
# The next steps use the TexLive package manager to install math + extra fonts (n.b. not exact equivalent of arch packages)
sudo tlmgr install latexmk collection-fontsrecommended lualatex-math
Zed with LaTeX on MacOS
Zed with LaTeX extension & Skim on MacOS running side by side

latexmk setup

As mentioned, latexmk is like the Make for LaTeX. It’s what VimTeX & Zed use under the hood to drive the LaTeX engines. You will likely want it to drive LuaTex as opposed to the default pdftex, so that among other things, you can use unicode symbols in maths with the unicode-math package. This config is good enough to start with VimTex & Zed, but you may want a full config file when developing your own build steps later.

Terminal window
# Set latexmkrc to use LuaLaTeX engine
echo "\$pdf_mode = 4;" > ~/.latexmkrc

Zed setup

The Zed LaTeX extension works out of the box, including live preview with both those setups above. This has been my preference. You can configcaption it manually. You can view extension output in the Language Servers section to troubleshoot.

VimTeX & VSCode

If you’re not a fan of Zed, VimTeX and NeoVim are a pretty good fit. Save yourself a headache and use the lazy plugin manager with a single file config setup to get started. VSCode has more mature LaTeX support. This is beyond the scope of this article as I am a Zed patrician.

.gitignore

Personally I’m in the business of adding files to .gitignore as they’re needed, but if you want a cover all, here is Github’s standard .gitignore file for TeX docs. So far I am using the following:

.gitignore
*.pdf
*.aux
*.fdb_latexmk
*.fls
*.log
*.synctex.gz

Footnotes

  • LaTeX is pronounced more like a hard ch. See explanation at tex.stackexchange.
  • If you make changes to the LaTeX environment e.g. adding or removing packages, you may have to delete the intermediate files. VimTeX optionally does this automatically, and also has keyboard shortcuts to do it.