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 ), 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 TypstString
s.
Commands
Construct and run TypstCommand
s 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 TypstString
s in IJulia.jl notebooks!