Contexts

This reference documents how to specify formatting data.

Typstry.ContextErrors.ContextErrorType
ContextError <: Exception
ContextError(::Type, ::Type, ::Symbol)

An Exception indicating that a context key returned a value of an incorrect type.

Interface

Implements the Exception interface.

  • showerror(::IO, ::ContextError)
  • show(::IO, ::MIME"text/plain", ::ContextError)

Examples

julia> ContextError(Mode, String, :mode)
ContextError(Mode, String, :mode)
source
Typstry.DefaultIOs.DefaultIOType
DefaultIO
DefaultIO()

A type used to initialize the default io in context.

Info

This is a workaround for stdout being invalid when assigned to a global variable.

Examples

julia> io = context[:io]::DefaultIO
(() -> IOContext(stdout, :compact => true))::DefaultIO

julia> io == DefaultIO()() == IOContext(stdout, :compact => true)
true
source
Typstry.TypstContextType
TypstContext <: AbstractDict{Symbol, Any}
TypstContext(::Any)
TypstContext(; kwargs...)

Provide formatting data for show_typst.

Implement a method of this constructor for a custom type to specify its custom settings and parameters.

Calls to show_typst from the following methods:

  • TypstString
  • render
  • show_typst(::IO, ::TypstContext, x)
  • show with the application/pdf, image/png, image/svg+xml, and text/typst MIME types and a TypstString, TypstText, and Typst value

specify the TypstContext by combining the following contexts:

  1. The context given by a TypstContext or keyword parameters, which are mutually exclusive
  2. The context given by IOContext(::IO, ::TypstContext)
  3. The context implicitly set when calling show_typst within show_typst
  4. The type's default context, specified by implementing the TypstContext constructor
  5. The global default context

Duplicate keys are handled such that each context is prioritized in order as listed. In other words, keyword parameters and TypstContext parameters have the highest priority while the default context has the lowest priority.

Interfaces

This type implements the dictionary and iteration interfaces. However, it does not support removing mappings except through reset_context.

  • IOContext(::IO, ::TypstContext)
    • Equivalent to IOContext(::IO, :typst_context => ::TypstContext)
  • copy(::TypstContext)
  • eltype(::TypstContext)
  • getkey(::TypstContext, ::Any, ::Any)
  • get(::TypstContext, ::Any, ::Any)
  • get(::Union{Function, Type}, ::TypstContext, ::Any)
  • iterate(::TypstContext, state)
  • iterate(::TypstContext)
  • length(::TypstContext)
  • mergewith(::Any, ::TypstContext, ::AbstractDict...)
  • merge!(::TypstContext, ::AbstractDict...)
  • merge(::TypstContext, ::AbstractDict...)
  • setindex!(::TypstContext, ::Any, ::Any)
  • show(::IO, ::TypstContext)
  • sizehint!(::TypstContext, ::Any; kwargs...)
source
Typstry.contextConstant
context

A constant TypstContext used to provide default formatting data when calling show_typst.

See also reset_context.

Tip

Set mappings in this dictionary to customize the default formatting in other packages and environments.

Warning

This should only be mutated by users. Mutating this in a package may result in conflicts.

SettingTypeDescription
backticksIntThe number of backticks to enclose raw text markup, which may be increased to disambiguiate nested raw text.
blockBoolWhen mode = math, specifies whether the enclosing dollar signs are padded with a space to render the element inline or its own block.
contextTypstContextThis is set implicitly by combining the context given by a TypstContext or keyword parameters with that given by an IO. It is used when calling show_typst from within show_typst. For example, formatting values in containers or changing the type of the input. This is necessary to construct a new combined context with the nested type-level default values, rather than the root type-level default values.
depthIntThe current level of nesting within container types to specify the degree of indentation by repeating the tab_size.
ioDefaultIOA function with the signature io()::IO, which is used by show_typst when an IO is not otherwise specified. This is implemented as a function to avoid initialization errors.
modeModeThe 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.
parenthesizeBoolWhether to enclose some mathematical elements in parentheses to specify their operator precedence and avoid ambiguity.
preambleTypstStringUsed at the beginning of Typst source files generated by render and some show methods.
tab_sizeIntThe number of spaces used by some elements with multi-line Typst formatting, which is repeated for each level of depth

Examples

julia> context
TypstContext with 8 entries:
  :mode         => markup
  :parenthesize => true
  :block        => false
  :preamble     => TypstString(TypstText("#set page(margin: 1em, height: auto, …
  :io           => (() -> IOContext(stdout, :compact => true))::DefaultIO
  :tab_size     => 2
  :backticks    => 3
  :depth        => 0
source
Typstry.reset_contextFunction
reset_context()

Remove any custom mappings from the context such that it is returned to its default state.

Examples

julia> reset_context()
TypstContext with 8 entries:
  :mode         => markup
  :parenthesize => true
  :block        => false
  :preamble     => TypstString(TypstText("#set page(margin: 1em, height: auto, …
  :io           => (() -> IOContext(stdout, :compact => true))::DefaultIO
  :tab_size     => 2
  :backticks    => 3
  :depth        => 0
source