Typst, the programming language for documents

Overview

Languages like TeX can be used to create documents with elaborate formatting, like scientific papers or anything with math formulae. Typst aims to be as powerful, but by way of a modern programming style, opening the door for programmatic ways to create richly designed books, manuals, documentation, or even web pages.

Register Now

Transcript

There is a programming language for just about every use case. Most of us know about Python, Rust, C, or Java. Those are general purpose languages. But then we get into more specific use cases like SQL for databases, or R or matlab for mathematics.

There's also a couple of programming languages for making documents, not just writing documentation like Pandoc, but for typesetting and laying out entire publications like books or scientific papers. And for decades now, the big name in this space was Tex or its descendant latex.

But now there is a new language for creating these kinds of things, one that's been inspired by Tex, but also by more modern languages, in particular fast markup languages like Markdown.

The language that I'm thinking of today is called Typst, and while it's still in a relatively early state, it's a powerful and complete enough project that you can accomplish a lot with it. Right now it's written in Rust, so it runs fast and it's memory efficient.

And there are extensions available for Visual Studio code that let you work with it and preview your work in real time. But for the sake of this demo, I'm going to use Typist's live web interface so you can try it out yourself as well.

Let's go take a look at how this works. Typst programs are just text files. If you type most normal text, the resulting output is formatted about the way you would expect.

It also uses a markdown like syntax to express things like emphasis or boldface, and you separate paragraphs with a double line break.

For more complex things, Typst uses the hash symbol to indicate a variable reference or a function in your Typst code. For instance, if I want to place a footnote in the document, I would hash footnote and then type the text for the footnote in square brackets.

If you want to type square brackets and hashes themselves in your text, you can just escape them with slashes, as I've done here with this other footnote. For mathematical formulas, Typst sets them apart from the main text with dollar signs.

Again, those can be escaped with a slash, and the way you type equations is pretty similar to how you would express them in a programming language. One little caveat though. If you already have experience with you won't be able to use tex expressions for equations.

Typst has its own equation mini language that is not like tex, but it shouldn't be difficult to switch over, and some third parties do offer packages for automatically converting Tex to Typst. Incidentally, the live preview that you're seeing here.

This synchronizes scrolling in both directions, both in the web version of this and in the Visual Studio code version. So if I click on something in the preview, I will jump to that part in my Typst program and vice versa.

Again, I want to emphasize that Typst is a programming language. The document is itself a program, and one common example of how this works is that all the properties of the document, its layout and its fonts and so on can be declared programmatically.

If I want to modify how paragraphs work, I can use a set command to do that.

Here I set that paragraphs will have a first line indent of 2M's and use justification. Note that the first graph here doesn't have an indent, because the default setting in Typst is to not indent the first paragraph of a section, for instance, after a page break.

But we can alter that if we want, and the same goes for how we set page size and the base text appearance, and pretty much every other variable you can think of. You can also dynamically transform text or styling by way of what are called show rules.

An easy example of this if I say show and then supply a regular expression, this will then take any match in my text for the regex and return something in its place.

So here I'm adding emphasis to each occurrence of the word math, whether it's init caps or not.

And these kinds of rules can be applied within a function or just the body of the text as a whole. Now, because this is a program again, I'm not limited to what directives I have in the document itself.

I can load code from other modules, I can use a whole curated directory of external third party packages, and so on. I can also create functions and write conditional logic for them in the usual if then else style.

And I can also alias existing names, as I've done here with Footnote for the sake of my own convenience. Now, Typst is optimized for documents where the positions of elements on the page are fixed and known in advance.

So when I want to write the document out to a static file, the default option is going to be PDF, which preserves formatting.

Or we can format out to an image file.

Now it is possible to currently export to HTML, but right now HTML export options are limited and in some features will simply not render in HTML mode because there's no way to know where they will render in an HTML document.

Now there are ways to work around that, and one of them is with conditional export rules, we can use the target function to determine what to write out depending on what export target has been selected.

So you'll notice that when I switch to HTML preview mode, this expression here, which is only evaluated when we're exporting to PDF, it vanishes when we use HTML.

So this gives us a way to generate HTML a little more flexibly than the current export options allow by default, and also opens the doors for other kinds of flexibility that will be available regardless of how they make HTML export in the future.

I'm expecting a lot more functionality to roll into Typist's base product over time, but there is already, as I mentioned, a sizable culture of third party packages available through Typist's own package manager, and these let you do things like wrap text around shapes or make drop caps, or do other things that are not readily available.

Some of what Typists can do right now is limited by how strict it is with its behaviors. For instance, you cannot import packages or even read files from outside the root directory of the document, and that's a safety measure.

I am really curious to see where Typst goes because it could provide a high level way to write elaborate and good looking documents that can be used across whole varieties of media, online or in ebooks, in print, and so on.

And all of this comes by the way of a programming language. Not a word processor, not a graphic editor. That's what makes it so interesting to me. And that is it for this episode. If you liked it, leave a comment below.

And don't forget to follow Dev Serdar and InfoWorld on Facebook, YouTube and InfoWorld.com. Sam.