mirror of
https://github.com/typst/typst
synced 2025-05-19 11:35:27 +08:00
Fix header query example (#3903)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
parent
0523f22d83
commit
4c8a8f122a
@ -13,58 +13,61 @@ use crate::introspection::Location;
|
|||||||
///
|
///
|
||||||
|
|
||||||
/// # Finding elements
|
/// # Finding elements
|
||||||
/// In the example below, we create a custom page header that displays the text
|
/// In the example below, we manually create a table of contents instead of
|
||||||
/// "Typst Academy" in small capitals and the current section title. On the
|
/// using the [`outline`] function.
|
||||||
/// first page, the section title is omitted because the header is before the
|
|
||||||
/// first section heading.
|
|
||||||
///
|
///
|
||||||
/// To realize this layout, we open a `context` and then query for all headings
|
/// To do this, we first query for all headings in the document at level 1 and
|
||||||
/// after the [current location]($here). The code within the context block
|
/// where `outlined` is true. Querying only for headings at level 1 ensures
|
||||||
/// runs twice: Once per page.
|
/// that, for the purpose of this example, sub-headings are not included in the
|
||||||
|
/// table of contents. The `outlined` field is used to exclude the "Table of
|
||||||
|
/// Contents" heading itself.
|
||||||
///
|
///
|
||||||
/// - On the first page the query for all headings before the current location
|
/// Note that we open a `context` to be able to use the `query` function.
|
||||||
/// yields an empty array: There are no previous headings. We check for this
|
|
||||||
/// case and just display "Typst Academy".
|
|
||||||
///
|
|
||||||
/// - For the second page, we retrieve the last element from the query's result.
|
|
||||||
/// This is the latest heading before the current position and as such, it is
|
|
||||||
/// the heading of the section we are currently in. We access its content
|
|
||||||
/// through the `body` field and display it alongside "Typst Academy".
|
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
/// >>> #set page(
|
/// >>> #set page(
|
||||||
/// >>> width: 240pt,
|
/// >>> width: 240pt,
|
||||||
/// >>> height: 180pt,
|
/// >>> height: 180pt,
|
||||||
/// >>> margin: (top: 35pt, rest: 15pt),
|
/// >>> margin: (top: 20pt, bottom: 35pt)
|
||||||
/// >>> header-ascent: 12pt,
|
|
||||||
/// >>> )
|
/// >>> )
|
||||||
/// #set page(header: context {
|
/// #set page(numbering: "1")
|
||||||
/// let elems = query(
|
///
|
||||||
/// selector(heading).before(here()),
|
/// #heading(outlined: false)[
|
||||||
|
/// Table of Contents
|
||||||
|
/// ]
|
||||||
|
/// #context {
|
||||||
|
/// let chapters = query(
|
||||||
|
/// heading.where(
|
||||||
|
/// level: 1,
|
||||||
|
/// outlined: true,
|
||||||
|
/// )
|
||||||
/// )
|
/// )
|
||||||
/// let academy = smallcaps[
|
/// for chapter in chapters {
|
||||||
/// Typst Academy
|
/// let loc = chapter.location()
|
||||||
/// ]
|
/// let nr = numbering(
|
||||||
/// if elems.len() == 0 {
|
/// loc.page-numbering(),
|
||||||
/// align(right, academy)
|
/// ..counter(page).at(loc),
|
||||||
/// } else {
|
/// )
|
||||||
/// let body = elems.last().body
|
/// [#chapter.body #h(1fr) #nr \ ]
|
||||||
/// academy + h(1fr) + emph(body)
|
|
||||||
/// }
|
/// }
|
||||||
/// })
|
/// }
|
||||||
///
|
///
|
||||||
/// = Introduction
|
/// = Introduction
|
||||||
/// #lorem(23)
|
/// #lorem(10)
|
||||||
|
/// #pagebreak()
|
||||||
///
|
///
|
||||||
/// = Background
|
/// == Sub-Heading
|
||||||
/// #lorem(30)
|
/// #lorem(8)
|
||||||
///
|
///
|
||||||
/// = Analysis
|
/// = Discussion
|
||||||
/// #lorem(15)
|
/// #lorem(18)
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// You can get the location of the elements returned by `query` with
|
/// To get the page numbers, we first get the location of the elements returned
|
||||||
/// [`location`]($content.location).
|
/// by `query` with [`location`]($content.location). We then also retrieve the
|
||||||
|
/// [page numbering]($location.page-numbering) and [page
|
||||||
|
/// counter]($counter/#page-counter) at that location and apply the numbering to
|
||||||
|
/// the counter.
|
||||||
///
|
///
|
||||||
/// # A word of caution { #caution }
|
/// # A word of caution { #caution }
|
||||||
/// To resolve all your queries, Typst evaluates and layouts parts of the
|
/// To resolve all your queries, Typst evaluates and layouts parts of the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user