Strings

This reference documents the Julia to Typst interface, a custom string corresponding to Typst source text, and its implementation of the String interface.

Typstry

Typstry.Strings.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.Strings.TypstStringType
TypstString <: AbstractString
TypstString(::Any; context...)

Format the value as a Typst formatted string.

Optional Julia settings and Typst parameters are passed to show(::IO, ::MIME"text/typst", ::Typst) in an IOContext. See also show_typst for a list of supported types.

Info

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

Examples

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

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

A wrapper whose show_typst method uses print.

Info

This may be used to insert control characters into a TypstString. Unescaped control characters in TypstStrings may break formatting in some environments, such as the REPL.

Examples

julia> TypstText(1)
TypstText{Int64}(1)

julia> TypstText("a")
TypstText{String}("a")
source
Typstry.Strings.@typst_strMacro
@typst_str("s")
typst"s"

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(::IO, ::MIME"text/typst", ::Typst).

See also the performance tip to 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.Strings.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.Strings.contextFunction
context(x)

Provide formatting data for show(::IO, ::MIME"text/typst", ::Typst).

Implement this function for a custom type to specify its custom settings and parameters. Passing a value wrapped in Typst will merge! its custom context with defaults, such that the defaults may be overwritten. To be compatible with merging contexts and constructing an IOContext, methods must return an AbstractDict{Symbol}.

SettingDefaultTypeDescription
:backticks3IntThe number of backticks to enclose raw text markup, which may be increased to disambiguiate nested raw text.
:blockfalseBoolWhen :mode => math, specifies whether the enclosing dollar signs are padded with a space to render the element inline or its own block.
:depth0IntThe current level of nesting within container types to specify the degree of indentation.
:modemarkupModeThe current Typst syntactical context where code follows the number sign, markup is at the top-level and enclosed in square brackets, and math is enclosed in dollar signs.
:parenthesizetrueBoolWhether to enclose some mathematical elements in parentheses to specify their operator precedence and avoid ambiguity.
:tab_size2IntThe number of spaces used by some elements with multi-line Typst formatting, which is repeated for each level of depth
source
Typstry.Strings.show_typstMethod
show_typst(io, x)

Print in Typst format with Julia settings and Typst parameters provided by an IOContext.

Implement 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. Settings each have a default value, whereas the default values of parameters are handled by Typst functions. Some settings, such as block, correspond with a parameter but may also be used in Julia.

For additional information on settings and parameters, see also context and the Typst Documentation, respectively.

Info

Some types, particularly containers, may call show(::IO, ::MIME"text/typst", ::Typst) to format a value, which may use additional settings and parameters.

Warning

This function's methods are incomplete. Please file an issue or create a pull-request for missing methods.

TypeSettingsParameters
AbstractArray:block, :depth, :mode, :tab_size:delim, :gap
AbstractChar
AbstractFloat:mode
AbstractMatrix:block, :depth, :mode, :tab_size:augment, :column_gap, :delim, :gap, :row_gap
AbstractString
Bool:mode
Complex{Bool}:block, :mode, :parenthesize
Complex:block, :mode, :parenthesize
Irrational:mode
Nothing:mode
OrdinalRange{<:Integer, <:Integer}:mode
Rational:block, :mode, :parenthesize
Regex:mode
Signed:mode
StepRangeLen{<:Integer, <:Integer, <:Integer}:mode
String:mode
Tuple:block, :depth, :mode, :tab_size:delim, :gap
Typst
TypstString
TypstText:mode
Unsigned:mode
VersionNumber:mode
Docs.HTML:block, :depth, :mode, :tab_size
Docs.Text:mode
Dates.Date:mode, :indent
Dates.DateTime:mode, :indent
Dates.Period:mode, :indent
Dates.Time:mode, :indent
source

Base

Base.IOBufferType
IOBuffer(::TypstString)

See also TypstString.

Examples

julia> IOBuffer(typst"a")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=1, maxsize=Inf, ptr=1, mark=-1)
source
Base.codeunitFunction
codeunit(::TypstString)
codeunit(::TypstString, ::Integer)

See also TypstString.

Examples

julia> codeunit(typst"a")
UInt8

julia> codeunit(typst"a", 1)
0x61
source
Base.iterateMethod
iterate(::TypstString, ::Integer)
iterate(::TypstString)

See also TypstString.

Examples

julia> iterate(typst"a")
('a', 2)

julia> iterate(typst"a", 1)
('a', 2)
source
Base.reprFunction
repr(::MIME, ::TypstString; kwargs...)

See also TypstString.

Info

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.

Examples

julia> repr("text/plain", typst"a")
"typst\"a\""

julia> repr("text/typst", typst"a")
typst"a"
source
Base.showMethod
show(::IO, ::MIME"text/typst", ::Union{Typst, TypstString, TypstText})

Print in Typst format.

This method provides formatting data to show_typst specified by a default and custom context.

See also Typst, TypstString, and TypstText.

Examples

julia> show(stdout, "text/typst", typst"a")
a

julia> show(stdout, "text/typst", Typst("a"))
"a"

julia> show(stdout, "text/typst", Typst(Text("a")))
#"a"
source