Friendship ended with LaTeX. Now Typst is my best friend.
Hello!
It has been a busy couple of works in the Functional Programming Strategies factory. The factory gnomes have been hard at work converting the book build system from a frankenmonster of Pandoc, custom plugins, and LaTeX, to Typst. The conversion has been an interesting process---more about the below---but first a few Scala themed announcements:
-
I'm speaking at Scala Days. The talk is about API design, which is a topic that may make up one of the final chapters in the book. They will be an early preview at the SLUG on 18th June.
-
Speaking of Scala Days, I have been given a discounted ticket (only 700CHF!) to give away. Drop me an email if you desire this ticket.
-
A group of us are heading to Scala Days by train. The route starts from London, changing at Paris and then Geneva. Consider joining if that's your jam! (I'll be there.)
So, let's talk about Typst. Last newsletter I had just finished the chapter on indexed types. I decided to spend some time on general cleanup: formatting, editing, and that kind of thing. The first task I decided to tackle was links---or the lack thereof---to items in the bibliography. And that's where it all fell apart.
When Dave cooked up the book build system back in the day, it was pretty darn good for its time. Writing text in Markdown is easy. Using Pandoc allowed us to target PDF, HTML, and ePub, and we could add custom elements via Pandoc plugins. But it is not without downsides. Building the PDF, for example, goes through mdoc, then Pandoc, and then LaTeX. When something goes wrong it could be anywhere in this process, but usually it's between Pandoc and LaTeX. Debugging this is a royal PITA. It requires generating the LaTeX, and then trying to figure out how to get Pandoc to output something different or changing the LaTeX template to work around Pandoc limitations.
This was the situation with the links to the bibliography. Pandoc has an option that supports this, but it didn't work. It wasn't clear to me from the LaTeX how I could fix this, so I tried updating Pandoc. This then required changes to the LaTeX template. At this point I noped out. I have never been a fan of TeX. It's definitely a remarkable achievement for its time, but programming a big pile of mutable state in a weird macro language is not my idea of fun. Typst, on the other hand, uses a straight forward language with innovations like lexical scoping. I noticed that Typst now supports HTML output, and Pandoc supports Typst. So I tried a quick conversion. It worked and I was sold. Goodbye LaTeX and hello Typst.
About 5 minutes after switching to Typst I realised I could, and should, get rid of Pandoc. Typst is as easy as Markdown to write. Why cobble something together out of fragile Pandoc plugins and random bits of Typst code, and continue to battle with the Pandoc / Typst interface, when I could just work with one system? While Pandoc can convert between many formats, including Typst, it only ever outputs a single file. So I couldn't use it to convert the Markdown to Typst because I definitely do not want to work on a single massive file. I thought Awk might be a good tool to do this conversion. I'm not very proficient with Awk, so I got some LLM assistance and created a simple prototype. Then I set about fixing the issues and quickly realized 1) Awk is kinda rubbish and 2) LLMs love to lie. What if I wrote the conversion in a language I know really well, Scala? Although Scala is not most people's first choice for simple scripting, the combination of the Scala CLI, Scala Toolkit, and regular expressions-as-patterns made this really easy. A few hours after initially looking into Typst I had a working build. Awesome!
My time since has been spent refining the Typst build. Most of that has gone into formatting. I've long wanted to tweak the book formatting (e.g. use a serif font for the body text) so this was a good opportunity to do some of that, as well as fix the layout of parts and chapters, add a copyright page, and do other "proper book" things. The build is now working, but I haven't released the Typst version yet as there are still a few things I need to manually convert to Typst (e.g tables). If you want to take a look, it's on the feature/typst
branch.
There are still lots of tasks to do, but the book is definitely feeling closer to done. It's quite exciting!
Until next time!
Regards,
Noel