Skip to content

Commit 6dcb65e

Browse files
committed
Improve documentation
1 parent db24996 commit 6dcb65e

File tree

5 files changed

+90
-63
lines changed

5 files changed

+90
-63
lines changed

docs/src/reference/scripting.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ It explains #name.
8181
Sum is #add(2, 3).
8282
```
8383

84-
Let bindings can be used to destructure arrays and dictionaries.
84+
Let bindings can also be used to destructure [arrays]($type/array) and
85+
[dictionaries]($type/dictionary).
8586

8687
```example
8788
#let (x, y) = (1, 2)
@@ -92,10 +93,11 @@ The first element is #a.
9293
The last element is #b.
9394
9495
#let books = (
95-
"Shakespeare": "Hamlet",
96-
"Homer": "The Odyssey",
97-
"Austen": "Persuasion",
96+
Shakespeare: "Hamlet",
97+
Homer: "The Odyssey",
98+
Austen: "Persuasion",
9899
)
100+
99101
#let (Austen,) = books
100102
Austen wrote #Austen.
101103
@@ -104,14 +106,13 @@ Homer wrote #h.
104106
105107
#let (Homer, ..other) = books
106108
#for (author, title) in other [
107-
#author wrote #title,
109+
#author wrote #title.
108110
]
109111
```
110112

111-
Note that the underscore `_` is the only identifier that can
112-
be used multiple times in the same assignment.
113+
You can use the underscore to discard elements in a destructuring pattern:
113114

114-
```
115+
```example
115116
#let (_, y, _) = (1, 2, 3)
116117
The y coordinate is #y.
117118
```

docs/src/reference/types.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,6 @@ Fails with an error if the index is out of bounds.
526526
The index at which to retrieve the item.
527527
- returns: any
528528

529-
### enumerate()
530-
Returns an array of the values along with their indices.
531-
532-
- returns: array
533-
534529
### push()
535530
Add a value to the end of the array.
536531

@@ -616,6 +611,15 @@ transformed with the given function.
616611
The function to apply to each item.
617612
- returns: array
618613

614+
### enumerate()
615+
Returns a new array with the values alongside their indices.
616+
617+
The returned array consists of `(index, value)` pairs in the form of length-2
618+
arrays. These can be [destructured]($scripting/#bindings) with a let binding or
619+
for loop.
620+
621+
- returns: array
622+
619623
### fold()
620624
Folds all items into a single value using an accumulator function.
621625

library/src/meta/context.rs

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ impl Show for LocateElem {
8989
/// In the future, the provided styles might also be directly accessed to look
9090
/// up styles defined by [set rules]($styling/#set-rules).
9191
///
92+
/// ```example
93+
/// #let thing(body) = style(styles => {
94+
/// let size = measure(body, styles)
95+
/// [Width of "#body" is #size.width]
96+
/// })
97+
///
98+
/// #thing[Hey] \
99+
/// #thing[Welcome]
100+
/// ```
101+
///
92102
/// Display: Style
93103
/// Category: meta
94104
/// Returns: content
@@ -122,67 +132,61 @@ impl Show for StyleElem {
122132
}
123133
}
124134

125-
/// Provides access to the current outer container's (or page's, if none) size (width and height).
126-
///
127-
/// The given function must accept a single parameter, `size`, which is a dictionary with keys
128-
/// `width` and `height`, both having the type [`length`]($type/length).
129-
///
130-
/// That is, if this `layout` call is done inside (for example) a box of size 800pt (width)
131-
/// by 400pt (height), then the specified function will be given the parameter
132-
/// `(width: 800pt, height: 400pt)`.
133-
///
134-
/// If, however, this `layout` call is placed directly on the page, not inside any container,
135-
/// then the function will be given `(width: page_width, height: page_height)`, where `page_width`
136-
/// and `page_height` correspond to the current page's respective dimensions, minus its margins.
137-
///
138-
/// This is useful, for example, to convert a [`ratio`]($type/ratio) value (such as `5%`, `100%`
139-
/// etc.), which are usually based upon the outer container's dimensions (precisely what this
140-
/// function gives), to a fixed length (in `pt`).
141-
///
142-
/// This is also useful if you're trying to make content fit a certain box, and doing certain
143-
/// arithmetic using `pt` (for example, comparing different lengths) is required.
135+
/// Provides access to the current outer container's (or page's, if none) size
136+
/// (width and height).
144137
///
145-
/// Please note: This function may provide a width or height of `infpt` if one of the page
146-
/// dimensions is `auto`, under certain circumstances. This should not normally occur for
147-
/// usual page sizes, however.
138+
/// The given function must accept a single parameter, `size`, which is a
139+
/// dictionary with keys `width` and `height`, both of type
140+
/// [`length`]($type/length).
148141
///
142+
149143
/// ```example
150-
/// layout(size => {
151-
/// // work with the width and height of the container we're in
152-
/// // using size.width and size.height
153-
/// })
144+
/// #let text = lorem(30)
145+
/// #layout(size => style(styles => [
146+
/// #let (height,) = measure(
147+
/// block(width: size.width, text),
148+
/// styles,
149+
/// )
150+
/// This text is #height high with
151+
/// the current page width: \
152+
/// #text
153+
/// ]))
154+
/// ```
154155
///
155-
/// layout(size => {
156-
/// // convert 49% (of page width) to 'pt'
157-
/// // note that "ratio" values are always relative to a certain, possibly arbitrary length,
158-
/// // but it's usually the current container's width or height (e.g., for table columns,
159-
/// // 15% would be relative to the width, but, for rows, it would be relative to the height).
160-
/// let percentage_of_width = (49% / 1%) * 0.01 * size.width
161-
/// // ... use the converted value ...
162-
/// })
156+
/// If the `layout` call is placed inside of a box width a width of `{800pt}`
157+
/// and a height of `{400pt}`, then the specified function will be given the
158+
/// parameter `{(width: 800pt, height: 400pt)}`. If it placed directly into the
159+
/// page it receives the page's dimensions minus its margins. This is mostly
160+
/// useful in combination with [measurement]($func/measure).
163161
///
164-
/// // The following two boxes are equivalent, and will have rectangles sized 200pt and 40pt:
162+
/// You can also use this function to resolve [`ratio`]($type/ratio) to fixed
163+
/// lengths. This might come in handy if you're building your own layout
164+
/// abstractions.
165165
///
166-
/// #box(width: 200pt, height: 40pt, {
167-
/// rect(width: 100%, height: 100%)
166+
/// ```example
167+
/// #layout(size => {
168+
/// let half = 50% * size.width
169+
/// [Half a page is #half wide.]
168170
/// })
169-
///
170-
/// #box(width: 200pt, height: 40pt, layout(size => {
171-
/// rect(width: size.width, height: size.height)
172-
/// }))
173171
/// ```
174172
///
173+
/// Note that this function will provide an infinite width or height if one of
174+
/// the page width or height is `auto`, respectively.
175+
///
175176
/// Display: Layout
176177
/// Category: meta
177178
/// Returns: content
178179
#[func]
179180
pub fn layout(
180-
/// A function to call with the outer container's size. Its return value is displayed
181-
/// in the document.
181+
/// A function to call with the outer container's size. Its return value is
182+
/// displayed in the document.
183+
///
184+
/// The container's size is given as a [dictionary]($type/dictionary) with
185+
/// the keys `width` and `height`.
182186
///
183187
/// This function is called once for each time the content returned by
184188
/// `layout` appears in the document. That makes it possible to generate
185-
/// content that depends on the size of the container it is inside.
189+
/// content that depends on the size of the container it is inside of.
186190
func: Func,
187191
) -> Value {
188192
LayoutElem::new(func).pack().into()

library/src/meta/document.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@ use crate::prelude::*;
33

44
/// The root element of a document and its metadata.
55
///
6-
/// All documents are automatically wrapped in a `document` element. The main
7-
/// use of this element is to use it in `set` rules to specify document
8-
/// metadata.
6+
/// All documents are automatically wrapped in a `document` element. You cannot
7+
/// create a document element yourself. This function is only used with
8+
/// [set rules]($styling/#set-rules) to specify document metadata. Such a set
9+
/// rule must appear before any of the document's contents.
910
///
10-
/// The metadata set with this function is not rendered within the document.
11-
/// Instead, it is embedded in the compiled PDF file.
11+
/// ```example
12+
/// #set document(title: "Hello")
13+
///
14+
/// This has no visible output, but
15+
/// embeds metadata into the PDF!
16+
/// ```
17+
///
18+
/// Note that metadata set with this function is not rendered within the
19+
/// document. Instead, it is embedded in the compiled PDF file.
1220
///
1321
/// Display: Document
1422
/// Category: meta
15-
#[element(LayoutRoot)]
23+
#[element(Construct, LayoutRoot)]
1624
pub struct DocumentElem {
1725
/// The document's title. This is often rendered as the title of the
1826
/// PDF viewer window.
@@ -27,6 +35,12 @@ pub struct DocumentElem {
2735
pub children: Vec<Content>,
2836
}
2937

38+
impl Construct for DocumentElem {
39+
fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> {
40+
bail!(args.span, "can only be used in set rules")
41+
}
42+
}
43+
3044
impl LayoutRoot for DocumentElem {
3145
/// Layout the document into a sequence of frames, one per page.
3246
fn layout_root(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Document> {

tests/typ/meta/document.typ

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Hello
2222
// Error: 2-30 document set rules must appear before any content
2323
#set document(title: "Hello")
2424

25+
---
26+
// Error: 10-12 can only be used in set rules
27+
#document()
28+
2529
---
2630
#box[
2731
// Error: 4-32 document set rules are not allowed inside of containers

0 commit comments

Comments
 (0)