Data retrieval functions
Functions used to retrieve data from custom elements, custom types, and their instances.
Main functions
e.data
This is the main function used to retrieve data from custom elements and custom types and their instances.
The other functions listed under "Helper functions" are convenient wrappers over this function to reduce typing.
It receives any input and returns a dictionary with one of the following values for the data-kind
key:
-
"element"
: dictionary with an element's relevant parameters and data generated it was declared. This is the data kind returned bye.data(constructor)
. Importantly, it contains information such aseid
for the element's unique ID (combining itsprefix
andname
, used to distinguish it from elements with the same name),sel
for the element's selector,outer-sel
for the outer selector (used exclusively for show-set rules),counter
for the element's counter,func
for the element's constructor,fields
for a parsed list of element fields, and more. -
"custom-type-data"
: conceptually similar to"element"
but for custom types, including their data at declaration time. This is returned bye.data(custom-type-constructor)
. Containstid
for the type's unique ID (combiningprefix
andname
), as well astypeinfo
for the type's typeinfo,fields
with field information andfunc
for the constructor. -
"element-instance"
: returned frome.data(it)
in a show rule on custom elements, or when usinge.data(some-element(...))
. Relevant keys here includeeid
for the element ID (prefix + name),func
for the element's constructor, as well asfields
, dictionary containing the values specified for each field for this instance.It also contains
body
, which, in show rules, contains the value returned by the element'sdisplay
function (the element's effective body), but note that thebody
shouldn't be placed directly; you should returnit
from the show rule to preserve the element. You usually will never need to usebody
. In addition, outside of show rules, it is rather meaningless and only contains the element itself.Note that, in show rules, the returned data will have
fields-known: true
which indicates that the final values of all fields, after synthesizing and set rules are applied, are known and stored infields
. Outside of show rules, however (e.g. on recently-constructed elements), the dictionary will havefields-known: false
indicating that the dictionary offields
is incomplete and only contains the arguments initially passed to the constructor, as set rules and default values for missing fields haven't been applied yet.Note also that this data kind is also returned when applying to elements matched in a show rule on filtered selectors returned by
e.select
. -
"type-instance"
: similar to"element-instance"
, exceptfields
are always known and complete since there are no show or set rules for custom types, soe.data(my-type(...))
will always have a complete set of fields, as well astid
for the type's ID andfunc
for its constructor. -
"incomplete-element-instance"
: this is only obtained when trying toe.data(it)
on a show rule on an element's outer selector (obtained frome.selector(elem, outer: true)
ore.data(elem).outer-sel
). The only relevant information it contains is theeid
of the matched element. The rest is all unknown. -
"content"
: returned when some arbitrarycontent
with native elements is received. In this case,eid
will benone
, butfunc
will be set toit.func()
,fields
will be set toit.fields()
andbody
will be set toit
(the given parameter) itself. -
"unknown"
: something that wasn't content, an element, a custom type, or their instances was given. For example, an integer (no data to extract).
Signature:
#e.data(
any
) -> dictionary
Example:
// Element
// (Equivalent to e.data(elem).sel)
#let sel = e.selector(elem)
// Instance
#show sel: it => {
// (Equivalent to e.data(it).fields)
let fields = e.fields(it)
[Given color: #fields.color]
[Counter value is #e.counter(elem).display("1.")]
// (Equivalent to e.data(it).eid, ...)
assert.eq(e.eid(it), e.eid(elem))
// (Equivalent to e.data(it).func)
assert.eq(e.func(it), elem)
it
}
// Custom type data
#let name = e.data(my-type).name
#let typeinfo = e.data(my-type).typeinfo
// Custom type instance data
#assert.eq(e.func(my-type(...)), my-type)
// Equivalent to e.data(my-type(a: 5, b: 6)).fields
#assert.eq(e.fields(my-type(a: 5, b: 6)), (a: 5, b: 6))
e.repr
This is used to obtain a debug representation of custom types.
In the future, this will support elements as well.
Also supports native types (just calls repr()
for them).
Signature:
#e.repr(
any
) -> str
Example:
// -> "person(name: \"John\", age: 40)"
#e.repr(person(name: "John", age: 40))
Helper functions
Functions which simply wrap and return some specific data
.
e.counter
Helper function to obtain an element's associated counter.
This is a wrapper over e.data(arg).counter
.
Signature:
#e.counter(
element
) -> counter
Example:
#e.counter(elem).step()
#context e.counter(elem).display("1.")
e.ctx
Helper function to obtain context from element instances within show rules and functions used by e.element.declare
.
This is only available if the element was declared with contextual: true
, as it is otherwise expensive to store. When available, it is a dictionary containing (get: function)
, where get(elem)
returns the current default values of fields for elem
considering set rules.
This is a wrapper over e.data(arg).ctx
.
Signature:
#e.ctx(
any
) -> dictionary | none
Example:
// For references to work
#show: e.prepare()
#let elem = e.element.declare(
"elem",
prefix: "@preview/my-package,v1",
contextual: true,
reference: (
custom: it => [#(e.ctx(it).get)(other-elem).field]
)
)
#elem(label: <a>)
// Will display the value of the other element's field
@a
e.eid
Helper function to obtain an element's unique ID (modified combination of prefix + name).
Can also be used on element instances.
This is a wrapper over e.data(arg).eid
.
Signature:
#e.eid(
any
) -> str | none
Example:
e.fields
Helper function to obtain an element or custom type instance's fields as a dictionary.
For elements, this will be incomplete outside of show rules. For custom types, it is always complete.
This is a wrapper over e.data(arg).fields
.
Signature:
#e.fields(
any
) -> dictionary | none
Example:
#assert.eq(e.fields(my-elem(a: 5)), (a: 5))
e.func
Helper function to obtain the constructor used to create an element or custom type instance.
Can also be used on elements or custom types themselves.
This is a wrapper over e.data(arg).func
.
Signature:
#e.func(
any
) -> function | none
e.func-name
Get the name of a content's constructor function as a string.
Returns none
on invalid input.
Signature:
#e.func-name(
content | custom element function
) -> function | none
Example:
assert.eq(func-name(my-elem()), "my-elem")
assert.eq(func-name([= abc]), "heading")
e.scope
Helper function to obtain an element or custom type's associated scope.
This is a wrapper over e.data(arg).scope
.
Signature:
#e.scope(
any
) -> module | dictionary | none
Example:
#import e.scope(elem): sub-elem
#sub-elem(...)
e.selector
Returns one of the element's selectors:
- By default, returns its main selector, used for show rules and querying. This is equivalent to
e.data(arg).sel
. - With
outer: true
, returns a selector that can be used for show-set rules. This is equivalent toe.data(arg).outer-sel
. - With
outline: true
, returns a selector that can be used inoutline(target: /* here */)
for outlinable elements. This is equivalent toe.data(arg).outline-sel
.
Signature:
#e.selector(
element,
outer: bool = false,
outline: bool = false,
) -> label | selector | none
Example:
#show e.selector(elem): strong
#show e.selector(elem, outer: true): set par(justify: false)
#outline(target: e.selector(elem, outline: true))
e.tid
Helper function to obtain the type ID of a custom type, or of an instance's custom type.
This is a wrapper over e.data(arg).tid
.
Signature:
#e.tid(
any
) -> str | none