How to automatically format acronyms according to The Economist’s style guide in Typst

Today I learnt …

Capital gains: following The Economist’s demanding standards using Typst’s state management, functions, and show rules

I love Typst. It’s a markup-based typesetting system that has the readability of Markdown with the power of LaTeX. I enjoy writing in it, and its PDF output is excellent. For my academic writing, Typst has replaced a homemade system of Markdown, Pandoc, and LaTeX that always felt clunky and brittle.

When I’m writing in Typst I try to follow The Economist’s style guide. It’s a well-regarded set of rules that can help produce readable prose in a formal style. One thing the style guide insists on, for example, is for acronyms to be typeset in small caps — so HTML becomes ʜᴛᴍʟ and NATO becomes ɴᴀᴛᴏ. This creates a more refined appearance on the page, preventing acronyms from appearing prominent in body text. However, there are two important exceptions: chemical formulae like H2O shouldn’t use small caps (probably because chemical notation is a precise symbolic language), and acronyms in headings should remain in full capitals (probably to maintain proper hierarchy, but also because it looks odd).

The challenge was implementing these rules in Typst without littering documents with #smallcaps function calls everywhere. Nobody wants to write #smallcaps[HTML] repeatedly throughout a document — it makes a document hard to write and its source hard to read.

The solution involves using Typst’s state management and show rules to create an automatic formatting system. A boolean state called acOK tracks whether acronyms should be converted to small caps. By default, it’s enabled, but it gets temporarily disabled within headings and can be manually disabled using a noac() wrapper function for chemical formulae.

The magic happens with a regex pattern that matches words made up of a capital letter followed by one or more capital letters and digits (\b[A-Z][A-Z\d]+\b). When this pattern is found in a document, Typst checks the current state and either applies small caps formatting or leaves the text unchanged.

#let acOK = state("acOK", true)
#let noac(content) = {
  acOK.update(false)
  content
  acOK.update(true)
}
#show heading: it => {
  acOK.update(false)
  it
  acOK.update(true)
}
#show regex("\b[A-Z][A-Z\d]+\b"): acronym => context [#if acOK.get() {smallcaps(acronym, all: true)} else {acronym}]

This combination of Typst features lets me write clean, readable documents while automatically applying The Economist’s style rules — I can focus on writing rather than formatting. Acronyms in body text are typeset in small caps, headings remain unaffected, and chemical formulae can be wrapped in noac() when needed. It’s a perfect example of the power of the Typst language: beautiful output from readable sources.

Example

Here’s a complete example document:

#set page(paper: "a6")

#let acOK = state("acOK", true)
#show " — ": [\u{202F}#sym.dash.em\u{202F}]
#let noac(content) = {
  acOK.update(false)
  content
  acOK.update(true)
}
#show heading: it => {
  acOK.update(false)
  it
  acOK.update(true)
}

#show regex("\b[A-Z\d]{2,}\b"): acronym => context [#if acOK.get() {smallcaps(acronym, all: true)} else {acronym}]

= What is HTML?

HTML is one of the three basic building block of all web pages,
the others being CSS and JavaScript.

On an analogue clock, the number 4 is often written as
#noac("IIII") rather than #noac("IV").

And its output:

Output of the Typst code