Strings

This reference documents formatting Julia values into Typst source text.

Typstry.TypstFunctions.TypstFunctionType
TypstFunction{C, P <: Tuple}

A wrapper representing a Typst function.

The default implementation formats the values in math mode, but show_typst may be implemented for custom types to format them in code mode too.

Fields

  • callable::C
  • parameters::P

Interface

  • repr(::MIME"text/typst", , ::TypstFunction)
  • show_typst(::IO, ::TypstContext, ::TypstFunction{String})
    • Format in call notation callable(parameters...).
  • show_typst(::IO, ::TypstContext, ::TypstFunction{Symbol})
    • If the predicates Base.isoperator(callable) and 0 < length(parameters) < 3 are satisfied, format in infix notation parameter_1 callable parameter_2. Otherwise, fallback to call notation callable(parameters...).
  • show_typst(::IO, ::TypstContext, ::TypstFunction{<:Union{DataType, Function}})
    • Fallback to show_typst(::IO, ::TypstContext, ::TypstFunction{Symbol})
  • show_typst(::IO, ::TypstContext, ::TypstFunction)
    • Fallback to show_typst(::IO, ::TypstContext, ::TypstFunction{TypstString})

See also TypstString.

source
Typstry.TypstStrings.TypstStringType
TypstString <: AbstractString
TypstString(::TypstContext, ::Any)
TypstString(::Any; context...)

A Typst formatted string.

The TypstContext is combined with additional context and passed to show_typst.

Interface

This type implements the String interface. However, the interface is undocumented, which may result in unexpected behavior.

  • IOBuffer(::TypstString)
  • codeunit(::TypstString, ::Integer)
  • codeunit(::TypstString)
  • isvalid(::TypstString, ::Integer)
  • iterate(::TypstString, ::Integer)
  • iterate(::TypstString)
  • ncodeunits(::TypstString)
  • pointer(::TypstString)
  • repr(::MIME"text/typst ::TypstString; context = nothing)
  • repr(::MIME, ::TypstString; context = nothing)
    • This method patches incorrect output from the assumption in repr that the parameter is already in the requested MIME type when the MIME type satisfies istextmime and the parameter is an AbstractString.
  • show_typst(::IO, ::TypstContext, ::TypstString)
  • show(::IO, ::MIME"text/typst", ::TypstString)
    • Accepts a IOContext(::IO, ::TypstContext).
  • show(::IO, ::Union{MIME"application/pdf", MIME"image/png", MIME"image/svg+xml"}, ::TypstString)
    • Accepts a IOContext(::IO, ::TypstContext).
    • Supports the julia_mono typeface.
    • The generated Typst source text contains the context's preamble and the formatted value.
  • show(::IO, ::TypstString)
    • Prints in @typst_str format if each character satisfies isprint. Otherwise, print in TypstString format.

Examples

julia> TypstString(1)
typst"$1$"

julia> TypstString(TypstContext(; mode = math), π)
typst"π"

julia> TypstString(1 + 2im; mode = math)
typst"(1 + 2i)"
source
Typstry.TypstTexts.TypstTextType
TypstText{T}
TypstText(::Any)

A wrapper whose show_typst method uses print on the wrapped value.

Interface

  • repr(::MIME"text/typst ::TypstText; context = nothing)
  • show_typst(::IO, ::TypstContext, ::TypstText)
  • show(::IO, ::MIME"text/typst", ::TypstText)
    • Accepts IOContext(::IO, :typst_context => ::TypstContext)
  • show(::IO, ::Union{MIME"application/pdf", MIME"image/png", MIME"image/svg+xml"}, ::TypstText)
    • Accepts IOContext(::IO, ::TypstContext)
    • Uses the preamble in context
    • Supports the julia_mono typeface

Examples

julia> tt = TypstText('a')
TypstText{Char}('a')

julia> show_typst(tt)
a
source
Typstry.Typsts.TypstType
Typst{T}
Typst(::T)

A wrapper used to pass values to show, whose show_typst method formats the wrapped value.

Interface

  • repr(::MIME"text/typst ::Typst; context = nothing)
  • show_typst(::IO, ::TypstContext, ::Typst)
  • show(::IO, ::MIME"text/typst", ::Typst)
    • Accepts IOContext(::IO, :typst_context => ::TypstContext)
  • show(::IO, ::Union{MIME"application/pdf", MIME"image/png", MIME"image/svg+xml"}, ::Typst)
    • Accepts IOContext(::IO, ::TypstContext)
    • Uses the preamble in context
    • Supports the julia_mono typeface

Examples

julia> t = Typst(1)
Typst{Int64}(1)

julia> show(stdout, "text/typst", t)
$1$
source
Typstry.TypstStrings.@typst_strMacro
@typst_str("")
typst""

Construct a TypstString.

Control characters are escaped, except double quotation marks and backslashes in the same manner as @raw_str. Values may be interpolated by calling the TypstString constructor, except using a backslash instead of the type name. Interpolation syntax may be escaped in the same manner as quotation marks.

Tip

Print directly to an IO using show_typst.

See also the performance tip to [Avoid string interpolation for I/O] (https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-string-interpolation-for-I/O).

Examples

julia> x = 1;

julia> typst"$ \(x; mode = math) / \(x + 1; mode = math) $"
typst"$ 1 / 2 $"

julia> typst"\(x//2)"
typst"$1 / 2$"

julia> typst"\(x // 2; mode = math)"
typst"(1 / 2)"

julia> typst"\\(x)"
typst"\\(x)"
source
Typstry.@typstMacro
(@typst value typst_context...)::TypstString

Transpile a subset of Julia to Typst.

The value is first processed, then passed to TypstString with the typst_context.

Examples

A symbol returns its assignment, if it exists, otherwise itself

julia> x = 1;

julia> @typst x + y
typst"$(1 + \"y\")$"

Other literal values, macro call expressions, and interpolation expressions are evaluated, returning the result

julia> @typst "hi"
typst"#\"hi\""

julia> @typst typst"hi"
typst"hi"

julia> @typst $(1 + 2)
typst"$3$"

All other expressions are processed recursively, returning an Expr

julia> @typst (1 + im ^ 2) / f(3)
typst"$((1 + (i ^ 2)) / (\"f\" (3)))$"
source
Typstry.Modes.ModeType
Mode

An Enumerated type used to specify that the current Typst syntactical context is code, markup, or math.

Examples

julia> Mode
Enum Mode:
code = 0
markup = 1
math = 2
source
Typstry.Modes.markupConstant
markup

A Typst syntactical Mode at the top-level of source text and enclosed within square brackets [].

julia> markup
markup::Mode = 1
source
Typstry.show_typstFunction
show_typst(::IO, ::TypstContext, ::Any)::Nothing
show_typst(::IO, ::Any; context...)::Nothing
show_typst(::TypstContext, ::Any)::Nothing
show_typst(::Any; context...)::Nothing

Print in Typst format with Julia settings and Typst parameters provided by the TypstContext.

Implement the three-parameter form of this function for a custom type to specify its Typst formatting. A setting is a value used in Julia, whose type varies across settings. A parameter is passed directly to a Typst function and must be a TypstString with the same name as in Typst, except that dashes are replaced with underscores. Some settings, such as block, correspond with a parameter but may also be used in Julia.

See also the Typst Formatting Examples.

Tip

Please create an issue or pull-request to implement new methods.

source