Typst

Typst is a modern typesetting system that aims to make editing documents really simple and fast. The language is markup based and aims at users in mathematics and the sciences. In many regards it is inspired by LaTeX but it also intentionally breaks with many old conventions. After hearing about it on Mastodon and from students I am finally trying it out.

LaTeX is based on the battle tested and famously bug free TeX system written by Donald Knuth. TeX is amazing. It is one of the oldest still active open source projects and has truly liberated publishing. Its maturity is clearly one of its strengths, but the age is showing. To name just one example, UTF-8 (conceived from 1989) was not even around when TeX was created (1978). Neither was PDF and the whole way a computer deals with fonts depends on the platform and has also changed several times in the decades. LaTeX does not natively support OpenType fonts and one needs to use re-implementations like XeTeX or LuaTeX. Writing in Chinese, Greek or Cyrillic can be painful.

On the other hand, LaTeX has an amazing community with thousands of packages that can do basically anything conceivable and more. The American Mathematical Society was an early adopter of TeX and has been developing tools for mathematical typesetting for decades. LaTeX is the lingua franca of typesetting and as a format expected by many publishers (although some do terrible things to the submissions…).

I have used LaTeX for decades. I use it to write my letters both professional and personal and of course papers, talk slides, DFG proposals, lecture notes etc. Every single of my submission to a journal or arXiv is a .tex-file.

So why even consider something new?

Well, the relationship of mathematicians with LaTeX can be love-hate. Just ask a random mathematician what \makeatletter does or how to fix this warning:

pdfTeX warning (ext4): destination with the same identifier 
(name{figure.1}) has been already used, duplicate ignored

And why is \small is not allowed in math mode? Most mathematicians just ignore obscure warnings and errors as long as the output is OK.

I can go on and on with odd quirks. Of course all of these have a solution and a good reason to be there. But by now the TeX system (which has good error messages!) is buried in layers and layers of macro sets from LaTeX and the hundreds of packages that one is using. Even our new LLM friends are pretty bad at fixing TeX-code or helping with the error messages. Probably because tex.stackexchange comprised most of the training data and just reading there has never been much help with specific problems. In the day to day life, TeX-problems are typically solved by identifying a mage of the old guard and getting some secret scroll whose content one puts into the own document without properly understanding why it magically fixes whatever is the problem at hand. What the tex.stackexchange did achieve is to make access to the mages easier.

When TeX was conceived, computers did not have much memory and hence it relies very heavily on intermediate files to store all sorts of information for the idiosyncratic multiple compiles runs, needed to get the list of sections for the table of contents (from an auxiliary file). This disk access slows everything down and finally it takes more than a minute to compile one of my lecture notes.

And finally, despite all of the love for (micro)typography going into LaTeX, the default ß looks really ugly!

Enter typst.

So typst tries to do a better job at the things above. Writing typst feels like writing markdown but with an internal programming language and more layout functionality. In particular, an empty file is a valid typst input file and so is a file that contains only text. It will render to a pdf with that text. No more \begin{document} because, hey, I created a .typ-file, it’s clear that I want to begin a document.

You can read all the ads and see code next to output on their github. The error messages are much more friendly and on the level that one knows from, say, python programming. Typst has named arguments to functions so that you can read the code and need not remember if the centering of the text or the color of the box comes first.

Typst is fast. The compile times are measured in μs. Long and complicated files can use ms. It is so fast, that you can play Tetris in typst. As you type your commands into the source code of a typst file, the game renders in the output.

Tetris implementation in typst

Of course typst does away with the intermediate files. So the tetris example is rendered in memory.

Typst is clearly not the first attempt to make typesetting easier, start from scratch, do something more modern etc. There is certain XKCD-927 vibe here, but for me personally it is the first attempt that is different and simplified enough to be worth a try.

First actual use

To test out typst I took a slide deck that was created using beamer and converted it to typst. My talk slides are usually pretty simple and consist of a couple of colorful boxes. Check out the before and after! Of course they look relatively similar, but that’s intentional. I only updated the colors a bit. Learning enough typst for this was a matter of very few hours. (If I keep editing this post any longer, it will be more time for the post than the slides.)

To tell just one story, in LaTeX I use the package tcolorbox. This is a very cool package. Its manual is a 551-page pdf file and here are some lengths to keep in mind when using it to construct your own boxes: Many lengths in tcolorbox

My TeX code for a green box is relatively simple. It was written 10 years ago and looks like this:

\newenvironment{gruenbox}[1]
{\begin{tcolorbox}[colback=ggruen!30,title=#1]}
{\end{tcolorbox}}

To make a box one then uses the command \begin{gruenbox}{title} and \end{gruenbox}. If you want no title you cannot use \begin{gruenbox}, but have to use \begin{gruenbox}{}. The title is an optional argument. Obviously I don’t have the smallest idea what goes on inside the tcolorbox package and would not be able to read the source code.

In typst I have recreated my boxes as functions that I can understand. Here is a basic box with two optional arguments for centering the entire box horizontally and for the background color of the box. The content of the box is left aligned inside the box. box is already a funtion in typst and it supports what I need from tcolorbox. So I set some basic named arguments of box and deal with alignment.

#let mybox(content, fill: none, _center: false) = {
  let box = box(
    radius: .3cm,
    inset: .7em,
    fill: fill,
    stroke: 1pt,
  )[#align(left)[#content]]
  if _center {
    align(center)[#box]
  } else {
    box
  }
}

A green box just further specifies this:

#let gruenbox(content, center: false) = mybox(
  content,
  fill: rgb("#c8fcbf"),
  _center: center
)

The full code for my first typst slides is here. I started with the university theme from touying and removed the header, footer and progress bar.

With a food preparation metaphor, what I’m doing here is throwing away my microwave-KitchenAid-fridge-machine with the 551 page manual that I used forever and start cooking on open fire again. Or maybe I just want to try something new? Maybe it’s just about self-efficacy or mid-life crisis? What I like most is that I can understand what is going on in this code. I hope that I can still understand it in 10 years, which is not the case for many LaTeX macros copied from the ancient scrolls.

Everything in typst from here on?

The ecosystem of typst looks very vibrant. For the most commonly requested features there are typst implementations. Some examples:

Will I switch to typst for everything? Certainly not yet. Many documents are written with co-authors (some of whom have not yet adapted to BibTeX … 😳), journals expect LaTeX submissions and so does arXiv. There is also no good typst to LaTeX compiler yet, although pandoc can do something in this direction.
In any case, my next lecture notes will be written in typst.