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!

76 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
15 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
7 Likes