Typstry.jl: The Julia to Typst Interface

Introduction

Hi everyone! I’m excited to announce Typstry.jl, which helps you create Typst formatted strings and render documents from Julia. If you are familiar with LaTeXStrings.jl and Latexify.jl, then Typstry.jl aims to provide similar features, and more.

What is Typst?

Typst is an open-source and relatively new typesetting system (written in Rust :rocket:), designed to improve upon the performance and usability of LaTeX. Documents can currently be compiled to PDF, PNG, and SVG, with plans to support HTML in the future. See also the Typst repository and documentation for examples and how to get started.

Features

Strings

Use the TypstString constructor to print Julia values to a Typst formatted string, with optional Julia settings and Typst parameters.

julia> TypstString("Hi everyone!")
typst"\"Hi everyone!\""

julia> TypstString(r"[a-z]", :mode => code)
typst"regex(\"[a-z]\")"

Write Typst directly using @typst_str with escaped control characters, such as the dollar signs used in Typst’s math mode. It supports formatted interpolation by calling TypstString.

julia> typst"$ 1 / x $"
typst"$ 1 / x $"

julia> typst"\(1 + 2im, :inline => true)"
typst"$1 + 2i$"

Instead of constructing a string, print in Typst format using show with the "text/typst" MIME type. Settings and parameters may be specified in an IOContext.

julia> show(IOContext(stdout, :mode => math), "text/typst", 1 // 2)
1 / 2

julia> show(stdout, "text/typst", [true 1; 1.0 [Any[true 1; 1.0 nothing]]])
$ mat(
    "true", 1;
    1.0, mat(
        "true", 1;
        1.0, ""
    )
) $

This show method provides a default IOContext for show_typst, which specifies the Typst formatting for each type.

julia> import Typstry: show_typst

julia> struct X end

julia> show_typst(io, ::X) = print(io, "everyone", io[:excited]::Bool ? "!" : ".");

julia> typst"Hi \(X(), :excited => true)"
typst"Hi everyone!"

Environments, such as this Pluto.jl notebook, can render TypstStrings.

Commands

Construct and run TypstCommands using the constructor or @typst_cmd.

julia> TypstCommand(["help"])
typst`help`

julia> typst`help compile`
typst`help compile`

Rendered documents can also use the JuliaMono typeface.

julia> addenv(typst`compile input.typ output.pdf`, "TYPST_FONT_PATHS" => julia_mono)
typst`compile input.typ output.pdf`

Conclusion

Please reach out with comments, questions, suggestions, issues, and contributions. I’m active in the Humans of Julia Discord server and the Julia Slack. Thank you for taking the time to check out Typstry.jl!

Contribute

More show_typst methods

A relatively small number of types are currently supported. Please file an issue or create a pull request for unsupported types in Base and the Standard Library. I am also happy to help with implementations for external packages and extensions.

IJulia.jl Notebook Support

Please help me resolve this problem to render TypstStrings in IJulia.jl notebooks!

92 Likes

Typstry.jl v0.2.0 is now released! See the news for a detailed list of updates.

Highlights

  • A tutorial illustrating the Julia to Typst Interface
  • A potential fix to render TypstStrings in IJulia.jl notebooks
  • Remove type piracy of show with the text/typst MIME type
    • Values may instead be wrapped in Typst
    • Formatting may be configured by implementing a custom context
  • Several new and improved show_typst methods
  • Easily render a Julia value to a Typst source file and compiled document
  • A few bug fixes
20 Likes

Typstry.jl v0.3.0 is now released! See the news for a detailed list of updates.

Highlights

  • Several new and improved show_typst methods
    • Implemented for types from Dates.jl, LaTeXStrings.jl, and Markdown.jl
  • A customizable preamble for render
  • Package interoperability
    • Use TypstStrings in Makie using MakieTeX.jl
    • Use Julia and TypstStrings in Typst using TypstJlyfish.jl
  • Better documentation and testing
  • Several bug fixes
    • Fixed escaped interpolation and corresponding pretty-printing

Fun Facts

  • show_typst has ~29 methods implementing the Julia to Typst interface for ~159 concrete types
  • Including Typstry.jl, there are now at least 5 registered Julia packages specifically for Typst and another 2 with Typst support
12 Likes

Typstry.jl v0.4.0 is now released! See the news for a detailed list of updates.

Highlights

  • Support for Typst v0.12
  • The ability to set_preamble
  • Automatically run, handle errors, and catch interrupts from the command-line interface using the typst function by @GunnarFarneback
  • A few bug fixes
15 Likes

Typstry.jl v0.5.0 is now released! This is a small update for Typst v0.13 and to Support read for TypstCommand #14.

12 Likes

Any example of how to use this package to make an actual document?

I believe it might have flown right over my head, but I was hoping I could use Typstry to make a document

Thanks!

2 Likes

Good question! This package uses the Typst command-line interface under the hood and provides a couple of wrappers over it. These are each documented here.

TypstCommand

This is used in a similar manner as constructing and running a Cmd, for example run(Cmd(["echo", "1"])).

julia> write("test_1.typ", "hi");

julia> run(TypstCommand(["compile", "test_1.typ"]));

@typst_cmd

This is the same as before, but is comparable to @cmd s with backticks.

julia> write("test_2.typ", "hey");

julia> run(typst`compile test_2.typ`);

typst

This function is similar to running commands, but with a few additional features. It’s intended to be used in the REPL.

  • Run the command immediately
  • By default, doesn’t throw a large error after an interrupt ctrl + c
  • By default, doesn’t throw a large error when the Typst command-line interface throws an error
  • Uses the JuliaMono typeface unless the TYPST_FONT_PATHS environment variable is set
julia> write("test_3.typ", "hello");

julia> typst("compile test_3.typ")

render

Instead of formatting a value to a TypstString, writing the string to a file, and then compiling that file, this function does all of that in a single step. It also automatically fits the page size, enables using the JuliaMono typeface, accepts a formatting context, and can ignore errors from Typst.

julia> render(typst"heyo"; output = "test_4.pdf");

After running this code, each of test_$n.pdf should be a document. Please let me know if this answers your question! :slight_smile:

3 Likes

Thanks for writing a package to integrate Typst better into Julia. I use both Julia and Typst a lot for my work. Basically, I have two Julia packages which do calculations and write Typst code. Then I compile it and have my reports.

Typst is amazingly designed and even at 0.13 it’s reliable for production.

3 Likes

You’re welcome, I’m happy to hear you’re having a good time with both languages <3

1 Like