feat: initial commit
This commit is contained in:
commit
b802ff49ab
454 files changed
+52429
No files matched your search
@@ -0,0 +1,92 @@
|
||||
# Arguments
|
||||
|
||||
Captured arguments to a function.
|
||||
|
||||
## Argument Sinks
|
||||
|
||||
Like built-in functions, custom functions can also take a variable number of arguments. You can specify an _argument sink_ which collects all excess arguments as `..sink`. The resulting `sink` value is of the `arguments` type. It exposes methods to access the positional and named arguments.
|
||||
|
||||
```typst
|
||||
#let format(title, ..authors) = {
|
||||
let by = authors
|
||||
.pos()
|
||||
.join(", ", last: " and ")
|
||||
|
||||
[*#title* \ _Written by #by;_]
|
||||
}
|
||||
|
||||
#format("ArtosFlow", "Jane", "Joe")
|
||||
```
|
||||
|
||||
## Spreading
|
||||
|
||||
Inversely to an argument sink, you can _spread_ arguments, arrays and dictionaries into a function call with the `..spread` operator:
|
||||
|
||||
```typst
|
||||
#let array = (2, 3, 5)
|
||||
#calc.min(..array)
|
||||
#let dict = (fill: blue)
|
||||
#text(..dict)[Hello]
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## arguments
|
||||
|
||||
Construct spreadable arguments in place.
|
||||
|
||||
This function behaves like `let args(..sink) = sink`.
|
||||
|
||||
```typst
|
||||
#let args = arguments(stroke: red, inset: 1em, [Body])
|
||||
#box(..args)
|
||||
```
|
||||
|
||||
```typst
|
||||
#arguments(
|
||||
arguments
|
||||
) -> arguments
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- arguments:
|
||||
- description: The arguments to construct.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## arguments.at
|
||||
|
||||
Returns the positional argument at the specified index, or the named argument with the specified name.
|
||||
|
||||
If the key is an [integer](/docs/reference/foundations/int/), this is equivalent to first calling [`pos`](/docs/reference/foundations/arguments/#definitions-pos) and then [`array.at`](/docs/reference/foundations/array/#definitions-at). If it is a [string](/docs/reference/foundations/str/), this is equivalent to first calling [`named`](/docs/reference/foundations/arguments/#definitions-named) and then [`dictionary.at`](/docs/reference/foundations/dictionary/#definitions-at).
|
||||
|
||||
```typst
|
||||
#arguments.at(
|
||||
key,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- key:
|
||||
- description: The index or name of the argument to get.
|
||||
- type: int | str
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the key is invalid.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## arguments.pos
|
||||
|
||||
Returns the captured positional arguments as an array.
|
||||
|
||||
## arguments.named
|
||||
|
||||
Returns the captured named arguments as a dictionary.
|
||||
|
||||
|
||||
@@ -0,0 +1,717 @@
|
||||
# Array
|
||||
|
||||
A sequence of values.
|
||||
|
||||
You can construct an array by enclosing a comma-separated sequence of values in parentheses. The values do not have to be of the same type.
|
||||
|
||||
You can access and update array items with the `.at()` method. Indices are zero-based and negative indices wrap around to the end of the array. You can iterate over an array using a [for loop](/docs/reference/scripting/#loops). Arrays can be added together with the `+` operator, [joined together](/docs/reference/scripting/#blocks) and multiplied with integers.
|
||||
|
||||
**Note:** An array of length one needs a trailing comma, as in `(1,)`. This is to disambiguate from a simple parenthesized expressions like `(1 + 2) * 3`. An empty array is written as `()`.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let values = (1, 7, 4, -3, 2)
|
||||
|
||||
#values.at(0) \
|
||||
#(values.at(0) = 3)
|
||||
#values.at(-1) \
|
||||
#values.find(calc.even) \
|
||||
#values.filter(calc.odd) \
|
||||
#values.map(calc.abs) \
|
||||
#values.rev() \
|
||||
#(1, (2, 3)).flatten() \
|
||||
#(("A", "B", "C")
|
||||
.join(", ", last: " and "))
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## array
|
||||
|
||||
Converts a value to an array.
|
||||
|
||||
Note that this function is only intended for conversion of a collection-like value to an array, not for creation of an array from individual items. Use the array syntax `(1, 2, 3)` (or `(1,)` for a single-element array) instead.
|
||||
|
||||
```typst
|
||||
#let hi = "Hello 😃"
|
||||
#array(bytes(hi))
|
||||
```
|
||||
|
||||
```typst
|
||||
#array(
|
||||
value
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to an array.
|
||||
- type: bytes | array | version
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## array.len
|
||||
|
||||
The number of values in the array.
|
||||
|
||||
## array.first
|
||||
|
||||
Returns the first item in the array. May be used on the left-hand side an assignment. Returns the default value if the array is empty or fails with an error is no default value was specified.
|
||||
|
||||
```typst
|
||||
#array.first(
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- default:
|
||||
- description: A default value to return if the array is empty.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.last
|
||||
|
||||
Returns the last item in the array. May be used on the left-hand side of an assignment. Returns the default value if the array is empty or fails with an error is no default value was specified.
|
||||
|
||||
```typst
|
||||
#array.last(
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- default:
|
||||
- description: A default value to return if the array is empty.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.at
|
||||
|
||||
Returns the item at the specified index in the array. May be used on the left-hand side of an assignment. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#array.at(
|
||||
index,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- index:
|
||||
- description: The index at which to retrieve the item. If negative, indexes from the back.
|
||||
- type: int
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the index is out of bounds.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.push
|
||||
|
||||
Adds a value to the end of the array.
|
||||
|
||||
```typst
|
||||
#array.push(
|
||||
value
|
||||
) ->
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value to insert at the end of the array.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.pop
|
||||
|
||||
Removes the last item from the array and returns it. Fails with an error if the array is empty.
|
||||
|
||||
## array.insert
|
||||
|
||||
Inserts a value into the array at the specified index, shifting all subsequent elements to the right. Fails with an error if the index is out of bounds.
|
||||
|
||||
To replace an element of an array, use [`at`](/docs/reference/foundations/array/#definitions-at).
|
||||
|
||||
```typst
|
||||
#array.insert(
|
||||
index,
|
||||
value
|
||||
) ->
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- index:
|
||||
- description: The index at which to insert the item. If negative, indexes from the back.
|
||||
- type: int
|
||||
- default: None
|
||||
- value:
|
||||
- description: The value to insert into the array.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.remove
|
||||
|
||||
Removes the value at the specified index from the array and return it.
|
||||
|
||||
```typst
|
||||
#array.remove(
|
||||
index,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- index:
|
||||
- description: The index at which to remove the item. If negative, indexes from the back.
|
||||
- type: int
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the index is out of bounds.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.slice
|
||||
|
||||
Extracts a subslice of the array. Fails with an error if the start or end index is out of bounds.
|
||||
|
||||
```typst
|
||||
#array.slice(
|
||||
start,
|
||||
end,
|
||||
count: int
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- start:
|
||||
- description: The start index (inclusive). If negative, indexes from the back.
|
||||
- type: int
|
||||
- default: None
|
||||
- end:
|
||||
- description: The end index (exclusive). If omitted, the whole slice until the end of the array is extracted. If negative, indexes from the back.
|
||||
- type: none | int
|
||||
- default: none
|
||||
- count:
|
||||
- description: The number of items to extract. This is equivalent to passing `start + count` as the `end` position. Mutually exclusive with `end`.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## array.contains
|
||||
|
||||
Whether the array contains the specified value.
|
||||
|
||||
This method also has dedicated syntax: You can write `2 in (1, 2, 3)` instead of `(1, 2, 3).contains(2)`.
|
||||
|
||||
```typst
|
||||
#array.contains(
|
||||
value
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value to search for.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.find
|
||||
|
||||
Searches for an item for which the given function returns `true` and returns the first match or `none` if there is no match.
|
||||
|
||||
```typst
|
||||
#array.find(
|
||||
searcher
|
||||
) -> any none
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- searcher:
|
||||
- description: The function to apply to each item. Must return a boolean.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.position
|
||||
|
||||
Searches for an item for which the given function returns `true` and returns the index of the first match or `none` if there is no match.
|
||||
|
||||
```typst
|
||||
#array.position(
|
||||
searcher
|
||||
) -> none int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- searcher:
|
||||
- description: The function to apply to each item. Must return a boolean.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.range
|
||||
|
||||
Create an array consisting of a sequence of numbers.
|
||||
|
||||
If you pass just one positional parameter, it is interpreted as the `end` of the range. If you pass two, they describe the `start` and `end` of the range.
|
||||
|
||||
This function is available both in the array function's scope and globally.
|
||||
|
||||
```typst
|
||||
#range(5) \
|
||||
#range(2, 5) \
|
||||
#range(20, step: 4) \
|
||||
#range(21, step: 4) \
|
||||
#range(5, 2, step: -1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.range(
|
||||
start,
|
||||
end,
|
||||
step: int
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- start:
|
||||
- description: The start of the range (inclusive).
|
||||
- type: int
|
||||
- default: 0
|
||||
- end:
|
||||
- description: The end of the range (exclusive).
|
||||
- type: int
|
||||
- default: None
|
||||
- step:
|
||||
- description: The distance between the generated numbers.
|
||||
- type: int
|
||||
- default: 1
|
||||
|
||||
## array.filter
|
||||
|
||||
Produces a new array with only the items from the original one for which the given function returns true.
|
||||
|
||||
```typst
|
||||
#array.filter(
|
||||
test
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- test:
|
||||
- description: The function to apply to each item. Must return a boolean.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.map
|
||||
|
||||
Produces a new array in which all items from the original one were transformed with the given function.
|
||||
|
||||
```typst
|
||||
#array.map(
|
||||
mapper
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- mapper:
|
||||
- description: The function to apply to each item.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.enumerate
|
||||
|
||||
Returns a new array with the values alongside their indices.
|
||||
|
||||
The returned array consists of `(index, value)` pairs in the form of length-2 arrays. These can be [destructured](/docs/reference/scripting/#bindings) with a let binding or for loop.
|
||||
|
||||
```typst
|
||||
#for (i, value) in ("A", "B", "C").enumerate() {
|
||||
[#i: #value \ ]
|
||||
}
|
||||
|
||||
#("A", "B", "C").enumerate(start: 1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.enumerate(
|
||||
start: int
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- start:
|
||||
- description: The index returned for the first pair of the returned list.
|
||||
- type: int
|
||||
- default: 0
|
||||
|
||||
## array.zip
|
||||
|
||||
Zips the array with other arrays.
|
||||
|
||||
Returns an array of arrays, where the `i`th inner array contains all the `i`th elements from each original array.
|
||||
|
||||
If the arrays to be zipped have different lengths, they are zipped up to the last element of the shortest array and all remaining elements are ignored.
|
||||
|
||||
This function is variadic, meaning that you can zip multiple arrays together at once: `(1, 2).zip(("A", "B"), (10, 20))` yields `((1, "A", 10), (2, "B", 20))`.
|
||||
|
||||
```typst
|
||||
#array.zip(
|
||||
exact: bool,
|
||||
others
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- exact:
|
||||
- description: Whether all arrays have to have the same length. For example, `(1, 2).zip((1, 2, 3), exact: true)` produces an error.
|
||||
- type: bool
|
||||
- default: false
|
||||
- others:
|
||||
- description: The arrays to zip with.
|
||||
- type: array
|
||||
- default: None
|
||||
|
||||
## array.fold
|
||||
|
||||
Folds all items into a single value using an accumulator function.
|
||||
|
||||
```typst
|
||||
#let array = (1, 2, 3, 4)
|
||||
#array.fold(0, (acc, x) => acc + x)
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.fold(
|
||||
init,
|
||||
folder
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- init:
|
||||
- description: The initial value to start with.
|
||||
- type: any
|
||||
- default: None
|
||||
- folder:
|
||||
- description: The folding function. Must have two parameters: One for the accumulated value and one for an item.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.sum
|
||||
|
||||
Sums all items (works for all types that can be added).
|
||||
|
||||
```typst
|
||||
#array.sum(
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- default:
|
||||
- description: What to return if the array is empty. Must be set if the array can be empty.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.product
|
||||
|
||||
Calculates the product of all items (works for all types that can be multiplied).
|
||||
|
||||
```typst
|
||||
#array.product(
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- default:
|
||||
- description: What to return if the array is empty. Must be set if the array can be empty.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.any
|
||||
|
||||
Whether the given function returns `true` for any item in the array.
|
||||
|
||||
```typst
|
||||
#array.any(
|
||||
test
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- test:
|
||||
- description: The function to apply to each item. Must return a boolean.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.all
|
||||
|
||||
Whether the given function returns `true` for all items in the array.
|
||||
|
||||
```typst
|
||||
#array.all(
|
||||
test
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- test:
|
||||
- description: The function to apply to each item. Must return a boolean.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.flatten
|
||||
|
||||
Combine all nested arrays into a single flat one.
|
||||
|
||||
## array.rev
|
||||
|
||||
Return a new array with the same items, but in reverse order.
|
||||
|
||||
## array.split
|
||||
|
||||
Split the array at occurrences of the specified value.
|
||||
|
||||
```typst
|
||||
#(1, 1, 2, 3, 2, 4, 5).split(2)
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.split(
|
||||
at
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- at:
|
||||
- description: The value to split at.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.join
|
||||
|
||||
Combine all items in the array into one.
|
||||
|
||||
```typst
|
||||
#array.join(
|
||||
separator,
|
||||
last: any,
|
||||
default: any | none
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- separator:
|
||||
- description: A value to insert between each item of the array.
|
||||
- type: any | none
|
||||
- default: none
|
||||
- last:
|
||||
- description: An alternative separator between the last two items.
|
||||
- type: any
|
||||
- default: None
|
||||
- default:
|
||||
- description: What to return if the array is empty.
|
||||
- type: any | none
|
||||
- default: none
|
||||
|
||||
## array.intersperse
|
||||
|
||||
Returns an array with a copy of the separator value placed between adjacent elements.
|
||||
|
||||
```typst
|
||||
#("A", "B", "C").intersperse("-")
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.intersperse(
|
||||
separator
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- separator:
|
||||
- description: The value that will be placed between each adjacent element.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## array.chunks
|
||||
|
||||
Splits an array into non-overlapping chunks, starting at the beginning, ending with a single remainder chunk.
|
||||
|
||||
All chunks but the last have `chunk-size` elements. If `exact` is set to `true`, the remainder is dropped if it contains less than `chunk-size` elements.
|
||||
|
||||
```typst
|
||||
#let array = (1, 2, 3, 4, 5, 6, 7, 8)
|
||||
#array.chunks(3) \
|
||||
#array.chunks(3, exact: true)
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.chunks(
|
||||
chunk-size,
|
||||
exact: bool
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- chunk-size:
|
||||
- description: How many elements each chunk may at most contain.
|
||||
- type: int
|
||||
- default: None
|
||||
- exact:
|
||||
- description: Whether to keep the remainder if its size is less than `chunk-size`.
|
||||
- type: bool
|
||||
- default: false
|
||||
|
||||
## array.windows
|
||||
|
||||
Returns sliding windows of `window-size` elements over an array.
|
||||
|
||||
If the array length is less than `window-size`, this will return an empty array.
|
||||
|
||||
```typst
|
||||
#let array = (1, 2, 3, 4, 5, 6, 7, 8)
|
||||
#array.windows(5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.windows(
|
||||
window-size
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- window-size:
|
||||
- description: How many elements each window will contain.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## array.sorted
|
||||
|
||||
Return a sorted version of this array, optionally by a given key function. The sorting algorithm used is stable.
|
||||
|
||||
Returns an error if a pair of values selected for comparison could not be compared, or if the key or comparison function (if given) yield an error.
|
||||
|
||||
To sort according to multiple criteria at once, e.g. in case of equality between some criteria, the key function can return an array. The results are in lexicographic order.
|
||||
|
||||
```typst
|
||||
#let array = (
|
||||
(a: 2, b: 4),
|
||||
(a: 1, b: 5),
|
||||
(a: 2, b: 3),
|
||||
)
|
||||
#array.sorted(key: it => (it.a, it.b))
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.sorted(
|
||||
key: function,
|
||||
by: function
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- key:
|
||||
- description: If given, applies this function to each element in the array to determine the keys to sort by.
|
||||
- type: function
|
||||
- default: None
|
||||
- by:
|
||||
- description: If given, uses this function to compare every two elements in the array. The function will receive two elements in the array for comparison, and should return a boolean indicating their order: `true` indicates that the elements are in order, while `false` indicates that they should be swapped. To keep the sort stable, if the two elements are equal, the function should return `true`. If this function does not order the elements properly (e.g., by returning `false` for both `(x, y)` and `(y, x)`, or for `(x, x)`), the resulting array will be in unspecified order. When used together with `key`, `by` will be passed the keys instead of the elements. ```typst #( "sorted", "by", "decreasing", "length", ).sorted( key: s => s.len(), by: (l, r) => l >= r, ) ```
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.dedup
|
||||
|
||||
Deduplicates all items in the array.
|
||||
|
||||
Returns a new array with all duplicate items removed. Only the first element of each duplicate is kept.
|
||||
|
||||
```typst
|
||||
#(3, 3, 1, 2, 3).dedup()
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.dedup(
|
||||
key: function
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- key:
|
||||
- description: If given, applies this function to each element in the array to determine the keys to deduplicate by. ```typst #("apple", "banana", " apple ").dedup(key: s => s.trim()) ```
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
## array.to-dict
|
||||
|
||||
Converts an array of pairs into a dictionary. The first value of each pair is the key, the second the value.
|
||||
|
||||
If the same key occurs multiple times, the last value is selected.
|
||||
|
||||
```typst
|
||||
#(
|
||||
("apples", 2),
|
||||
("peaches", 3),
|
||||
("apples", 5),
|
||||
).to-dict()
|
||||
```
|
||||
|
||||
## array.reduce
|
||||
|
||||
Reduces the elements to a single one, by repeatedly applying a reducing operation.
|
||||
|
||||
If the array is empty, returns `none`, otherwise, returns the result of the reduction.
|
||||
|
||||
The reducing function is a closure with two arguments: an "accumulator", and an element.
|
||||
|
||||
For arrays with at least one element, this is the same as [`array.fold`](/docs/reference/foundations/array/#definitions-fold) with the first element of the array as the initial accumulator value, folding every subsequent element into it.
|
||||
|
||||
```typst
|
||||
#let array = (2, 1, 4, 3)
|
||||
#array.reduce((acc, x) => calc.max(acc, x))
|
||||
```
|
||||
|
||||
```typst
|
||||
#array.reduce(
|
||||
reducer
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- reducer:
|
||||
- description: The reducing function. Must have two parameters: One for the accumulated value and one for an item.
|
||||
- type: function
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# Assert
|
||||
|
||||
# assert
|
||||
|
||||
Ensures that a condition is fulfilled.
|
||||
|
||||
Fails with an error if the condition is not fulfilled. Does not produce any output in the document.
|
||||
|
||||
If you wish to test equality between two values, see [`assert.eq`](/docs/reference/foundations/assert/#definitions-eq) and [`assert.ne`](/docs/reference/foundations/assert/#definitions-ne).
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#assert(1 < 2, message: "math broke")
|
||||
```
|
||||
|
||||
```typst
|
||||
#assert(
|
||||
condition,
|
||||
message: str
|
||||
) ->
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- condition:
|
||||
- description: The condition that must be true for the assertion to pass.
|
||||
- type: bool
|
||||
- default: None
|
||||
- message:
|
||||
- description: The error message when the assertion fails.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### assert.eq
|
||||
|
||||
Ensures that two values are equal.
|
||||
|
||||
Fails with an error if the first value is not equal to the second. Does not produce any output in the document.
|
||||
|
||||
```typst
|
||||
#assert.eq(10, 10)
|
||||
```
|
||||
|
||||
```typst
|
||||
#assert.eq(
|
||||
left,
|
||||
right,
|
||||
message: str
|
||||
) ->
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- left:
|
||||
- description: The first value to compare.
|
||||
- type: any
|
||||
- default: None
|
||||
- right:
|
||||
- description: The second value to compare.
|
||||
- type: any
|
||||
- default: None
|
||||
- message:
|
||||
- description: An optional message to display on error instead of the representations of the compared values.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
### assert.ne
|
||||
|
||||
Ensures that two values are not equal.
|
||||
|
||||
Fails with an error if the first value is equal to the second. Does not produce any output in the document.
|
||||
|
||||
```typst
|
||||
#assert.ne(3, 4)
|
||||
```
|
||||
|
||||
```typst
|
||||
#assert.ne(
|
||||
left,
|
||||
right,
|
||||
message: str
|
||||
) ->
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- left:
|
||||
- description: The first value to compare.
|
||||
- type: any
|
||||
- default: None
|
||||
- right:
|
||||
- description: The second value to compare.
|
||||
- type: any
|
||||
- default: None
|
||||
- message:
|
||||
- description: An optional message to display on error instead of the representations of the compared values.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Auto
|
||||
|
||||
A value that indicates a smart default.
|
||||
|
||||
The auto type has exactly one value: `auto`.
|
||||
|
||||
Parameters that support the `auto` value have some smart default or contextual behaviour. A good example is the [text direction](/docs/reference/text/text/#parameters-dir) parameter. Setting it to `auto` lets Typst automatically determine the direction from the [text language](/docs/reference/text/text/#parameters-lang).
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# Boolean
|
||||
|
||||
A type with two states.
|
||||
|
||||
The boolean type has two values: `true` and `false`. It denotes whether something is active or enabled.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#false \
|
||||
#true \
|
||||
#(1 < 2)
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
# Bytes
|
||||
|
||||
A sequence of bytes.
|
||||
|
||||
This is conceptually similar to an array of [integers](/docs/reference/foundations/int/) between `0` and `255`, but represented much more efficiently. You can iterate over it using a [for loop](/docs/reference/scripting/#loops).
|
||||
|
||||
You can convert
|
||||
|
||||
- a [string](/docs/reference/foundations/str/) or an [array](/docs/reference/foundations/array/) of integers to bytes with the [`bytes`](/docs/reference/foundations/bytes/) constructor
|
||||
- bytes to a string with the [`str`](/docs/reference/foundations/str/) constructor, with UTF-8 encoding
|
||||
- bytes to an array of integers with the [`array`](/docs/reference/foundations/array/) constructor
|
||||
|
||||
When [reading](/docs/reference/data-loading/read/) data from a file, you can decide whether to load it as a string or as raw bytes.
|
||||
|
||||
```typst
|
||||
#bytes((123, 160, 22, 0)) \
|
||||
#bytes("Hello 😃")
|
||||
|
||||
#let data = read(
|
||||
"rhino.png",
|
||||
encoding: none,
|
||||
)
|
||||
|
||||
// Magic bytes.
|
||||
#array(data.slice(0, 4)) \
|
||||
#str(data.slice(1, 4))
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## bytes
|
||||
|
||||
Converts a value to bytes.
|
||||
|
||||
- Strings are encoded in UTF-8.
|
||||
- Arrays of integers between `0` and `255` are converted directly. The dedicated byte representation is much more efficient than the array representation and thus typically used for large byte buffers (e.g. image data).
|
||||
|
||||
```typst
|
||||
#bytes("Hello 😃") \
|
||||
#bytes((123, 160, 22, 0))
|
||||
```
|
||||
|
||||
```typst
|
||||
#bytes(
|
||||
value
|
||||
) -> bytes
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to bytes.
|
||||
- type: str | bytes | array
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## bytes.len
|
||||
|
||||
The length in bytes.
|
||||
|
||||
## bytes.at
|
||||
|
||||
Returns the byte at the specified index. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#bytes.at(
|
||||
index,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- index:
|
||||
- description: The index at which to retrieve the byte.
|
||||
- type: int
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the index is out of bounds.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## bytes.slice
|
||||
|
||||
Extracts a subslice of the bytes. Fails with an error if the start or end index is out of bounds.
|
||||
|
||||
```typst
|
||||
#bytes.slice(
|
||||
start,
|
||||
end,
|
||||
count: int
|
||||
) -> bytes
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- start:
|
||||
- description: The start index (inclusive).
|
||||
- type: int
|
||||
- default: None
|
||||
- end:
|
||||
- description: The end index (exclusive). If omitted, the whole slice until the end is extracted.
|
||||
- type: none | int
|
||||
- default: none
|
||||
- count:
|
||||
- description: The number of items to extract. This is equivalent to passing `start + count` as the `end` position. Mutually exclusive with `end`.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,984 @@
|
||||
# Calculation
|
||||
|
||||
Module for calculations and processing of numeric values.
|
||||
|
||||
These definitions are part of the `calc` module and not imported by default. In addition to the functions listed below, the `calc` module also defines the constants `pi`, `tau`, `e`, and `inf`.
|
||||
|
||||
# calc.abs
|
||||
|
||||
Calculates the absolute value of a numeric value.
|
||||
|
||||
```typst
|
||||
#calc.abs(-5) \
|
||||
#calc.abs(5pt - 2cm) \
|
||||
#calc.abs(2fr) \
|
||||
#calc.abs(decimal("-342.440"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.abs(
|
||||
value
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The value whose absolute value to calculate.
|
||||
- type: int | float | length | angle | ratio | fraction | decimal
|
||||
- default: None
|
||||
|
||||
# calc.pow
|
||||
|
||||
Raises a value to some exponent.
|
||||
|
||||
```typst
|
||||
#calc.pow(2, 3) \
|
||||
#calc.pow(decimal("2.5"), 2)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.pow(
|
||||
base,
|
||||
exponent
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- base:
|
||||
- description: The base of the power. If this is a [`decimal`](/docs/reference/foundations/decimal/), the exponent can only be an [integer](/docs/reference/foundations/int/).
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- exponent:
|
||||
- description: The exponent of the power.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.exp
|
||||
|
||||
Raises a value to some exponent of e.
|
||||
|
||||
```typst
|
||||
#calc.exp(1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.exp(
|
||||
exponent
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- exponent:
|
||||
- description: The exponent of the power.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.sqrt
|
||||
|
||||
Calculates the square root of a number.
|
||||
|
||||
```typst
|
||||
#calc.sqrt(16) \
|
||||
#calc.sqrt(2.5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.sqrt(
|
||||
value
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number whose square root to calculate. Must be non-negative.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.root
|
||||
|
||||
Calculates the real nth root of a number.
|
||||
|
||||
If the number is negative, then n must be odd.
|
||||
|
||||
```typst
|
||||
#calc.root(16.0, 4) \
|
||||
#calc.root(27.0, 3)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.root(
|
||||
radicand,
|
||||
index
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- radicand:
|
||||
- description: The expression to take the root of.
|
||||
- type: float
|
||||
- default: None
|
||||
- index:
|
||||
- description: Which root of the radicand to take.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.sin
|
||||
|
||||
Calculates the sine of an angle.
|
||||
|
||||
When called with an integer or a float, they will be interpreted as radians.
|
||||
|
||||
```typst
|
||||
#calc.sin(1.5) \
|
||||
#calc.sin(90deg)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.sin(
|
||||
angle
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- angle:
|
||||
- description: The angle whose sine to calculate.
|
||||
- type: int | float | angle
|
||||
- default: None
|
||||
|
||||
# calc.cos
|
||||
|
||||
Calculates the cosine of an angle.
|
||||
|
||||
When called with an integer or a float, they will be interpreted as radians.
|
||||
|
||||
```typst
|
||||
#calc.cos(1.5) \
|
||||
#calc.cos(90deg)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.cos(
|
||||
angle
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- angle:
|
||||
- description: The angle whose cosine to calculate.
|
||||
- type: int | float | angle
|
||||
- default: None
|
||||
|
||||
# calc.tan
|
||||
|
||||
Calculates the tangent of an angle.
|
||||
|
||||
When called with an integer or a float, they will be interpreted as radians.
|
||||
|
||||
```typst
|
||||
#calc.tan(1.5) \
|
||||
#calc.tan(90deg)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.tan(
|
||||
angle
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- angle:
|
||||
- description: The angle whose tangent to calculate.
|
||||
- type: int | float | angle
|
||||
- default: None
|
||||
|
||||
# calc.asin
|
||||
|
||||
Calculates the arcsine of a number.
|
||||
|
||||
```typst
|
||||
#calc.asin(0) \
|
||||
#calc.asin(1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.asin(
|
||||
value
|
||||
) -> angle
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number whose arcsine to calculate. Must be between -1 and 1.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.acos
|
||||
|
||||
Calculates the arccosine of a number.
|
||||
|
||||
```typst
|
||||
#calc.acos(0) \
|
||||
#calc.acos(1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.acos(
|
||||
value
|
||||
) -> angle
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number whose arccosine to calculate. Must be between -1 and 1.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.atan
|
||||
|
||||
Calculates the arctangent of a number.
|
||||
|
||||
```typst
|
||||
#calc.atan(0) \
|
||||
#calc.atan(1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.atan(
|
||||
value
|
||||
) -> angle
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number whose arctangent to calculate.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.atan2
|
||||
|
||||
Calculates the four-quadrant arctangent of a coordinate.
|
||||
|
||||
The arguments are `(x, y)`, not `(y, x)`.
|
||||
|
||||
```typst
|
||||
#calc.atan2(1, 1) \
|
||||
#calc.atan2(-2, -3)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.atan2(
|
||||
x,
|
||||
y
|
||||
) -> angle
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- x:
|
||||
- description: The X coordinate.
|
||||
- type: int | float
|
||||
- default: None
|
||||
- y:
|
||||
- description: The Y coordinate.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.sinh
|
||||
|
||||
Calculates the hyperbolic sine of a hyperbolic angle.
|
||||
|
||||
```typst
|
||||
#calc.sinh(0) \
|
||||
#calc.sinh(1.5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.sinh(
|
||||
value
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The hyperbolic angle whose hyperbolic sine to calculate.
|
||||
- type: float
|
||||
- default: None
|
||||
|
||||
# calc.cosh
|
||||
|
||||
Calculates the hyperbolic cosine of a hyperbolic angle.
|
||||
|
||||
```typst
|
||||
#calc.cosh(0) \
|
||||
#calc.cosh(1.5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.cosh(
|
||||
value
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The hyperbolic angle whose hyperbolic cosine to calculate.
|
||||
- type: float
|
||||
- default: None
|
||||
|
||||
# calc.tanh
|
||||
|
||||
Calculates the hyperbolic tangent of a hyperbolic angle.
|
||||
|
||||
```typst
|
||||
#calc.tanh(0) \
|
||||
#calc.tanh(1.5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.tanh(
|
||||
value
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The hyperbolic angle whose hyperbolic tangent to calculate.
|
||||
- type: float
|
||||
- default: None
|
||||
|
||||
# calc.log
|
||||
|
||||
Calculates the logarithm of a number.
|
||||
|
||||
If the base is not specified, the logarithm is calculated in base 10.
|
||||
|
||||
```typst
|
||||
#calc.log(100)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.log(
|
||||
value,
|
||||
base: float
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number whose logarithm to calculate. Must be strictly positive.
|
||||
- type: int | float
|
||||
- default: None
|
||||
- base:
|
||||
- description: The base of the logarithm. May not be zero.
|
||||
- type: float
|
||||
- default: 10.0
|
||||
|
||||
# calc.ln
|
||||
|
||||
Calculates the natural logarithm of a number.
|
||||
|
||||
```typst
|
||||
#calc.ln(calc.e)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.ln(
|
||||
value
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number whose logarithm to calculate. Must be strictly positive.
|
||||
- type: int | float
|
||||
- default: None
|
||||
|
||||
# calc.fact
|
||||
|
||||
Calculates the factorial of a number.
|
||||
|
||||
```typst
|
||||
#calc.fact(5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.fact(
|
||||
number
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- number:
|
||||
- description: The number whose factorial to calculate. Must be non-negative.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.perm
|
||||
|
||||
Calculates a permutation.
|
||||
|
||||
Returns the `k`-permutation of `n`, or the number of ways to choose `k` items from a set of `n` with regard to order.
|
||||
|
||||
```typst
|
||||
$ "perm"(n, k) &= n!/((n - k)!) \
|
||||
"perm"(5, 3) &= #calc.perm(5, 3) $
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.perm(
|
||||
base,
|
||||
numbers
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- base:
|
||||
- description: The base number. Must be non-negative.
|
||||
- type: int
|
||||
- default: None
|
||||
- numbers:
|
||||
- description: The number of permutations. Must be non-negative.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.binom
|
||||
|
||||
Calculates a binomial coefficient.
|
||||
|
||||
Returns the `k`-combination of `n`, or the number of ways to choose `k` items from a set of `n` without regard to order.
|
||||
|
||||
```typst
|
||||
#calc.binom(10, 5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.binom(
|
||||
n,
|
||||
k
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- n:
|
||||
- description: The upper coefficient. Must be non-negative.
|
||||
- type: int
|
||||
- default: None
|
||||
- k:
|
||||
- description: The lower coefficient. Must be non-negative.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.gcd
|
||||
|
||||
Calculates the greatest common divisor of two integers.
|
||||
|
||||
This will error if the result of integer division would be larger than the maximum 64-bit signed integer.
|
||||
|
||||
```typst
|
||||
#calc.gcd(7, 42)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.gcd(
|
||||
a,
|
||||
b
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- a:
|
||||
- description: The first integer.
|
||||
- type: int
|
||||
- default: None
|
||||
- b:
|
||||
- description: The second integer.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.lcm
|
||||
|
||||
Calculates the least common multiple of two integers.
|
||||
|
||||
```typst
|
||||
#calc.lcm(96, 13)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.lcm(
|
||||
a,
|
||||
b
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- a:
|
||||
- description: The first integer.
|
||||
- type: int
|
||||
- default: None
|
||||
- b:
|
||||
- description: The second integer.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.floor
|
||||
|
||||
Rounds a number down to the nearest integer.
|
||||
|
||||
If the number is already an integer, it is returned unchanged.
|
||||
|
||||
Note that this function will always return an [integer](/docs/reference/foundations/int/), and will error if the resulting [`float`](/docs/reference/foundations/float/) or [`decimal`](/docs/reference/foundations/decimal/) is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
|
||||
|
||||
```typst
|
||||
#calc.floor(500.1)
|
||||
#assert(calc.floor(3) == 3)
|
||||
#assert(calc.floor(3.14) == 3)
|
||||
#assert(calc.floor(decimal("-3.14")) == -4)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.floor(
|
||||
value
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to round down.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.ceil
|
||||
|
||||
Rounds a number up to the nearest integer.
|
||||
|
||||
If the number is already an integer, it is returned unchanged.
|
||||
|
||||
Note that this function will always return an [integer](/docs/reference/foundations/int/), and will error if the resulting [`float`](/docs/reference/foundations/float/) or [`decimal`](/docs/reference/foundations/decimal/) is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
|
||||
|
||||
```typst
|
||||
#calc.ceil(500.1)
|
||||
#assert(calc.ceil(3) == 3)
|
||||
#assert(calc.ceil(3.14) == 4)
|
||||
#assert(calc.ceil(decimal("-3.14")) == -3)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.ceil(
|
||||
value
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to round up.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.trunc
|
||||
|
||||
Returns the integer part of a number.
|
||||
|
||||
If the number is already an integer, it is returned unchanged.
|
||||
|
||||
Note that this function will always return an [integer](/docs/reference/foundations/int/), and will error if the resulting [`float`](/docs/reference/foundations/float/) or [`decimal`](/docs/reference/foundations/decimal/) is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
|
||||
|
||||
```typst
|
||||
#calc.trunc(15.9)
|
||||
#assert(calc.trunc(3) == 3)
|
||||
#assert(calc.trunc(-3.7) == -3)
|
||||
#assert(calc.trunc(decimal("8493.12949582390")) == 8493)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.trunc(
|
||||
value
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to truncate.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.fract
|
||||
|
||||
Returns the fractional part of a number.
|
||||
|
||||
If the number is an integer, returns `0`.
|
||||
|
||||
```typst
|
||||
#calc.fract(-3.1)
|
||||
#assert(calc.fract(3) == 0)
|
||||
#assert(calc.fract(decimal("234.23949211")) == decimal("0.23949211"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.fract(
|
||||
value
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to truncate.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.round
|
||||
|
||||
Rounds a number to the nearest integer.
|
||||
|
||||
Half-integers are rounded away from zero.
|
||||
|
||||
Optionally, a number of decimal places can be specified. If negative, its absolute value will indicate the amount of significant integer digits to remove before the decimal point.
|
||||
|
||||
Note that this function will return the same type as the operand. That is, applying `round` to a [`float`](/docs/reference/foundations/float/) will return a `float`, and to a [`decimal`](/docs/reference/foundations/decimal/), another `decimal`. You may explicitly convert the output of this function to an integer with [`int`](/docs/reference/foundations/int/), but note that such a conversion will error if the `float` or `decimal` is larger than the maximum 64-bit signed integer or smaller than the minimum integer.
|
||||
|
||||
In addition, this function can error if there is an attempt to round beyond the maximum or minimum integer or `decimal`. If the number is a `float`, such an attempt will cause `float.inf` or `-float.inf` to be returned for maximum and minimum respectively.
|
||||
|
||||
```typst
|
||||
#calc.round(3.1415, digits: 2)
|
||||
#assert(calc.round(3) == 3)
|
||||
#assert(calc.round(3.14) == 3)
|
||||
#assert(calc.round(3.5) == 4.0)
|
||||
#assert(calc.round(3333.45, digits: -2) == 3300.0)
|
||||
#assert(calc.round(-48953.45, digits: -3) == -49000.0)
|
||||
#assert(calc.round(3333, digits: -2) == 3300)
|
||||
#assert(calc.round(-48953, digits: -3) == -49000)
|
||||
#assert(calc.round(decimal("-6.5")) == decimal("-7"))
|
||||
#assert(calc.round(decimal("7.123456789"), digits: 6) == decimal("7.123457"))
|
||||
#assert(calc.round(decimal("3333.45"), digits: -2) == decimal("3300"))
|
||||
#assert(calc.round(decimal("-48953.45"), digits: -3) == decimal("-49000"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.round(
|
||||
value,
|
||||
digits: int
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to round.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- digits:
|
||||
- description: If positive, the number of decimal places. If negative, the number of significant integer digits that should be removed before the decimal point.
|
||||
- type: int
|
||||
- default: 0
|
||||
|
||||
# calc.clamp
|
||||
|
||||
Clamps a number between a minimum and maximum value.
|
||||
|
||||
```typst
|
||||
#calc.clamp(5, 0, 4)
|
||||
#assert(calc.clamp(5, 0, 10) == 5)
|
||||
#assert(calc.clamp(5, 6, 10) == 6)
|
||||
#assert(calc.clamp(decimal("5.45"), 2, decimal("45.9")) == decimal("5.45"))
|
||||
#assert(calc.clamp(decimal("5.45"), decimal("6.75"), 12) == decimal("6.75"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.clamp(
|
||||
value,
|
||||
min,
|
||||
max
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to clamp.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- min:
|
||||
- description: The inclusive minimum value.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- max:
|
||||
- description: The inclusive maximum value.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.min
|
||||
|
||||
Determines the minimum of a sequence of values.
|
||||
|
||||
```typst
|
||||
#calc.min(1, -3, -5, 20, 3, 6) \
|
||||
#calc.min("typst", "is", "cool")
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.min(
|
||||
values
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- values:
|
||||
- description: The sequence of values from which to extract the minimum. Must not be empty.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
# calc.max
|
||||
|
||||
Determines the maximum of a sequence of values.
|
||||
|
||||
```typst
|
||||
#calc.max(1, -3, -5, 20, 3, 6) \
|
||||
#calc.max("typst", "is", "cool")
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.max(
|
||||
values
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- values:
|
||||
- description: The sequence of values from which to extract the maximum. Must not be empty.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
# calc.even
|
||||
|
||||
Determines whether an integer is even.
|
||||
|
||||
```typst
|
||||
#calc.even(4) \
|
||||
#calc.even(5) \
|
||||
#range(10).filter(calc.even)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.even(
|
||||
value
|
||||
) -> bool
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to check for evenness.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.odd
|
||||
|
||||
Determines whether an integer is odd.
|
||||
|
||||
```typst
|
||||
#calc.odd(4) \
|
||||
#calc.odd(5) \
|
||||
#range(10).filter(calc.odd)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.odd(
|
||||
value
|
||||
) -> bool
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The number to check for oddness.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
# calc.rem
|
||||
|
||||
Calculates the remainder of two numbers.
|
||||
|
||||
The value `calc.rem(x, y)` always has the same sign as `x`, and is smaller in magnitude than `y`.
|
||||
|
||||
This can error if given a [`decimal`](/docs/reference/foundations/decimal/) input and the dividend is too small in magnitude compared to the divisor.
|
||||
|
||||
```typst
|
||||
#calc.rem(7, 3) \
|
||||
#calc.rem(7, -3) \
|
||||
#calc.rem(-7, 3) \
|
||||
#calc.rem(-7, -3) \
|
||||
#calc.rem(1.75, 0.5)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.rem(
|
||||
dividend,
|
||||
divisor
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- dividend:
|
||||
- description: The dividend of the remainder.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- divisor:
|
||||
- description: The divisor of the remainder.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.div-euclid
|
||||
|
||||
Performs euclidean division of two numbers.
|
||||
|
||||
The result of this computation is that of a division rounded to the integer `n` such that the dividend is greater than or equal to `n` times the divisor.
|
||||
|
||||
This can error if the resulting number is larger than the maximum value or smaller than the minimum value for its type.
|
||||
|
||||
```typst
|
||||
#calc.div-euclid(7, 3) \
|
||||
#calc.div-euclid(7, -3) \
|
||||
#calc.div-euclid(-7, 3) \
|
||||
#calc.div-euclid(-7, -3) \
|
||||
#calc.div-euclid(1.75, 0.5) \
|
||||
#calc.div-euclid(decimal("1.75"), decimal("0.5"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.div-euclid(
|
||||
dividend,
|
||||
divisor
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- dividend:
|
||||
- description: The dividend of the division.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- divisor:
|
||||
- description: The divisor of the division.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.rem-euclid
|
||||
|
||||
This calculates the least nonnegative remainder of a division.
|
||||
|
||||
Warning: Due to a floating point round-off error, the remainder may equal the absolute value of the divisor if the dividend is much smaller in magnitude than the divisor and the dividend is negative. This only applies for floating point inputs.
|
||||
|
||||
In addition, this can error if given a [`decimal`](/docs/reference/foundations/decimal/) input and the dividend is too small in magnitude compared to the divisor.
|
||||
|
||||
```typst
|
||||
#calc.rem-euclid(7, 3) \
|
||||
#calc.rem-euclid(7, -3) \
|
||||
#calc.rem-euclid(-7, 3) \
|
||||
#calc.rem-euclid(-7, -3) \
|
||||
#calc.rem-euclid(1.75, 0.5) \
|
||||
#calc.rem-euclid(decimal("1.75"), decimal("0.5"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.rem-euclid(
|
||||
dividend,
|
||||
divisor
|
||||
) -> int float decimal
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- dividend:
|
||||
- description: The dividend of the remainder.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- divisor:
|
||||
- description: The divisor of the remainder.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.quo
|
||||
|
||||
Calculates the quotient (floored division) of two numbers.
|
||||
|
||||
Note that this function will always return an [integer](/docs/reference/foundations/int/), and will error if the resulting number is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
|
||||
|
||||
```typst
|
||||
$ "quo"(a, b) &= floor(a/b) \
|
||||
"quo"(14, 5) &= #calc.quo(14, 5) \
|
||||
"quo"(3.46, 0.5) &= #calc.quo(3.46, 0.5) $
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.quo(
|
||||
dividend,
|
||||
divisor
|
||||
) -> int
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- dividend:
|
||||
- description: The dividend of the quotient.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
- divisor:
|
||||
- description: The divisor of the quotient.
|
||||
- type: int | float | decimal
|
||||
- default: None
|
||||
|
||||
# calc.norm
|
||||
|
||||
Calculates the p-norm of a sequence of values.
|
||||
|
||||
```typst
|
||||
#calc.norm(1, 2, -3, 0.5) \
|
||||
#calc.norm(p: 3, 1, 2)
|
||||
```
|
||||
|
||||
```typst
|
||||
#calc.norm(
|
||||
p: float,
|
||||
values
|
||||
) -> float
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- p:
|
||||
- description: The p value to calculate the p-norm of.
|
||||
- type: float
|
||||
- default: 2.0
|
||||
- values:
|
||||
- description: The sequence of values from which to calculate the p-norm. Returns `0.0` if empty.
|
||||
- type: float
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
# Content
|
||||
|
||||
A piece of document content.
|
||||
|
||||
This type is at the heart of Typst. All markup you write and most [functions](/docs/reference/foundations/function/) you call produce content values. You can create a content value by enclosing markup in square brackets. This is also how you pass content to functions.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
Type of *Hello!* is
|
||||
#type([*Hello!*])
|
||||
```
|
||||
|
||||
Content can be added with the `+` operator, [joined together](/docs/reference/scripting/#blocks) and multiplied with integers. Wherever content is expected, you can also pass a [string](/docs/reference/foundations/str/) or `none`.
|
||||
|
||||
## Representation
|
||||
|
||||
Content consists of elements with fields. When constructing an element with its _element function,_ you provide these fields as arguments and when you have a content value, you can access its fields with [field access syntax](/docs/reference/scripting/#field-access).
|
||||
|
||||
Some fields are required: These must be provided when constructing an element and as a consequence, they are always available through field access on content of that type. Required fields are marked as such in the documentation.
|
||||
|
||||
Most fields are optional: Like required fields, they can be passed to the element function to configure them for a single element. However, these can also be configured with [set rules](/docs/reference/styling/#set-rules) to apply them to all elements within a scope. Optional fields are only available with field access syntax when they were explicitly passed to the element function, not when they result from a set rule.
|
||||
|
||||
Each element has a default appearance. However, you can also completely customize its appearance with a [show rule](/docs/reference/styling/#show-rules). The show rule is passed the element. It can access the element's field and produce arbitrary content from it.
|
||||
|
||||
In the web app, you can hover over a content variable to see exactly which elements the content is composed of and what fields they have. Alternatively, you can inspect the output of the [`repr`](/docs/reference/foundations/repr/) function.
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## content.func
|
||||
|
||||
The content's element function. This function can be used to create the element contained in this content. It can be used in set and show rules for the element. Can be compared with global functions to check whether you have a specific kind of element.
|
||||
|
||||
## content.has
|
||||
|
||||
Whether the content has the specified field.
|
||||
|
||||
```typst
|
||||
#content.has(
|
||||
field
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- field:
|
||||
- description: The field to look for.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
## content.at
|
||||
|
||||
Access the specified field on the content. Returns the default value if the field does not exist or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#content.at(
|
||||
field,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- field:
|
||||
- description: The field to access.
|
||||
- type: str
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the field does not exist.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## content.fields
|
||||
|
||||
Returns the fields of this content.
|
||||
|
||||
```typst
|
||||
#rect(
|
||||
width: 10cm,
|
||||
height: 10cm,
|
||||
).fields()
|
||||
```
|
||||
|
||||
## content.location
|
||||
|
||||
The location of the content. This is only available on content returned by [query](/docs/reference/introspection/query/) or provided by a [show rule](/docs/reference/styling/#show-rules), for other content it will be `none`. The resulting location can be used with [counters](/docs/reference/introspection/counter/), [state](/docs/reference/introspection/state/) and [queries](/docs/reference/introspection/query/).
|
||||
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
# Datetime
|
||||
|
||||
Represents a date, a time, or a combination of both.
|
||||
|
||||
Can be created by either specifying a custom datetime using this type's constructor function or getting the current date with [`datetime.today`](/docs/reference/foundations/datetime/#definitions-today).
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let date = datetime(
|
||||
year: 2020,
|
||||
month: 10,
|
||||
day: 4,
|
||||
)
|
||||
|
||||
#date.display() \
|
||||
#date.display(
|
||||
"y:[year repr:last_two]"
|
||||
)
|
||||
|
||||
#let time = datetime(
|
||||
hour: 18,
|
||||
minute: 2,
|
||||
second: 23,
|
||||
)
|
||||
|
||||
#time.display() \
|
||||
#time.display(
|
||||
"h:[hour repr:12][period]"
|
||||
)
|
||||
```
|
||||
|
||||
## Datetime and Duration
|
||||
|
||||
You can get a [duration](/docs/reference/foundations/duration/) by subtracting two datetime:
|
||||
|
||||
```typst
|
||||
#let first-of-march = datetime(day: 1, month: 3, year: 2024)
|
||||
#let first-of-jan = datetime(day: 1, month: 1, year: 2024)
|
||||
#let distance = first-of-march - first-of-jan
|
||||
#distance.hours()
|
||||
```
|
||||
|
||||
You can also add/subtract a datetime and a duration to retrieve a new, offset datetime:
|
||||
|
||||
```typst
|
||||
#let date = datetime(day: 1, month: 3, year: 2024)
|
||||
#let two-days = duration(days: 2)
|
||||
#let two-days-earlier = date - two-days
|
||||
#let two-days-later = date + two-days
|
||||
|
||||
#date.display() \
|
||||
#two-days-earlier.display() \
|
||||
#two-days-later.display()
|
||||
```
|
||||
|
||||
## Format
|
||||
|
||||
You can specify a customized formatting using the [`display`](/docs/reference/foundations/datetime/#definitions-display) method. The format of a datetime is specified by providing _components_ with a specified number of _modifiers_. A component represents a certain part of the datetime that you want to display, and with the help of modifiers you can define how you want to display that component. In order to display a component, you wrap the name of the component in square brackets (e.g. `[year]` will display the year). In order to add modifiers, you add a space after the component name followed by the name of the modifier, a colon and the value of the modifier (e.g. `[month repr:short]` will display the short representation of the month).
|
||||
|
||||
The possible combination of components and their respective modifiers is as follows:
|
||||
|
||||
- `year`: Displays the year of the datetime.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the year is padded.
|
||||
- `repr` Can be either `full` in which case the full year is displayed or `last_two` in which case only the last two digits are displayed.
|
||||
- `sign`: Can be either `automatic` or `mandatory`. Specifies when the sign should be displayed.
|
||||
- `month`: Displays the month of the datetime.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the month is padded.
|
||||
- `repr`: Can be either `numerical`, `long` or `short`. Specifies if the month should be displayed as a number or a word. Unfortunately, when choosing the word representation, it can currently only display the English version. In the future, it is planned to support localization.
|
||||
- `day`: Displays the day of the datetime.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the day is padded.
|
||||
- `week_number`: Displays the week number of the datetime.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the week number is padded.
|
||||
- `repr`: Can be either `ISO`, `sunday` or `monday`. In the case of `ISO`, week numbers are between 1 and 53, while the other ones are between 0 and 53.
|
||||
- `weekday`: Displays the weekday of the date.
|
||||
- `repr` Can be either `long`, `short`, `sunday` or `monday`. In the case of `long` and `short`, the corresponding English name will be displayed (same as for the month, other languages are currently not supported). In the case of `sunday` and `monday`, the numerical value will be displayed (assuming Sunday and Monday as the first day of the week, respectively).
|
||||
- `one_indexed`: Can be either `true` or `false`. Defines whether the numerical representation of the week starts with 0 or 1.
|
||||
- `hour`: Displays the hour of the date.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the hour is padded.
|
||||
- `repr`: Can be either `24` or `12`. Changes whether the hour is displayed in the 24-hour or 12-hour format.
|
||||
- `period`: The AM/PM part of the hour
|
||||
- `case`: Can be `lower` to display it in lower case and `upper` to display it in upper case.
|
||||
- `minute`: Displays the minute of the date.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the minute is padded.
|
||||
- `second`: Displays the second of the date.
|
||||
- `padding`: Can be either `zero`, `space` or `none`. Specifies how the second is padded.
|
||||
|
||||
Keep in mind that not always all components can be used. For example, if you create a new datetime with `datetime(year: 2023, month: 10, day: 13)`, it will be stored as a plain date internally, meaning that you cannot use components such as `hour` or `minute`, which would only work on datetimes that have a specified time.
|
||||
|
||||
## Constructor
|
||||
## datetime
|
||||
|
||||
Creates a new datetime.
|
||||
|
||||
You can specify the [datetime](/docs/reference/foundations/datetime/) using a year, month, day, hour, minute, and second.
|
||||
|
||||
_Note_: Depending on which components of the datetime you specify, Typst will store it in one of the following three ways:
|
||||
|
||||
- If you specify year, month and day, Typst will store just a date.
|
||||
- If you specify hour, minute and second, Typst will store just a time.
|
||||
- If you specify all of year, month, day, hour, minute and second, Typst will store a full datetime.
|
||||
|
||||
Depending on how it is stored, the [`display`](/docs/reference/foundations/datetime/#definitions-display) method will choose a different formatting by default.
|
||||
|
||||
```typst
|
||||
#datetime(
|
||||
year: 2012,
|
||||
month: 8,
|
||||
day: 3,
|
||||
).display()
|
||||
```
|
||||
|
||||
```typst
|
||||
#datetime(
|
||||
year: int,
|
||||
month: int,
|
||||
day: int,
|
||||
hour: int,
|
||||
minute: int,
|
||||
second: int
|
||||
) -> datetime
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- year:
|
||||
- description: The year of the datetime.
|
||||
- type: int
|
||||
- default: None
|
||||
- month:
|
||||
- description: The month of the datetime.
|
||||
- type: int
|
||||
- default: None
|
||||
- day:
|
||||
- description: The day of the datetime.
|
||||
- type: int
|
||||
- default: None
|
||||
- hour:
|
||||
- description: The hour of the datetime.
|
||||
- type: int
|
||||
- default: None
|
||||
- minute:
|
||||
- description: The minute of the datetime.
|
||||
- type: int
|
||||
- default: None
|
||||
- second:
|
||||
- description: The second of the datetime.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## datetime.today
|
||||
|
||||
Returns the current date.
|
||||
|
||||
In the CLI, this can be overridden with the `--creation-timestamp` argument or by setting the [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/) environment variable. In both cases, the value should be given as a UNIX timestamp.
|
||||
|
||||
```typst
|
||||
Today's date is
|
||||
#datetime.today().display().
|
||||
```
|
||||
|
||||
```typst
|
||||
#datetime.today(
|
||||
offset: auto | int
|
||||
) -> datetime
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- offset:
|
||||
- description: An offset to apply to the current UTC date. If set to `auto`, the offset will be the local offset.
|
||||
- type: auto | int
|
||||
- default: auto
|
||||
|
||||
## datetime.display
|
||||
|
||||
Displays the datetime in a specified format.
|
||||
|
||||
Depending on whether you have defined just a date, a time or both, the default format will be different. If you specified a date, it will be `[year]-[month]-[day]`. If you specified a time, it will be `[hour]:[minute]:[second]`. In the case of a datetime, it will be `[year]-[month]-[day] [hour]:[minute]:[second]`.
|
||||
|
||||
See the [format syntax](/docs/reference/foundations/datetime/#format) for more information.
|
||||
|
||||
```typst
|
||||
#datetime.display(
|
||||
pattern
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The format used to display the datetime.
|
||||
- type: auto | str
|
||||
- default: auto
|
||||
|
||||
## datetime.year
|
||||
|
||||
The year if it was specified, or `none` for times without a date.
|
||||
|
||||
## datetime.month
|
||||
|
||||
The month if it was specified, or `none` for times without a date.
|
||||
|
||||
## datetime.weekday
|
||||
|
||||
The weekday (counting Monday as 1) or `none` for times without a date.
|
||||
|
||||
## datetime.day
|
||||
|
||||
The day if it was specified, or `none` for times without a date.
|
||||
|
||||
## datetime.hour
|
||||
|
||||
The hour if it was specified, or `none` for dates without a time.
|
||||
|
||||
## datetime.minute
|
||||
|
||||
The minute if it was specified, or `none` for dates without a time.
|
||||
|
||||
## datetime.second
|
||||
|
||||
The second if it was specified, or `none` for dates without a time.
|
||||
|
||||
## datetime.ordinal
|
||||
|
||||
The ordinal (day of the year), or `none` for times without a date.
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
# Decimal
|
||||
|
||||
A fixed-point decimal number type.
|
||||
|
||||
This type should be used for precise arithmetic operations on numbers represented in base 10. A typical use case is representing currency.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
Decimal: #(decimal("0.1") + decimal("0.2")) \
|
||||
Float: #(0.1 + 0.2)
|
||||
```
|
||||
|
||||
## Construction and casts
|
||||
|
||||
To create a decimal number, use the `decimal(string)` constructor, such as in `decimal("3.141592653")` **(note the double quotes!)**. This constructor preserves all given fractional digits, provided they are representable as per the limits specified below (otherwise, an error is raised).
|
||||
|
||||
You can also convert any [integer](/docs/reference/foundations/int/) to a decimal with the `decimal(int)` constructor, e.g. `decimal(59)`. However, note that constructing a decimal from a [floating-point number](/docs/reference/foundations/float/), while supported, **is an imprecise conversion and therefore discouraged.** A warning will be raised if Typst detects that there was an accidental `float` to `decimal` cast through its constructor, e.g. if writing `decimal(3.14)` (note the lack of double quotes, indicating this is an accidental `float` cast and therefore imprecise). It is recommended to use strings for constant decimal values instead (e.g. `decimal("3.14")`).
|
||||
|
||||
The precision of a `float` to `decimal` cast can be slightly improved by rounding the result to 15 digits with [`calc.round`](/docs/reference/foundations/calc/#functions-round), but there are still no precision guarantees for that kind of conversion.
|
||||
|
||||
## Operations
|
||||
|
||||
Basic arithmetic operations are supported on two decimals and on pairs of decimals and integers.
|
||||
|
||||
Built-in operations between `float` and `decimal` are not supported in order to guard against accidental loss of precision. They will raise an error instead.
|
||||
|
||||
Certain `calc` functions, such as trigonometric functions and power between two real numbers, are also only supported for `float` (although raising `decimal` to integer exponents is supported). You can opt into potentially imprecise operations with the `float(decimal)` constructor, which casts the `decimal` number into a `float`, allowing for operations without precision guarantees.
|
||||
|
||||
## Displaying decimals
|
||||
|
||||
To display a decimal, simply insert the value into the document. To only display a certain number of digits, [round](/docs/reference/foundations/calc/#functions-round) the decimal first. Localized formatting of decimals and other numbers is not yet supported, but planned for the future.
|
||||
|
||||
You can convert decimals to strings using the [`str`](/docs/reference/foundations/str/) constructor. This way, you can post-process the displayed representation, e.g. to replace the period with a comma (as a stand-in for proper built-in localization to languages that use the comma).
|
||||
|
||||
## Precision and limits
|
||||
|
||||
A `decimal` number has a limit of 28 to 29 significant base-10 digits. This includes the sum of digits before and after the decimal point. As such, numbers with more fractional digits have a smaller range. The maximum and minimum `decimal` numbers have a value of `79228162514264337593543950335` and `-79228162514264337593543950335` respectively. In contrast with [`float`](/docs/reference/foundations/float/), this type does not support infinity or NaN, so overflowing or underflowing operations will raise an error.
|
||||
|
||||
Typical operations between `decimal` numbers, such as addition, multiplication, and [power](/docs/reference/foundations/calc/#functions-pow) to an integer, will be highly precise due to their fixed-point representation. Note, however, that multiplication and division may not preserve all digits in some edge cases: while they are considered precise, digits past the limits specified above are rounded off and lost, so some loss of precision beyond the maximum representable digits is possible. Note that this behavior can be observed not only when dividing, but also when multiplying by numbers between 0 and 1, as both operations can push a number's fractional digits beyond the limits described above, leading to rounding. When those two operations do not surpass the digit limits, they are fully precise.
|
||||
|
||||
## Constructor
|
||||
## decimal
|
||||
|
||||
Converts a value to a `decimal`.
|
||||
|
||||
It is recommended to use a string to construct the decimal number, or an [integer](/docs/reference/foundations/int/) (if desired). The string must contain a number in the format `"3.14159"` (or `"-3.141519"` for negative numbers). The fractional digits are fully preserved; if that's not possible due to the limit of significant digits (around 28 to 29) having been reached, an error is raised as the given decimal number wouldn't be representable.
|
||||
|
||||
While this constructor can be used with [floating-point numbers](/docs/reference/foundations/float/) to cast them to `decimal`, doing so is **discouraged** as **this cast is
|
||||
inherently imprecise.** It is easy to accidentally perform this cast by writing `decimal(1.234)` (note the lack of double quotes), which is why Typst will emit a warning in that case. Please write `decimal("1.234")` instead for that particular case (initialization of a constant decimal). Also note that floats that are NaN or infinite cannot be cast to decimals and will raise an error.
|
||||
|
||||
```typst
|
||||
#decimal("1.222222222222222")
|
||||
```
|
||||
|
||||
```typst
|
||||
#decimal(
|
||||
value
|
||||
) -> decimal
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to a decimal.
|
||||
- type: bool | int | float | str | decimal
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
# Dictionary
|
||||
|
||||
A map from string keys to values.
|
||||
|
||||
You can construct a dictionary by enclosing comma-separated `key: value` pairs in parentheses. The values do not have to be of the same type. Since empty parentheses already yield an empty array, you have to use the special `(:)` syntax to create an empty dictionary.
|
||||
|
||||
A dictionary is conceptually similar to an [array](/docs/reference/foundations/array/), but it is indexed by strings instead of integers. You can access and create dictionary entries with the `.at()` method. If you know the key statically, you can alternatively use [field access notation](/docs/reference/scripting/#fields) (`.key`) to access the value. To check whether a key is present in the dictionary, use the `in` keyword.
|
||||
|
||||
You can iterate over the pairs in a dictionary using a [for loop](/docs/reference/scripting/#loops). This will iterate in the order the pairs were inserted / declared initially.
|
||||
|
||||
Dictionaries can be added with the `+` operator and [joined together](/docs/reference/scripting/#blocks). They can also be [spread](/docs/reference/foundations/arguments/#spreading) into a function call or another dictionary[1](#1) with the `..spread` operator. In each case, if a key appears multiple times, the last value will override the others.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let dict = (
|
||||
name: "Typst",
|
||||
born: 2019,
|
||||
)
|
||||
|
||||
#dict.name \
|
||||
#(dict.launch = 20)
|
||||
#dict.len() \
|
||||
#dict.keys() \
|
||||
#dict.values() \
|
||||
#dict.at("born") \
|
||||
#dict.insert("city", "Berlin")
|
||||
#("name" in dict)
|
||||
```
|
||||
|
||||
[^1]: When spreading into a dictionary, if all items between the parentheses are spread, you have to use the special `(:..spread)` syntax. Otherwise, it will spread into an array.
|
||||
|
||||
## Constructor
|
||||
## dictionary
|
||||
|
||||
Converts a value into a dictionary.
|
||||
|
||||
Note that this function is only intended for conversion of a dictionary-like value to a dictionary, not for creation of a dictionary from individual pairs. Use the dictionary syntax `(key: value)` instead.
|
||||
|
||||
```typst
|
||||
#dictionary(sys).at("version")
|
||||
```
|
||||
|
||||
```typst
|
||||
#dictionary(
|
||||
value
|
||||
) -> dictionary
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to a dictionary.
|
||||
- type: module
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## dictionary.len
|
||||
|
||||
The number of pairs in the dictionary.
|
||||
|
||||
## dictionary.at
|
||||
|
||||
Returns the value associated with the specified key in the dictionary. May be used on the left-hand side of an assignment if the key is already present in the dictionary. Returns the default value if the key is not part of the dictionary or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#dictionary.at(
|
||||
key,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- key:
|
||||
- description: The key at which to retrieve the item.
|
||||
- type: str
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the key is not part of the dictionary.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## dictionary.insert
|
||||
|
||||
Inserts a new pair into the dictionary. If the dictionary already contains this key, the value is updated.
|
||||
|
||||
To insert multiple pairs at once, you can just alternatively another dictionary with the `+=` operator.
|
||||
|
||||
```typst
|
||||
#dictionary.insert(
|
||||
key,
|
||||
value
|
||||
) ->
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- key:
|
||||
- description: The key of the pair that should be inserted.
|
||||
- type: str
|
||||
- default: None
|
||||
- value:
|
||||
- description: The value of the pair that should be inserted.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## dictionary.remove
|
||||
|
||||
Removes a pair from the dictionary by key and return the value.
|
||||
|
||||
```typst
|
||||
#dictionary.remove(
|
||||
key,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- key:
|
||||
- description: The key of the pair to remove.
|
||||
- type: str
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the key does not exist.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## dictionary.keys
|
||||
|
||||
Returns the keys of the dictionary as an array in insertion order.
|
||||
|
||||
## dictionary.values
|
||||
|
||||
Returns the values of the dictionary as an array in insertion order.
|
||||
|
||||
## dictionary.pairs
|
||||
|
||||
Returns the keys and values of the dictionary as an array of pairs. Each pair is represented as an array of length two.
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
# Duration
|
||||
|
||||
Represents a positive or negative span of time.
|
||||
|
||||
## Constructor
|
||||
## duration
|
||||
|
||||
Creates a new duration.
|
||||
|
||||
You can specify the [duration](/docs/reference/foundations/duration/) using weeks, days, hours, minutes and seconds. You can also get a duration by subtracting two [datetimes](/docs/reference/foundations/datetime/).
|
||||
|
||||
```typst
|
||||
#duration(
|
||||
days: 3,
|
||||
hours: 12,
|
||||
).hours()
|
||||
```
|
||||
|
||||
```typst
|
||||
#duration(
|
||||
seconds: int,
|
||||
minutes: int,
|
||||
hours: int,
|
||||
days: int,
|
||||
weeks: int
|
||||
) -> duration
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- seconds:
|
||||
- description: The number of seconds.
|
||||
- type: int
|
||||
- default: 0
|
||||
- minutes:
|
||||
- description: The number of minutes.
|
||||
- type: int
|
||||
- default: 0
|
||||
- hours:
|
||||
- description: The number of hours.
|
||||
- type: int
|
||||
- default: 0
|
||||
- days:
|
||||
- description: The number of days.
|
||||
- type: int
|
||||
- default: 0
|
||||
- weeks:
|
||||
- description: The number of weeks.
|
||||
- type: int
|
||||
- default: 0
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## duration.seconds
|
||||
|
||||
The duration expressed in seconds.
|
||||
|
||||
This function returns the total duration represented in seconds as a floating-point number rather than the second component of the duration.
|
||||
|
||||
## duration.minutes
|
||||
|
||||
The duration expressed in minutes.
|
||||
|
||||
This function returns the total duration represented in minutes as a floating-point number rather than the second component of the duration.
|
||||
|
||||
## duration.hours
|
||||
|
||||
The duration expressed in hours.
|
||||
|
||||
This function returns the total duration represented in hours as a floating-point number rather than the second component of the duration.
|
||||
|
||||
## duration.days
|
||||
|
||||
The duration expressed in days.
|
||||
|
||||
This function returns the total duration represented in days as a floating-point number rather than the second component of the duration.
|
||||
|
||||
## duration.weeks
|
||||
|
||||
The duration expressed in weeks.
|
||||
|
||||
This function returns the total duration represented in weeks as a floating-point number rather than the second component of the duration.
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# Evaluate
|
||||
|
||||
# eval
|
||||
|
||||
Evaluates a string as Typst code.
|
||||
|
||||
This function should only be used as a last resort.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#eval("1 + 1") \
|
||||
#eval("(1, 2, 3, 4)").len() \
|
||||
#eval("*Markup!*", mode: "markup") \
|
||||
```
|
||||
|
||||
```typst
|
||||
#eval(
|
||||
source,
|
||||
mode: str,
|
||||
scope: dictionary
|
||||
) -> any
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A string of Typst code to evaluate.
|
||||
- type: str
|
||||
- default: None
|
||||
- mode:
|
||||
- description: The [syntactical mode](/docs/reference/syntax/#modes) in which the string is parsed. ```typst #eval("= Heading", mode: "markup") #eval("1_2^3", mode: "math") ```
|
||||
- type: str
|
||||
- default: "code"
|
||||
- scope:
|
||||
- description: A scope of definitions that are made available. ```typst #eval("x + 1", scope: (x: 2)) \\ #eval( "abc/xyz", mode: "math", scope: ( abc: $a + b + c$, xyz: $x + y + z$, ), ) ```
|
||||
- type: dictionary
|
||||
- default: (:)
|
||||
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
# Float
|
||||
|
||||
A floating-point number.
|
||||
|
||||
A limited-precision representation of a real number. Typst uses 64 bits to store floats. Wherever a float is expected, you can also pass an [integer](/docs/reference/foundations/int/).
|
||||
|
||||
You can convert a value to a float with this type's constructor.
|
||||
|
||||
NaN and positive infinity are available as `float.nan` and `float.inf` respectively.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#3.14 \
|
||||
#1e4 \
|
||||
#(10 / 4)
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## float
|
||||
|
||||
Converts a value to a float.
|
||||
|
||||
- Booleans are converted to `0.0` or `1.0`.
|
||||
- Integers are converted to the closest 64-bit float. For integers with absolute value less than `calc.pow(2, 53)`, this conversion is exact.
|
||||
- Ratios are divided by 100%.
|
||||
- Strings are parsed in base 10 to the closest 64-bit float. Exponential notation is supported.
|
||||
|
||||
```typst
|
||||
#float(false) \
|
||||
#float(true) \
|
||||
#float(4) \
|
||||
#float(40%) \
|
||||
#float("2.7") \
|
||||
#float("1e5")
|
||||
```
|
||||
|
||||
```typst
|
||||
#float(
|
||||
value
|
||||
) -> float
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to a float.
|
||||
- type: bool | int | float | ratio | str | decimal
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## float.is-nan
|
||||
|
||||
Checks if a float is not a number.
|
||||
|
||||
In IEEE 754, more than one bit pattern represents a NaN. This function returns `true` if the float is any of those bit patterns.
|
||||
|
||||
```typst
|
||||
#float.is-nan(0) \
|
||||
#float.is-nan(1) \
|
||||
#float.is-nan(float.nan)
|
||||
```
|
||||
|
||||
## float.is-infinite
|
||||
|
||||
Checks if a float is infinite.
|
||||
|
||||
Floats can represent positive infinity and negative infinity. This function returns `true` if the float is an infinity.
|
||||
|
||||
```typst
|
||||
#float.is-infinite(0) \
|
||||
#float.is-infinite(1) \
|
||||
#float.is-infinite(float.inf)
|
||||
```
|
||||
|
||||
## float.signum
|
||||
|
||||
Calculates the sign of a floating point number.
|
||||
|
||||
- If the number is positive (including `+0.0`), returns `1.0`.
|
||||
- If the number is negative (including `-0.0`), returns `-1.0`.
|
||||
- If the number is NaN, returns `float.nan`.
|
||||
|
||||
```typst
|
||||
#(5.0).signum() \
|
||||
#(-5.0).signum() \
|
||||
#(0.0).signum() \
|
||||
#float.nan.signum()
|
||||
```
|
||||
|
||||
## float.from-bytes
|
||||
|
||||
Interprets bytes as a float.
|
||||
|
||||
```typst
|
||||
#float.from-bytes(bytes((0, 0, 0, 0, 0, 0, 240, 63))) \
|
||||
#float.from-bytes(bytes((63, 240, 0, 0, 0, 0, 0, 0)), endian: "big")
|
||||
```
|
||||
|
||||
```typst
|
||||
#float.from-bytes(
|
||||
bytes,
|
||||
endian: str
|
||||
) -> float
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- bytes:
|
||||
- description: The bytes that should be converted to a float. Must have a length of either 4 or 8. The bytes are then interpreted in [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754)\'s binary32 (single-precision) or binary64 (double-precision) format depending on the length of the bytes.
|
||||
- type: bytes
|
||||
- default: None
|
||||
- endian:
|
||||
- description: The endianness of the conversion.
|
||||
- type: str
|
||||
- default: "little"
|
||||
|
||||
## float.to-bytes
|
||||
|
||||
Converts a float to bytes.
|
||||
|
||||
```typst
|
||||
#array(1.0.to-bytes(endian: "big")) \
|
||||
#array(1.0.to-bytes())
|
||||
```
|
||||
|
||||
```typst
|
||||
#float.to-bytes(
|
||||
endian: str,
|
||||
size: int
|
||||
) -> bytes
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- endian:
|
||||
- description: The endianness of the conversion.
|
||||
- type: str
|
||||
- default: "little"
|
||||
- size:
|
||||
- description: The size of the resulting bytes. This must be either 4 or 8. The call will return the representation of this float in either [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754)\'s binary32 (single-precision) or binary64 (double-precision) format depending on the provided size.
|
||||
- type: int
|
||||
- default: 8
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
# Function
|
||||
|
||||
A mapping from argument values to a return value.
|
||||
|
||||
You can call a function by writing a comma-separated list of function _arguments_ enclosed in parentheses directly after the function name. Additionally, you can pass any number of trailing content block arguments to a function _after_ the normal argument list. If the normal argument list would become empty, it can be omitted. Typst supports positional and named arguments. The former are identified by position and type, while the latter are written as `name: value`.
|
||||
|
||||
Within math mode, function calls have special behaviour. See the [math documentation](/docs/reference/math/) for more details.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
// Call a function.
|
||||
#list([A], [B])
|
||||
|
||||
// Named arguments and trailing
|
||||
// content blocks.
|
||||
#enum(start: 2)[A][B]
|
||||
|
||||
// Version without parentheses.
|
||||
#list[A][B]
|
||||
```
|
||||
|
||||
Functions are a fundamental building block of Typst. Typst provides functions for a variety of typesetting tasks. Moreover, the markup you write is backed by functions and all styling happens through functions. This reference lists all available functions and how you can use them. Please also refer to the documentation about [set](/docs/reference/styling/#set-rules) and [show](/docs/reference/styling/#show-rules) rules to learn about additional ways you can work with functions in Typst.
|
||||
|
||||
## Element functions
|
||||
|
||||
Some functions are associated with _elements_ like [headings](/docs/reference/model/heading/) or [tables](/docs/reference/model/table/). When called, these create an element of their respective kind. In contrast to normal functions, they can further be used in [set rules](/docs/reference/styling/#set-rules), [show rules](/docs/reference/styling/#show-rules), and [selectors](/docs/reference/foundations/selector/).
|
||||
|
||||
## Function scopes
|
||||
|
||||
Functions can hold related definitions in their own scope, similar to a [module](/docs/reference/scripting/#modules). Examples of this are [`assert.eq`](/docs/reference/foundations/assert/#definitions-eq) or [`list.item`](/docs/reference/model/list/#definitions-item). However, this feature is currently only available for built-in functions.
|
||||
|
||||
## Defining functions
|
||||
|
||||
You can define your own function with a [let binding](/docs/reference/scripting/#bindings) that has a parameter list after the binding's name. The parameter list can contain mandatory positional parameters, named parameters with default values and [argument sinks](/docs/reference/foundations/arguments/).
|
||||
|
||||
The right-hand side of a function binding is the function body, which can be a block or any other expression. It defines the function's return value and can depend on the parameters. If the function body is a [code block](/docs/reference/scripting/#blocks), the return value is the result of joining the values of each expression in the block.
|
||||
|
||||
Within a function body, the `return` keyword can be used to exit early and optionally specify a return value. If no explicit return value is given, the body evaluates to the result of joining all expressions preceding the `return`.
|
||||
|
||||
Functions that don't return any meaningful value return [`none`](/docs/reference/foundations/none/) instead. The return type of such functions is not explicitly specified in the documentation. (An example of this is [`array.push`](/docs/reference/foundations/array/#definitions-push)).
|
||||
|
||||
```typst
|
||||
#let alert(body, fill: red) = {
|
||||
set text(white)
|
||||
set align(center)
|
||||
rect(
|
||||
fill: fill,
|
||||
inset: 8pt,
|
||||
radius: 4pt,
|
||||
[*Warning:\ #body*],
|
||||
)
|
||||
}
|
||||
|
||||
#alert[
|
||||
Danger is imminent!
|
||||
]
|
||||
|
||||
#alert(fill: blue)[
|
||||
KEEP OFF TRACKS
|
||||
]
|
||||
```
|
||||
|
||||
## Importing functions
|
||||
|
||||
Functions can be imported from one file ([`module`](/docs/reference/scripting/#modules)) into another using `import`. For example, assume that we have defined the `alert` function from the previous example in a file called `foo.typ`. We can import it into another file by writing `import "foo.typ": alert`.
|
||||
|
||||
## Unnamed functions
|
||||
|
||||
You can also create an unnamed function without creating a binding by specifying a parameter list followed by `=>` and the function body. If your function has just one parameter, the parentheses around the parameter list are optional. Unnamed functions are mainly useful for show rules, but also for settable properties that take functions like the page function's [`footer`](/docs/reference/layout/page/#parameters-footer) property.
|
||||
|
||||
```typst
|
||||
#show "once?": it => [#it #it]
|
||||
once?
|
||||
```
|
||||
|
||||
## Note on function purity
|
||||
|
||||
In Typst, all functions are _pure._ This means that for the same arguments, they always return the same result. They cannot "remember" things to produce another value when they are called a second time.
|
||||
|
||||
The only exception are built-in methods like [`array.push(value)`](/docs/reference/foundations/array/#definitions-push). These can modify the values they are called on.
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## function.with
|
||||
|
||||
Returns a new function that has the given arguments pre-applied.
|
||||
|
||||
```typst
|
||||
#function.with(
|
||||
arguments
|
||||
) -> function
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- arguments:
|
||||
- description: The arguments to apply to the function.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## function.where
|
||||
|
||||
Returns a selector that filters for elements belonging to this function whose fields have the values of the given arguments.
|
||||
|
||||
```typst
|
||||
#show heading.where(level: 2): set text(blue)
|
||||
= Section
|
||||
== Subsection
|
||||
=== Sub-subsection
|
||||
```
|
||||
|
||||
```typst
|
||||
#function.where(
|
||||
fields
|
||||
) -> selector
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- fields:
|
||||
- description: The fields to filter for.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
# Foundations
|
||||
|
||||
Foundational types and functions.
|
||||
|
||||
Here, you'll find documentation for basic data types like [integers](/docs/reference/foundations/int/) and [strings](/docs/reference/foundations/str/) as well as details about core computational functions.
|
||||
|
||||
## Definitions
|
||||
|
||||
| Name | Description |
|
||||
| --- | --- |
|
||||
| [`arguments`](/reference/foundations/arguments/) | Captured arguments to a function. |
|
||||
| [`array`](/reference/foundations/array/) | A sequence of values. |
|
||||
| [`assert`](/reference/foundations/assert/) | Ensures that a condition is fulfilled. |
|
||||
| [`auto`](/reference/foundations/auto/) | A value that indicates a smart default. |
|
||||
| [`bool`](/reference/foundations/bool/) | A type with two states. |
|
||||
| [`bytes`](/reference/foundations/bytes/) | A sequence of bytes. |
|
||||
| [`calc`](/reference/foundations/calc/) | Module for calculations and processing of numeric values. |
|
||||
| [`content`](/reference/foundations/content/) | A piece of document content. |
|
||||
| [`datetime`](/reference/foundations/datetime/) | Represents a date, a time, or a combination of both. |
|
||||
| [`decimal`](/reference/foundations/decimal/) | A fixed-point decimal number type. |
|
||||
| [`dictionary`](/reference/foundations/dictionary/) | A map from string keys to values. |
|
||||
| [`duration`](/reference/foundations/duration/) | Represents a positive or negative span of time. |
|
||||
| [`eval`](/reference/foundations/eval/) | Evaluates a string as Typst code. |
|
||||
| [`float`](/reference/foundations/float/) | A floating-point number. |
|
||||
| [`function`](/reference/foundations/function/) | A mapping from argument values to a return value. |
|
||||
| [`int`](/reference/foundations/int/) | A whole number. |
|
||||
| [`label`](/reference/foundations/label/) | A label for an element. |
|
||||
| [`module`](/reference/foundations/module/) | A collection of variables and functions that are commonly related to a single theme. |
|
||||
| [`none`](/reference/foundations/none/) | A value that indicates the absence of any other value. |
|
||||
| [`panic`](/reference/foundations/panic/) | Fails with an error. |
|
||||
| [`plugin`](/reference/foundations/plugin/) | Loads a WebAssembly module. |
|
||||
| [`regex`](/reference/foundations/regex/) | A regular expression. |
|
||||
| [`repr`](/reference/foundations/repr/) | Returns the string representation of a value. |
|
||||
| [`selector`](/reference/foundations/selector/) | A filter for selecting elements within the document. |
|
||||
| [`std`](/reference/foundations/std/) | A module that contains all globally accessible items. |
|
||||
| [`str`](/reference/foundations/str/) | A sequence of Unicode codepoints. |
|
||||
| [`symbol`](/reference/foundations/symbol/) | A Unicode symbol. |
|
||||
| [`sys`](/reference/foundations/sys/) | Module for system interactions. |
|
||||
| [`target`](/reference/foundations/target/) | Returns the current export target. |
|
||||
| [`type`](/reference/foundations/type/) | Describes a kind of value. |
|
||||
| [`version`](/reference/foundations/version/) | A version with an arbitrary number of components. |
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
# Integer
|
||||
|
||||
A whole number.
|
||||
|
||||
The number can be negative, zero, or positive. As Typst uses 64 bits to store integers, integers cannot be smaller than `-9223372036854775808` or larger than `9223372036854775807`. Integer literals are always positive, so a negative integer such as `-1` is semantically the negation `-` of the positive literal `1`. A positive integer greater than the maximum value and a negative integer less than or equal to the minimum value cannot be represented as an integer literal, and are instead parsed as a `float`. The minimum integer value can still be obtained through integer arithmetic.
|
||||
|
||||
The number can also be specified as hexadecimal, octal, or binary by starting it with a zero followed by either `x`, `o`, or `b`.
|
||||
|
||||
You can convert a value to an integer with this type's constructor.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#(1 + 2) \
|
||||
#(2 - 5) \
|
||||
#(3 + 4 < 8)
|
||||
|
||||
#0xff \
|
||||
#0o10 \
|
||||
#0b1001
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## int
|
||||
|
||||
Converts a value to an integer. Raises an error if there is an attempt to produce an integer larger than the maximum 64-bit signed integer or smaller than the minimum 64-bit signed integer.
|
||||
|
||||
- Booleans are converted to `0` or `1`.
|
||||
- Floats and decimals are rounded to the next 64-bit integer towards zero.
|
||||
- Strings are parsed in base 10.
|
||||
|
||||
```typst
|
||||
#int(false) \
|
||||
#int(true) \
|
||||
#int(2.7) \
|
||||
#int(decimal("3.8")) \
|
||||
#(int("27") + int("4"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#int(
|
||||
value
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to an integer.
|
||||
- type: bool | int | float | str | decimal
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## int.signum
|
||||
|
||||
Calculates the sign of an integer.
|
||||
|
||||
- If the number is positive, returns `1`.
|
||||
- If the number is negative, returns `-1`.
|
||||
- If the number is zero, returns `0`.
|
||||
|
||||
```typst
|
||||
#(5).signum() \
|
||||
#(-5).signum() \
|
||||
#(0).signum()
|
||||
```
|
||||
|
||||
## int.bit-not
|
||||
|
||||
Calculates the bitwise NOT of an integer.
|
||||
|
||||
For the purposes of this function, the operand is treated as a signed integer of 64 bits.
|
||||
|
||||
```typst
|
||||
#4.bit-not() \
|
||||
#(-1).bit-not()
|
||||
```
|
||||
|
||||
## int.bit-and
|
||||
|
||||
Calculates the bitwise AND between two integers.
|
||||
|
||||
For the purposes of this function, the operands are treated as signed integers of 64 bits.
|
||||
|
||||
```typst
|
||||
#128.bit-and(192)
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.bit-and(
|
||||
rhs
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- rhs:
|
||||
- description: The right-hand operand of the bitwise AND.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## int.bit-or
|
||||
|
||||
Calculates the bitwise OR between two integers.
|
||||
|
||||
For the purposes of this function, the operands are treated as signed integers of 64 bits.
|
||||
|
||||
```typst
|
||||
#64.bit-or(32)
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.bit-or(
|
||||
rhs
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- rhs:
|
||||
- description: The right-hand operand of the bitwise OR.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## int.bit-xor
|
||||
|
||||
Calculates the bitwise XOR between two integers.
|
||||
|
||||
For the purposes of this function, the operands are treated as signed integers of 64 bits.
|
||||
|
||||
```typst
|
||||
#64.bit-xor(96)
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.bit-xor(
|
||||
rhs
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- rhs:
|
||||
- description: The right-hand operand of the bitwise XOR.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## int.bit-lshift
|
||||
|
||||
Shifts the operand's bits to the left by the specified amount.
|
||||
|
||||
For the purposes of this function, the operand is treated as a signed integer of 64 bits. An error will occur if the result is too large to fit in a 64-bit integer.
|
||||
|
||||
```typst
|
||||
#33.bit-lshift(2) \
|
||||
#(-1).bit-lshift(3)
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.bit-lshift(
|
||||
shift
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- shift:
|
||||
- description: The amount of bits to shift. Must not be negative.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## int.bit-rshift
|
||||
|
||||
Shifts the operand's bits to the right by the specified amount. Performs an arithmetic shift by default (extends the sign bit to the left, such that negative numbers stay negative), but that can be changed by the `logical` parameter.
|
||||
|
||||
For the purposes of this function, the operand is treated as a signed integer of 64 bits.
|
||||
|
||||
```typst
|
||||
#64.bit-rshift(2) \
|
||||
#(-8).bit-rshift(2) \
|
||||
#(-8).bit-rshift(2, logical: true)
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.bit-rshift(
|
||||
shift,
|
||||
logical: bool
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- shift:
|
||||
- description: The amount of bits to shift. Must not be negative. Shifts larger than 63 are allowed and will cause the return value to saturate. For non-negative numbers, the return value saturates at `0`, while, for negative numbers, it saturates at `-1` if `logical` is set to `false`, or `0` if it is `true`. This behavior is consistent with just applying this operation multiple times. Therefore, the shift will always succeed.
|
||||
- type: int
|
||||
- default: None
|
||||
- logical:
|
||||
- description: Toggles whether a logical (unsigned) right shift should be performed instead of arithmetic right shift. If this is `true`, negative operands will not preserve their sign bit, and bits which appear to the left after the shift will be `0`. This parameter has no effect on non-negative operands.
|
||||
- type: bool
|
||||
- default: false
|
||||
|
||||
## int.from-bytes
|
||||
|
||||
Converts bytes to an integer.
|
||||
|
||||
```typst
|
||||
#int.from-bytes(bytes((0, 0, 0, 0, 0, 0, 0, 1))) \
|
||||
#int.from-bytes(bytes((1, 0, 0, 0, 0, 0, 0, 0)), endian: "big")
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.from-bytes(
|
||||
bytes,
|
||||
endian: str,
|
||||
signed: bool
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- bytes:
|
||||
- description: The bytes that should be converted to an integer. Must be of length at most 8 so that the result fits into a 64-bit signed integer.
|
||||
- type: bytes
|
||||
- default: None
|
||||
- endian:
|
||||
- description: The endianness of the conversion.
|
||||
- type: str
|
||||
- default: "little"
|
||||
- signed:
|
||||
- description: Whether the bytes should be treated as a signed integer. If this is `true` and the most significant bit is set, the resulting number will negative.
|
||||
- type: bool
|
||||
- default: true
|
||||
|
||||
## int.to-bytes
|
||||
|
||||
Converts an integer to bytes.
|
||||
|
||||
```typst
|
||||
#array(10000.to-bytes(endian: "big")) \
|
||||
#array(10000.to-bytes(size: 4))
|
||||
```
|
||||
|
||||
```typst
|
||||
#int.to-bytes(
|
||||
endian: str,
|
||||
size: int
|
||||
) -> bytes
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- endian:
|
||||
- description: The endianness of the conversion.
|
||||
- type: str
|
||||
- default: "little"
|
||||
- size:
|
||||
- description: The size in bytes of the resulting bytes (must be at least zero). If the integer is too large to fit in the specified size, the conversion will truncate the remaining bytes based on the endianness. To keep the same resulting value, if the endianness is big-endian, the truncation will happen at the rightmost bytes. Otherwise, if the endianness is little-endian, the truncation will happen at the leftmost bytes. Be aware that if the integer is negative and the size is not enough to make the number fit, when passing the resulting bytes to `int.from-bytes`, the resulting number might be positive, as the most significant bit might not be set to 1.
|
||||
- type: int
|
||||
- default: 8
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
# Label
|
||||
|
||||
A label for an element.
|
||||
|
||||
Inserting a label into content attaches it to the closest preceding element that is not a space. The preceding element must be in the same scope as the label, which means that `Hello #[<label>]`, for instance, wouldn't work.
|
||||
|
||||
A labelled element can be [referenced](/docs/reference/model/ref/), [queried](/docs/reference/introspection/query/) for, and [styled](/docs/reference/styling/) through its label.
|
||||
|
||||
Once constructed, you can get the name of a label using [`str`](/docs/reference/foundations/str/#constructor).
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#show <a>: set text(blue)
|
||||
#show label("b"): set text(red)
|
||||
|
||||
= Heading <a>
|
||||
*Strong* #label("b")
|
||||
```
|
||||
|
||||
## Syntax
|
||||
|
||||
This function also has dedicated syntax: You can create a label by enclosing its name in angle brackets. This works both in markup and code. A label's name can contain letters, numbers, `_`, `-`, `:`, and `.`. A label cannot be empty.
|
||||
|
||||
Note that there is a syntactical difference when using the dedicated syntax for this function. In the code below, the `<a>` terminates the heading and thus attaches to the heading itself, whereas the `#label("b")` is part of the heading and thus attaches to the heading's text.
|
||||
|
||||
```typst
|
||||
// Equivalent to `#heading[Introduction] <a>`.
|
||||
= Introduction <a>
|
||||
|
||||
// Equivalent to `#heading[Conclusion #label("b")]`.
|
||||
= Conclusion #label("b")
|
||||
```
|
||||
|
||||
Currently, labels can only be attached to elements in markup mode, not in code mode. This might change in the future.
|
||||
|
||||
## Constructor
|
||||
## label
|
||||
|
||||
Creates a label from a string.
|
||||
|
||||
```typst
|
||||
#label(
|
||||
name
|
||||
) -> label
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- name:
|
||||
- description: The name of the label. Unlike the [dedicated syntax](#syntax), this constructor accepts any non-empty string, including names with special characters.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# Module
|
||||
|
||||
A collection of variables and functions that are commonly related to a single theme.
|
||||
|
||||
A module can
|
||||
|
||||
- be built-in
|
||||
- stem from a [file import](/docs/reference/scripting/#modules)
|
||||
- stem from a [package import](/docs/reference/scripting/#packages) (and thus indirectly its entrypoint file)
|
||||
- result from a call to the [plugin](/docs/reference/foundations/plugin/) function
|
||||
|
||||
You can access definitions from the module using [field access notation](/docs/reference/scripting/#fields) and interact with it using the [import and include syntaxes](/docs/reference/scripting/#modules).
|
||||
|
||||
```typst
|
||||
#import "utils.typ"
|
||||
#utils.add(2, 5)
|
||||
|
||||
#import utils: sub
|
||||
#sub(1, 4)
|
||||
```
|
||||
|
||||
You can check whether a definition is present in a module using the `in` operator, with a string on the left-hand side. This can be useful to [conditionally access](/docs/reference/foundations/std/#conditional-access) definitions in a module.
|
||||
|
||||
```typst
|
||||
#("table" in std) \
|
||||
#("nope" in std)
|
||||
```
|
||||
|
||||
Alternatively, it is possible to convert a module to a dictionary, and therefore access its contents dynamically, using the [dictionary constructor](/docs/reference/foundations/dictionary/#constructor).
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# None
|
||||
|
||||
A value that indicates the absence of any other value.
|
||||
|
||||
The none type has exactly one value: `none`.
|
||||
|
||||
When inserted into the document, it is not visible. This is also the value that is produced by empty code blocks. It can be [joined](/docs/reference/scripting/#blocks) with any value, yielding the other value.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
Not visible: #none
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Panic
|
||||
|
||||
# panic
|
||||
|
||||
Fails with an error.
|
||||
|
||||
Arguments are displayed to the user (not rendered in the document) as strings, converting with `repr` if necessary.
|
||||
|
||||
## Example
|
||||
|
||||
The code below produces the error `panicked with: "this is wrong"`.
|
||||
|
||||
```typst
|
||||
#panic("this is wrong")
|
||||
```
|
||||
|
||||
```typst
|
||||
#panic(
|
||||
values
|
||||
) ->
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- values:
|
||||
- description: The values to panic with and display to the user.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
# Plugin
|
||||
|
||||
# plugin
|
||||
|
||||
Loads a WebAssembly module.
|
||||
|
||||
The resulting [module](/docs/reference/foundations/module/) will contain one Typst [function](/docs/reference/foundations/function/) for each function export of the loaded WebAssembly module.
|
||||
|
||||
Typst WebAssembly plugins need to follow a specific [protocol](/docs/reference/foundations/plugin/#protocol). To run as a plugin, a program needs to be compiled to a 32-bit shared WebAssembly library. Plugin functions may accept multiple [byte buffers](/docs/reference/foundations/bytes/) as arguments and return a single byte buffer. They should typically be wrapped in idiomatic Typst functions that perform the necessary conversions between native Typst types and bytes by leveraging [`str`](/docs/reference/foundations/str/#constructor), [`bytes`](/docs/reference/foundations/bytes/#constructor), and [data loading functions](/docs/reference/data-loading/).
|
||||
|
||||
For security reasons, plugins run in isolation from your system. This means that printing, reading files, or similar things are not supported.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let myplugin = plugin("hello.wasm")
|
||||
#let concat(a, b) = str(
|
||||
myplugin.concatenate(
|
||||
bytes(a),
|
||||
bytes(b),
|
||||
)
|
||||
)
|
||||
|
||||
#concat("hello", "world")
|
||||
```
|
||||
|
||||
Since the plugin function returns a module, it can be used with import syntax:
|
||||
|
||||
```typst
|
||||
#import plugin("hello.wasm"): concatenate
|
||||
```
|
||||
|
||||
## Purity
|
||||
|
||||
Plugin functions **must be pure:** A plugin function call must not have any observable side effects on future plugin calls and given the same arguments, it must always return the same value.
|
||||
|
||||
The reason for this is that Typst functions must be pure (which is quite fundamental to the language design) and, since Typst function can call plugin functions, this requirement is inherited. In particular, if a plugin function is called twice with the same arguments, Typst might cache the results and call your function only once. Moreover, Typst may run multiple instances of your plugin in multiple threads, with no state shared between them.
|
||||
|
||||
Typst does not enforce plugin function purity (for efficiency reasons), but calling an impure function will lead to unpredictable and irreproducible results and must be avoided.
|
||||
|
||||
That said, mutable operations _can be_ useful for plugins that require costly runtime initialization. Due to the purity requirement, such initialization cannot be performed through a normal function call. Instead, Typst exposes a [plugin transition API](/docs/reference/foundations/plugin/#definitions-transition), which executes a function call and then creates a derived module with new functions which will observe the side effects produced by the transition call. The original plugin remains unaffected.
|
||||
|
||||
## Plugins and Packages
|
||||
|
||||
Any Typst code can make use of a plugin simply by including a WebAssembly file and loading it. However, because the byte-based plugin interface is quite low-level, plugins are typically exposed through a package containing the plugin and idiomatic wrapper functions.
|
||||
|
||||
## WASI
|
||||
|
||||
Many compilers will use the [WASI ABI](https://wasi.dev/) by default or as their only option (e.g. emscripten), which allows printing, reading files, etc. This ABI will not directly work with Typst. You will either need to compile to a different target or [stub all functions](https://github.com/astrale-sharp/wasm-minimal-protocol/tree/master/crates/wasi-stub).
|
||||
|
||||
## Protocol
|
||||
|
||||
To be used as a plugin, a WebAssembly module must conform to the following protocol:
|
||||
|
||||
### Exports
|
||||
|
||||
A plugin module can export functions to make them callable from Typst. To conform to the protocol, an exported function should:
|
||||
|
||||
- Take `n` 32-bit integer arguments `a_1`, `a_2`, ..., `a_n` (interpreted as lengths, so `usize/size_t` may be preferable), and return one 32-bit integer.
|
||||
- The function should first allocate a buffer `buf` of length `a_1 + a_2 + ... + a_n`, and then call `wasm_minimal_protocol_write_args_to_buffer(buf.ptr)`.
|
||||
- The `a_1` first bytes of the buffer now constitute the first argument, the `a_2` next bytes the second argument, and so on.
|
||||
- The function can now do its job with the arguments and produce an output buffer. Before returning, it should call `wasm_minimal_protocol_send_result_to_host` to send its result back to the host.
|
||||
- To signal success, the function should return `0`.
|
||||
- To signal an error, the function should return `1`. The written buffer is then interpreted as an UTF-8 encoded error message.
|
||||
|
||||
### Imports
|
||||
|
||||
Plugin modules need to import two functions that are provided by the runtime. (Types and functions are described using WAT syntax.)
|
||||
|
||||
- `(import "typst_env" "wasm_minimal_protocol_write_args_to_buffer" (func (param i32)))` Writes the arguments for the current function into a plugin-allocated buffer. When a plugin function is called, it [receives the lengths](#exports) of its input buffers as arguments. It should then allocate a buffer whose capacity is at least the sum of these lengths. It should then call this function with a `ptr` to the buffer to fill it with the arguments, one after another.
|
||||
- `(import "typst_env" "wasm_minimal_protocol_send_result_to_host" (func (param i32 i32)))` Sends the output of the current function to the host (Typst). The first parameter shall be a pointer to a buffer (`ptr`), while the second is the length of that buffer (`len`). The memory pointed at by `ptr` can be freed immediately after this function returns. If the message should be interpreted as an error message, it should be encoded as UTF-8.
|
||||
|
||||
## Resources
|
||||
|
||||
For more resources, check out the [wasm-minimal-protocol repository](https://github.com/astrale-sharp/wasm-minimal-protocol). It contains:
|
||||
|
||||
- A list of example plugin implementations and a test runner for these examples
|
||||
- Wrappers to help you write your plugin in Rust (Zig wrapper in development)
|
||||
- A stubber for WASI
|
||||
|
||||
```typst
|
||||
#plugin(
|
||||
source
|
||||
) -> module
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- source:
|
||||
- description: A [path](/docs/reference/syntax/#paths) to a WebAssembly file or raw WebAssembly bytes.
|
||||
- type: str | bytes
|
||||
- default: None
|
||||
|
||||
|
||||
## Definitions
|
||||
### plugin.transition
|
||||
|
||||
Calls a plugin function that has side effects and returns a new module with plugin functions that are guaranteed to have observed the results of the mutable call.
|
||||
|
||||
Note that calling an impure function through a normal function call (without use of the transition API) is forbidden and leads to unpredictable behaviour. Read the [section on purity](/docs/reference/foundations/plugin/#purity) for more details.
|
||||
|
||||
In the example below, we load the plugin `hello-mut.wasm` which exports two functions: The `get()` function retrieves a global array as a string. The `add(value)` function adds a value to the global array.
|
||||
|
||||
We call `add` via the transition API. The call `mutated.get()` on the derived module will observe the addition. Meanwhile the original module remains untouched as demonstrated by the `base.get()` call.
|
||||
|
||||
_Note:_ Due to limitations in the internal WebAssembly implementation, the transition API can only guarantee to reflect changes in the plugin's memory, not in WebAssembly globals. If your plugin relies on changes to globals being visible after transition, you might want to avoid use of the transition API for now. We hope to lift this limitation in the future.
|
||||
|
||||
```typst
|
||||
#let base = plugin("hello-mut.wasm")
|
||||
#assert.eq(base.get(), "[]")
|
||||
|
||||
#let mutated = plugin.transition(base.add, "hello")
|
||||
#assert.eq(base.get(), "[]")
|
||||
#assert.eq(mutated.get(), "[hello]")
|
||||
```
|
||||
|
||||
```typst
|
||||
#plugin.transition(
|
||||
func,
|
||||
arguments
|
||||
) -> module
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
- func:
|
||||
- description: The plugin function to call.
|
||||
- type: function
|
||||
- default: None
|
||||
- arguments:
|
||||
- description: The byte buffers to call the function with.
|
||||
- type: bytes
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
# Regex
|
||||
|
||||
A regular expression.
|
||||
|
||||
Can be used as a [show rule selector](/docs/reference/styling/#show-rules) and with [string methods](/docs/reference/foundations/str/) like `find`, `split`, and `replace`.
|
||||
|
||||
[See here](https://docs.rs/regex/latest/regex/#syntax) for a specification of the supported syntax.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
// Works with string methods.
|
||||
#"a,b;c".split(regex("[,;]"))
|
||||
|
||||
// Works with show rules.
|
||||
#show regex("\\d+"): set text(red)
|
||||
|
||||
The numbers 1 to 10.
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## regex
|
||||
|
||||
Create a regular expression from a string.
|
||||
|
||||
```typst
|
||||
#regex(
|
||||
regex
|
||||
) -> regex
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- regex:
|
||||
- description: The regular expression as a string. Both Typst strings and regular expressions use backslashes for escaping. To produce a regex escape sequence that is also valid in Typst, you need to escape the backslash itself (e.g., writing `regex("\\\\\\\\")` for the regex `\\\\`). Regex escape sequences that are not valid Typst escape sequences (e.g., `\\d` and `\\b`) can be entered into strings directly, but it\'s good practice to still escape them to avoid ambiguity (i.e., `regex("\\\\b\\\\d")`). See the [list of valid string escape sequences](/docs/reference/foundations/str/#escapes). If you need many escape sequences, you can also create a raw element and extract its text to use it for your regular expressions: `regex(`\\d+\\.\\d+\\.\\d+`.text)`.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# Representation
|
||||
|
||||
# repr
|
||||
|
||||
Returns the string representation of a value.
|
||||
|
||||
When inserted into content, most values are displayed as this representation in monospace with syntax-highlighting. The exceptions are `none`, integers, floats, strings, content, and functions.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#none vs #repr(none) \
|
||||
#"hello" vs #repr("hello") \
|
||||
#(1, 2) vs #repr((1, 2)) \
|
||||
#[*Hi*] vs #repr([*Hi*])
|
||||
```
|
||||
|
||||
## For debugging purposes only
|
||||
|
||||
This function is for debugging purposes. Its output should not be considered stable and may change at any time.
|
||||
|
||||
To be specific, having the same `repr` does not guarantee that values are equivalent, and `repr` is not a strict inverse of [`eval`](/docs/reference/foundations/eval/). In the following example, for readability, the [`length`](/docs/reference/layout/length/) is rounded to two significant digits and the parameter list and body of the [unnamed `function`](/docs/reference/foundations/function/#unnamed) are omitted.
|
||||
|
||||
```typst
|
||||
#assert(2pt / 3 < 0.67pt)
|
||||
#repr(2pt / 3)
|
||||
|
||||
#repr(x => x + 1)
|
||||
```
|
||||
|
||||
```typst
|
||||
#repr(
|
||||
value
|
||||
) -> str
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- value:
|
||||
- description: The value whose string representation to produce.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
# Selector
|
||||
|
||||
A filter for selecting elements within the document.
|
||||
|
||||
To construct a selector you can:
|
||||
|
||||
- use an [element function](/docs/reference/foundations/function/#element-functions)
|
||||
- filter for an element function with [specific fields](/docs/reference/foundations/function/#definitions-where)
|
||||
- use a [string](/docs/reference/foundations/str/) or [regular expression](/docs/reference/foundations/regex/)
|
||||
- use a [`<label>`](/docs/reference/foundations/label/)
|
||||
- use a [`location`](/docs/reference/introspection/location/)
|
||||
- call the [`selector`](/docs/reference/foundations/selector/) constructor to convert any of the above types into a selector value and use the methods below to refine it
|
||||
|
||||
Selectors are used to [apply styling rules](/docs/reference/styling/#show-rules) to elements. You can also use selectors to [query](/docs/reference/introspection/query/) the document for certain types of elements.
|
||||
|
||||
Furthermore, you can pass a selector to several of Typst's built-in functions to configure their behaviour. One such example is the [outline](/docs/reference/model/outline/) where it can be used to change which elements are listed within the outline.
|
||||
|
||||
Multiple selectors can be combined using the methods shown below. However, not all kinds of selectors are supported in all places, at the moment.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#context query(
|
||||
heading.where(level: 1)
|
||||
.or(heading.where(level: 2))
|
||||
)
|
||||
|
||||
= This will be found
|
||||
== So will this
|
||||
=== But this will not.
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## selector
|
||||
|
||||
Turns a value into a selector. The following values are accepted:
|
||||
|
||||
- An element function like a `heading` or `figure`.
|
||||
- A [string](/docs/reference/foundations/str/) or [regular expression](/docs/reference/foundations/regex/).
|
||||
- A `<label>`.
|
||||
- A [`location`](/docs/reference/introspection/location/).
|
||||
- A more complex selector like `heading.where(level: 1)`.
|
||||
|
||||
```typst
|
||||
#selector(
|
||||
target
|
||||
) -> selector
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- target:
|
||||
- description: Can be an element function like a `heading` or `figure`, a `<label>` or a more complex selector like `heading.where(level: 1)`.
|
||||
- type: str | regex | label | selector | location | function
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## selector.or
|
||||
|
||||
Selects all elements that match this or any of the other selectors.
|
||||
|
||||
```typst
|
||||
#selector.or(
|
||||
others
|
||||
) -> selector
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- others:
|
||||
- description: The other selectors to match on.
|
||||
- type: str | regex | label | selector | location | function
|
||||
- default: None
|
||||
|
||||
## selector.and
|
||||
|
||||
Selects all elements that match this and all of the other selectors.
|
||||
|
||||
```typst
|
||||
#selector.and(
|
||||
others
|
||||
) -> selector
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- others:
|
||||
- description: The other selectors to match on.
|
||||
- type: str | regex | label | selector | location | function
|
||||
- default: None
|
||||
|
||||
## selector.before
|
||||
|
||||
Returns a modified selector that will only match elements that occur before the first match of `end`.
|
||||
|
||||
```typst
|
||||
#selector.before(
|
||||
end,
|
||||
inclusive: bool
|
||||
) -> selector
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- end:
|
||||
- description: The original selection will end at the first match of `end`.
|
||||
- type: label | selector | location | function
|
||||
- default: None
|
||||
- inclusive:
|
||||
- description: Whether `end` itself should match or not. This is only relevant if both selectors match the same type of element. Defaults to `true`.
|
||||
- type: bool
|
||||
- default: true
|
||||
|
||||
## selector.after
|
||||
|
||||
Returns a modified selector that will only match elements that occur after the first match of `start`.
|
||||
|
||||
```typst
|
||||
#selector.after(
|
||||
start,
|
||||
inclusive: bool
|
||||
) -> selector
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- start:
|
||||
- description: The original selection will start at the first match of `start`.
|
||||
- type: label | selector | location | function
|
||||
- default: None
|
||||
- inclusive:
|
||||
- description: Whether `start` itself should match or not. This is only relevant if both selectors match the same type of element. Defaults to `true`.
|
||||
- type: bool
|
||||
- default: true
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Standard Library
|
||||
|
||||
A module that contains all globally accessible items.
|
||||
|
||||
## Using "shadowed" definitions
|
||||
|
||||
The `std` module is useful whenever you overrode a name from the global scope (this is called _shadowing_). For instance, you might have used the name `text` for a parameter. To still access the `text` element, write `std.text`.
|
||||
|
||||
```typst
|
||||
#let par = [My special paragraph.]
|
||||
#let special(text) = {
|
||||
set std.text(style: "italic")
|
||||
set std.par.line(numbering: "1")
|
||||
text
|
||||
}
|
||||
|
||||
#special(par)
|
||||
|
||||
#lorem(10)
|
||||
```
|
||||
|
||||
## Conditional access
|
||||
|
||||
You can also use this in combination with the [dictionary constructor](/docs/reference/foundations/dictionary/) to conditionally access global definitions. This can, for instance, be useful to use new or experimental functionality when it is available, while falling back to an alternative implementation if used on an older Typst version. In particular, this allows us to create [polyfills](https://en.wikipedia.org/wiki/Polyfill_(programming)).
|
||||
|
||||
This can be as simple as creating an alias to prevent warning messages, for example, conditionally using `pattern` in Typst version 0.12, but using [`tiling`](/docs/reference/visualize/tiling/) in newer versions. Since the parameters accepted by the `tiling` function match those of the older `pattern` function, using the `tiling` function when available and falling back to `pattern` otherwise will unify the usage across all versions. Note that, when creating a polyfill, [`sys.version`](/docs/reference/foundations/sys/) can also be very useful.
|
||||
|
||||
```typst
|
||||
#let tiling = if "tiling" in std { tiling } else { pattern }
|
||||
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,465 @@
|
||||
# String
|
||||
|
||||
A sequence of Unicode codepoints.
|
||||
|
||||
You can iterate over the grapheme clusters of the string using a [for loop](/docs/reference/scripting/#loops). Grapheme clusters are basically characters but keep together things that belong together, e.g. multiple codepoints that together form a flag emoji. Strings can be added with the `+` operator, [joined together](/docs/reference/scripting/#blocks) and multiplied with integers.
|
||||
|
||||
Typst provides utility methods for string manipulation. Many of these methods (e.g., [`split`](/docs/reference/foundations/str/#definitions-split), [`trim`](/docs/reference/foundations/str/#definitions-trim) and [`replace`](/docs/reference/foundations/str/#definitions-replace)) operate on _patterns:_ A pattern can be either a string or a [regular expression](/docs/reference/foundations/regex/). This makes the methods quite versatile.
|
||||
|
||||
All lengths and indices are expressed in terms of UTF-8 bytes. Indices are zero-based and negative indices wrap around to the end of the string.
|
||||
|
||||
You can convert a value to a string with this type's constructor.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#"hello world!" \
|
||||
#"\"hello\n world\"!" \
|
||||
#"1 2 3".split() \
|
||||
#"1,2;3".split(regex("[,;]")) \
|
||||
#(regex("\\d+") in "ten euros") \
|
||||
#(regex("\\d+") in "10 euros")
|
||||
```
|
||||
|
||||
## Escape sequences
|
||||
|
||||
Just like in markup, you can escape a few symbols in strings:
|
||||
|
||||
- `\\` for a backslash
|
||||
- `\"` for a quote
|
||||
- `\n` for a newline
|
||||
- `\r` for a carriage return
|
||||
- `\t` for a tab
|
||||
- `\u\{1f600\}` for a hexadecimal Unicode escape sequence
|
||||
|
||||
## Constructor
|
||||
## str
|
||||
|
||||
Converts a value to a string.
|
||||
|
||||
- Integers are formatted in base 10. This can be overridden with the optional `base` parameter.
|
||||
- Floats are formatted in base 10 and never in exponential notation.
|
||||
- Negative integers and floats are formatted with the Unicode minus sign ("−" U+2212) instead of the ASCII minus sign ("-" U+002D).
|
||||
- From labels the name is extracted.
|
||||
- Bytes are decoded as UTF-8.
|
||||
|
||||
If you wish to convert from and to Unicode code points, see the [`to-unicode`](/docs/reference/foundations/str/#definitions-to-unicode) and [`from-unicode`](/docs/reference/foundations/str/#definitions-from-unicode) functions.
|
||||
|
||||
```typst
|
||||
#str(10) \
|
||||
#str(4000, base: 16) \
|
||||
#str(2.7) \
|
||||
#str(1e8) \
|
||||
#str(<intro>)
|
||||
```
|
||||
|
||||
```typst
|
||||
#str(
|
||||
value,
|
||||
base: int
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value that should be converted to a string.
|
||||
- type: int | float | str | bytes | label | decimal | version | type
|
||||
- default: None
|
||||
- base:
|
||||
- description: The base (radix) to display integers in, between 2 and 36.
|
||||
- type: int
|
||||
- default: 10
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## str.len
|
||||
|
||||
The length of the string in UTF-8 encoded bytes.
|
||||
|
||||
## str.first
|
||||
|
||||
Extracts the first grapheme cluster of the string.
|
||||
|
||||
Returns the provided default value if the string is empty or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#str.first(
|
||||
default: str
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- default:
|
||||
- description: A default value to return if the string is empty.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
## str.last
|
||||
|
||||
Extracts the last grapheme cluster of the string.
|
||||
|
||||
Returns the provided default value if the string is empty or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#str.last(
|
||||
default: str
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- default:
|
||||
- description: A default value to return if the string is empty.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
## str.at
|
||||
|
||||
Extracts the first grapheme cluster after the specified index. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.
|
||||
|
||||
```typst
|
||||
#str.at(
|
||||
index,
|
||||
default: any
|
||||
) -> any
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- index:
|
||||
- description: The byte index. If negative, indexes from the back.
|
||||
- type: int
|
||||
- default: None
|
||||
- default:
|
||||
- description: A default value to return if the index is out of bounds.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
## str.slice
|
||||
|
||||
Extracts a substring of the string. Fails with an error if the start or end index is out of bounds.
|
||||
|
||||
```typst
|
||||
#str.slice(
|
||||
start,
|
||||
end,
|
||||
count: int
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- start:
|
||||
- description: The start byte index (inclusive). If negative, indexes from the back.
|
||||
- type: int
|
||||
- default: None
|
||||
- end:
|
||||
- description: The end byte index (exclusive). If omitted, the whole slice until the end of the string is extracted. If negative, indexes from the back.
|
||||
- type: none | int
|
||||
- default: none
|
||||
- count:
|
||||
- description: The number of bytes to extract. This is equivalent to passing `start + count` as the `end` position. Mutually exclusive with `end`.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## str.clusters
|
||||
|
||||
Returns the grapheme clusters of the string as an array of substrings.
|
||||
|
||||
## str.codepoints
|
||||
|
||||
Returns the Unicode codepoints of the string as an array of substrings.
|
||||
|
||||
## str.to-unicode
|
||||
|
||||
Converts a character into its corresponding code point.
|
||||
|
||||
```typst
|
||||
#"a".to-unicode() \
|
||||
#("a\u{0300}"
|
||||
.codepoints()
|
||||
.map(str.to-unicode))
|
||||
```
|
||||
|
||||
```typst
|
||||
#str.to-unicode(
|
||||
character
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- character:
|
||||
- description: The character that should be converted.
|
||||
- type: str
|
||||
- default: None
|
||||
|
||||
## str.from-unicode
|
||||
|
||||
Converts a unicode code point into its corresponding string.
|
||||
|
||||
```typst
|
||||
#str.from-unicode(97)
|
||||
```
|
||||
|
||||
```typst
|
||||
#str.from-unicode(
|
||||
value
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The code point that should be converted.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## str.normalize
|
||||
|
||||
Normalizes the string to the given Unicode normal form.
|
||||
|
||||
This is useful when manipulating strings containing Unicode combining characters.
|
||||
|
||||
```typst
|
||||
#assert.eq("é".normalize(form: "nfd"), "e\u{0301}")
|
||||
#assert.eq("ſ́".normalize(form: "nfkc"), "ś")
|
||||
```
|
||||
|
||||
```typst
|
||||
#str.normalize(
|
||||
form: str
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- form:
|
||||
- description:
|
||||
- type: str
|
||||
- default: "nfc"
|
||||
|
||||
## str.contains
|
||||
|
||||
Whether the string contains the specified pattern.
|
||||
|
||||
This method also has dedicated syntax: You can write `"bc" in "abcd"` instead of `"abcd".contains("bc")`.
|
||||
|
||||
```typst
|
||||
#str.contains(
|
||||
pattern
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.starts-with
|
||||
|
||||
Whether the string starts with the specified pattern.
|
||||
|
||||
```typst
|
||||
#str.starts-with(
|
||||
pattern
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern the string might start with.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.ends-with
|
||||
|
||||
Whether the string ends with the specified pattern.
|
||||
|
||||
```typst
|
||||
#str.ends-with(
|
||||
pattern
|
||||
) -> bool
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern the string might end with.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.find
|
||||
|
||||
Searches for the specified pattern in the string and returns the first match as a string or `none` if there is no match.
|
||||
|
||||
```typst
|
||||
#str.find(
|
||||
pattern
|
||||
) -> none str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.position
|
||||
|
||||
Searches for the specified pattern in the string and returns the index of the first match as an integer or `none` if there is no match.
|
||||
|
||||
```typst
|
||||
#str.position(
|
||||
pattern
|
||||
) -> none int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.match
|
||||
|
||||
Searches for the specified pattern in the string and returns a dictionary with details about the first match or `none` if there is no match.
|
||||
|
||||
The returned dictionary has the following keys:
|
||||
|
||||
- `start`: The start offset of the match
|
||||
- `end`: The end offset of the match
|
||||
- `text`: The text that matched.
|
||||
- `captures`: An array containing a string for each matched capturing group. The first item of the array contains the first matched capturing, not the whole match! This is empty unless the `pattern` was a regex with capturing groups.
|
||||
|
||||
```typst
|
||||
#let pat = regex("not (a|an) (apple|cat)")
|
||||
#"I'm a doctor, not an apple.".match(pat) \
|
||||
#"I am not a cat!".match(pat)
|
||||
```
|
||||
|
||||
```typst
|
||||
#assert.eq("Is there a".match("for this?"), none)
|
||||
#"The time of my life.".match(regex("[mit]+e"))
|
||||
```
|
||||
|
||||
```typst
|
||||
#str.match(
|
||||
pattern
|
||||
) -> none dictionary
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.matches
|
||||
|
||||
Searches for the specified pattern in the string and returns an array of dictionaries with details about all matches. For details about the returned dictionaries, see [above](/docs/reference/foundations/str/#definitions-match).
|
||||
|
||||
```typst
|
||||
#"Day by Day.".matches("Day")
|
||||
```
|
||||
|
||||
```typst
|
||||
#str.matches(
|
||||
pattern
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
|
||||
## str.replace
|
||||
|
||||
Replace at most `count` occurrences of the given pattern with a replacement string or function (beginning from the start). If no count is given, all occurrences are replaced.
|
||||
|
||||
```typst
|
||||
#str.replace(
|
||||
pattern,
|
||||
replacement,
|
||||
count: int
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for.
|
||||
- type: str | regex
|
||||
- default: None
|
||||
- replacement:
|
||||
- description: The string to replace the matches with or a function that gets a dictionary for each match and can return individual replacement strings. The dictionary passed to the function has the same shape as the dictionary returned by [`match`](/docs/reference/foundations/str/#definitions-match).
|
||||
- type: str | function
|
||||
- default: None
|
||||
- count:
|
||||
- description: If given, only the first `count` matches of the pattern are placed.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
## str.trim
|
||||
|
||||
Removes matches of a pattern from one or both sides of the string, once or repeatedly and returns the resulting string.
|
||||
|
||||
```typst
|
||||
#str.trim(
|
||||
pattern,
|
||||
at: alignment,
|
||||
repeat: bool
|
||||
) -> str
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to search for. If `none`, trims white spaces.
|
||||
- type: none | str | regex
|
||||
- default: none
|
||||
- at:
|
||||
- description: Can be `start` or `end` to only trim the start or end of the string. If omitted, both sides are trimmed.
|
||||
- type: alignment
|
||||
- default: None
|
||||
- repeat:
|
||||
- description: Whether to repeatedly removes matches of the pattern or just once. Defaults to `true`.
|
||||
- type: bool
|
||||
- default: true
|
||||
|
||||
## str.split
|
||||
|
||||
Splits a string at matches of a specified pattern and returns an array of the resulting parts.
|
||||
|
||||
When the empty string is used as a separator, it separates every character (i.e., Unicode code point) in the string, along with the beginning and end of the string. In practice, this means that the resulting list of parts will contain the empty string at the start and end of the list.
|
||||
|
||||
```typst
|
||||
#str.split(
|
||||
pattern
|
||||
) -> array
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- pattern:
|
||||
- description: The pattern to split at. Defaults to whitespace.
|
||||
- type: none | str | regex
|
||||
- default: none
|
||||
|
||||
## str.rev
|
||||
|
||||
Reverse the string.
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# Symbol
|
||||
|
||||
A Unicode symbol.
|
||||
|
||||
Typst defines common symbols so that they can easily be written with standard keyboards. The symbols are defined in modules, from which they can be accessed using [field access notation](/docs/reference/scripting/#fields):
|
||||
|
||||
- General symbols are defined in the [`sym` module](/docs/reference/symbols/sym/) and are accessible without the `sym.` prefix in math mode.
|
||||
- Emoji are defined in the [`emoji` module](/docs/reference/symbols/emoji/)
|
||||
|
||||
Moreover, you can define custom symbols with this type's constructor function.
|
||||
|
||||
```typst
|
||||
#sym.arrow.r \
|
||||
#sym.gt.eq.not \
|
||||
$gt.eq.not$ \
|
||||
#emoji.face.halo
|
||||
```
|
||||
|
||||
Many symbols have different variants, which can be selected by appending the modifiers with dot notation. The order of the modifiers is not relevant. Visit the documentation pages of the symbol modules and click on a symbol to see its available variants.
|
||||
|
||||
```typst
|
||||
$arrow.l$ \
|
||||
$arrow.r$ \
|
||||
$arrow.t.quad$
|
||||
```
|
||||
|
||||
## Constructor
|
||||
## symbol
|
||||
|
||||
Create a custom symbol with modifiers.
|
||||
|
||||
```typst
|
||||
#let envelope = symbol(
|
||||
"🖂",
|
||||
("stamped", "🖃"),
|
||||
("stamped.pen", "🖆"),
|
||||
("lightning", "🖄"),
|
||||
("fly", "🖅"),
|
||||
)
|
||||
|
||||
#envelope
|
||||
#envelope.stamped
|
||||
#envelope.stamped.pen
|
||||
#envelope.lightning
|
||||
#envelope.fly
|
||||
```
|
||||
|
||||
```typst
|
||||
#symbol(
|
||||
variants
|
||||
) -> symbol
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- variants:
|
||||
- description: The variants of the symbol. Can be a just a string consisting of a single character for the modifierless variant or an array with two strings specifying the modifiers and the symbol. Individual modifiers should be separated by dots. When displaying a symbol, Typst selects the first from the variants that have all attached modifiers and the minimum number of other modifiers.
|
||||
- type: str | array
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
# System
|
||||
|
||||
Module for system interactions.
|
||||
|
||||
This module defines the following items:
|
||||
|
||||
- The `sys.version` constant (of type [`version`](/docs/reference/foundations/version/)) that specifies the currently active Typst compiler version.
|
||||
- The `sys.inputs` [dictionary](/docs/reference/foundations/dictionary/), which makes external inputs available to the project. An input specified in the command line as `--input key=value` becomes available under `sys.inputs.key` as `"value"`. To include spaces in the value, it may be enclosed with single or double quotes. The value is always of type [string](/docs/reference/foundations/str/). More complex data may be parsed manually using functions like [`json`](/docs/reference/data-loading/json/).
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# Target
|
||||
|
||||
# target
|
||||
|
||||
Returns the current export target.
|
||||
|
||||
This function returns either
|
||||
|
||||
- `"paged"` (for PDF, PNG, and SVG export), or
|
||||
- `"html"` (for HTML export).
|
||||
|
||||
The design of this function is not yet finalized and for this reason it is guarded behind the `html` feature. Visit the [HTML documentation page](/docs/reference/html/) for more details.
|
||||
|
||||
## When to use it
|
||||
|
||||
This function allows you to format your document properly across both HTML and paged export targets. It should primarily be used in templates and show rules, rather than directly in content. This way, the document's contents can be fully agnostic to the export target and content can be shared between PDF and HTML export.
|
||||
|
||||
## Varying targets
|
||||
|
||||
This function is [contextual](/docs/reference/context/) as the target can vary within a single compilation: When exporting to HTML, the target will be `"paged"` while within an [`html.frame`](/docs/reference/html/frame/).
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let kbd(it) = context {
|
||||
if target() == "html" {
|
||||
html.elem("kbd", it)
|
||||
} else {
|
||||
set text(fill: rgb("#1f2328"))
|
||||
let r = 3pt
|
||||
box(
|
||||
fill: rgb("#f6f8fa"),
|
||||
stroke: rgb("#d1d9e0b3"),
|
||||
outset: (y: r),
|
||||
inset: (x: r),
|
||||
radius: r,
|
||||
raw(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Press #kbd("F1") for help.
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
# Type
|
||||
|
||||
Describes a kind of value.
|
||||
|
||||
To style your document, you need to work with values of different kinds: Lengths specifying the size of your elements, colors for your text and shapes, and more. Typst categorizes these into clearly defined _types_ and tells you where it expects which type of value.
|
||||
|
||||
Apart from basic types for numeric values and [typical](/docs/reference/foundations/int/) [types](/docs/reference/foundations/float/) [known](/docs/reference/foundations/str/) [from](/docs/reference/foundations/array/) [programming](/docs/reference/foundations/dictionary/) languages, Typst provides a special type for [_content._](/docs/reference/foundations/content/) A value of this type can hold anything that you can enter into your document: Text, elements like headings and shapes, and style information.
|
||||
|
||||
## Example
|
||||
|
||||
```typst
|
||||
#let x = 10
|
||||
#if type(x) == int [
|
||||
#x is an integer!
|
||||
] else [
|
||||
#x is another value...
|
||||
]
|
||||
|
||||
An image is of type
|
||||
#type(image("glacier.jpg")).
|
||||
```
|
||||
|
||||
The type of `10` is `int`. Now, what is the type of `int` or even `type`?
|
||||
|
||||
```typst
|
||||
#type(int) \
|
||||
#type(type)
|
||||
```
|
||||
|
||||
Unlike other types like `int`, [none](/docs/reference/foundations/none/) and [auto](/docs/reference/foundations/auto/) do not have a name representing them. To test if a value is one of these, compare your value to them directly, e.g:
|
||||
|
||||
```typst
|
||||
#let val = none
|
||||
#if val == none [
|
||||
Yep, it's none.
|
||||
]
|
||||
```
|
||||
|
||||
Note that `type` will return [`content`](/docs/reference/foundations/content/) for all document elements. To programmatically determine which kind of content you are dealing with, see [`content.func`](/docs/reference/foundations/content/#definitions-func).
|
||||
|
||||
## Constructor
|
||||
## type
|
||||
|
||||
Determines a value's type.
|
||||
|
||||
```typst
|
||||
#type(12) \
|
||||
#type(14.7) \
|
||||
#type("hello") \
|
||||
#type(<glacier>) \
|
||||
#type([Hi]) \
|
||||
#type(x => x + 1) \
|
||||
#type(type)
|
||||
```
|
||||
|
||||
```typst
|
||||
#type(
|
||||
value
|
||||
) -> type
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- value:
|
||||
- description: The value whose type\'s to determine.
|
||||
- type: any
|
||||
- default: None
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# Version
|
||||
|
||||
A version with an arbitrary number of components.
|
||||
|
||||
The first three components have names that can be used as fields: `major`, `minor`, `patch`. All following components do not have names.
|
||||
|
||||
The list of components is semantically extended by an infinite list of zeros. This means that, for example, `0.8` is the same as `0.8.0`. As a special case, the empty version (that has no components at all) is the same as `0`, `0.0`, `0.0.0`, and so on.
|
||||
|
||||
The current version of the Typst compiler is available as `sys.version`.
|
||||
|
||||
You can convert a version to an array of explicitly given components using the [`array`](/docs/reference/foundations/array/) constructor.
|
||||
|
||||
## Constructor
|
||||
## version
|
||||
|
||||
Creates a new version.
|
||||
|
||||
It can have any number of components (even zero).
|
||||
|
||||
```typst
|
||||
#version() \
|
||||
#version(1) \
|
||||
#version(1, 2, 3, 4) \
|
||||
#version((1, 2, 3, 4)) \
|
||||
#version((1, 2), 3)
|
||||
```
|
||||
|
||||
As a practical use case, this allows comparing the current version ([`sys.version`](/docs/reference/foundations/version/)) to a specific one.
|
||||
|
||||
```typst
|
||||
Current version: #sys.version \
|
||||
#(sys.version >= version(0, 14, 0)) \
|
||||
#(version(3, 2, 0) > version(4, 1, 0))
|
||||
```
|
||||
|
||||
```typst
|
||||
#version(
|
||||
components
|
||||
) -> version
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- components:
|
||||
- description: The components of the version (array arguments are flattened)
|
||||
- type: int | array
|
||||
- default: None
|
||||
|
||||
|
||||
## Methods
|
||||
|
||||
## version.at
|
||||
|
||||
Retrieves a component of a version.
|
||||
|
||||
The returned integer is always non-negative. Returns `0` if the version isn't specified to the necessary length.
|
||||
|
||||
```typst
|
||||
#version.at(
|
||||
index
|
||||
) -> int
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- index:
|
||||
- description: The index at which to retrieve the component. If negative, indexes from the back of the explicitly given components.
|
||||
- type: int
|
||||
- default: None
|
||||
|
||||
|
||||
Reference in new issue
Block a user