- 3 Minutes Wednesdays
- Posts
- Colored Text, Two Outputs, One Filter
Colored Text, Two Outputs, One Filter
Guten Tag! š
Many greetings from Munich, Germany. First off, a big thank you to everyone who has already grabbed a seat for the workshop ā¤ļø
But I know a lot of you are still deciding. And honestly, thatās fair. So far Iāve mostly talked about ideas: branding, dual outputs, the Lua filter pattern. Thatās all still pretty abstract.
So let me make it specific with something small. If youāve been paying attention to my previous mails then you know that the Quarto extension weāre building allows Quarto code like this:
The Fund continues to maintain a [low-risk strategy]{.text-primary} That {.text-primary} bit is a span class. On the HTML side, plain CSS just colors that text in the brandās primary color. Nothing too fancy there.
On the Typst PDF side, thereās no built-in concept of ācolor this span using my brand colorā. So a small Lua filter step
catches any span marked
.text-primaryor.text-muted,converts its contents to real Typst syntax, and
wraps it in a call to a small Typst function.
And itās this latter Typst function that knows how to apply the right color. Thatās how you get the output you want.
Thatās a small taste of the pattern I walked through a couple of emails ago. Donāt remember that? Well, let me remind you. The idea was a three step process:
A Lua filter finds the marked content
That content is modified as little as possible and then handed off to Typst
A Typst function does the actual styling
Letās go over the actual Lua code.
Target the right content
First, you specify the color classes and a filter for span elements.

Then, you will have to iterate over all your color classes that you specified. After all, you donāt know which one might be attached to your span element.

Inside that iteration, you can run a simply check on the classes of the element. Basically you ask āDid Quarto attach one of the color classes to the inline elementā?

Modify the content as little as possible
And once youāe established that youāre currently with one of the desired inline elements, you ideally use the content of the element with el.content.
But Pandoc does not like that, so you use a little helper function (here not shown) to wrap this into a format that Pandoc likes. Thatās a technical hiccup thatās best shown live and hard to describe in an email.
But once you have the content body, you pass that off to Typst by filling a Typst function called colored-text with arguments that your Lua filter extracted for you.

Pass that off to a Typst function
This Typst function is defined in your Typst template and then takes care of the actual rendering.

Itās a small example, but itās the same shape youāll see again and again once you know what to look for. Doesnāt matter if itās coloring text, wrapping a table, or laying out a header. The pattern is the same.
If thatās the kind of thing you want to learn about with the full BIRD Investment demo document, you can grab a seat here:
This 3-hour live workshop teaches you how to build branded, dual-output Quarto documents (HTML and PDF) using Typst, Lua filters, and a reusable Quarto extension.
And if this preview leaves you with questions, donāt hesitate to hit reply. Iād rather answer them now than have you sit on the fence.
Best,
Albert
Whenever youāre ready, there are three I can help you:
Automate Your Data Reports: This course helps data analysts eliminate manual copy-paste reporting by automating PDF reports end-to-end, saving hours every cycle and preventing costly mistakes. (Using the lovely Typst language š)
Generate Insights in Minutes, not Hours: This comprehensive course teaches you to handle data faster, smarter, and more efficiently.
Bespoke Data Science Solutions: Iāve helped clients build their own data science solutions. Whether building custom web apps, PDF reports, AI automations or teaching workshops, Iāve got you covered. You can reach out to me via this form (or simply hit reply to this email)

Reply