TYPST:基于状态

问题描述 投票:0回答:2
show heading

创建一个新标题时更新标头的函数。 show heading: it => { set text(blue.darken(40%)) set page(header: grid(columns: (1fr, 3fr, 1fr), align(left)[LeftHead], align(center, title), align(right)[Right Head] )) it } 但是,我有以下错误:

error: page configuration is not allowed inside of containers ┌─ my_definitions.typ:81:4 │ 81 │ ╭ set page(header: grid(columns: (1fr, 3fr, 1fr), 82 │ │ align(left)[LeftHead], 83 │ │ align(center, title), 84 │ │ align(right)[Right Head] 85 │ │ )) │ ╰──────^

有可能获得更新的标题?

请参阅上面的详细信息
    

我还没有足够精通典型的熟练来解释您遇到的错误背后的原因(也许与键入功能是“纯”函数有关吗?)...

,但是,对于它的价值,我认为您可以通过在
#locate

函数中定义内容,可以为第一页(或任何页码)具有不同的标题,如下所示:

#set page( header: locate( loc => if [#loc.page()] == [1] { [header first page] } else { [header other pages] } ) )

header typst
2个回答
10
投票
#locate

调用内部检索部分的正文,并直接在标题定义中使用。为了实现这一目标,我使用

#find

函数来获取每个页面的第一个和最后一个标题,以及两个变量来“记住”这些页面的页面,这些页面不包含标题。

#let ht-first = state("page-first-section", [])
#let ht-last = state("page-last-section", [])

#set page(
    header: locate(
        loc => [
            // find first heading of level 1 on current page
            #let first-heading = query(
                heading.where(level: 1), loc)
                .find(h => h.location().page() == loc.page())
            // find last heading of level 1 on current page
            #let last-heading = query(
                heading.where(level: 1), loc)
                .rev()
                .find(h => h.location().page() == loc.page())
            // test if the find function returned none (i.e. no headings on this page)
            #{
                if not first-heading == none {
                    ht-first.update([
                        // change style here if update needed section per section
                        (#counter(heading).at(first-heading.location()).at(0)) #first-heading.body
                    ])
                    ht-last.update([
                        // change style here if update needed section per section
                        (#counter(heading).at(last-heading.location()).at(0)) #last-heading.body
                    ])
                // if one or more headings on the page, use first heading
                // change style here if update needed page per page
                [#ht-first.display(), p. #loc.page()]
            } else {
                // no headings on the page, use last heading from variable
                // change style here if update needed page per page
                [#ht-last.display(), p. #loc.page()]
            }}
        ]
    )
)

现在每个页面都应作为标题:(截面)截面标题,第1页。 page-number.
    

typst
版本
0.11.1

此版本不再有效,并在
loc
上引发错误:

only element functions can be used as selectors


0
投票
iNSTEAD,您可以做类似的事情

#set page( header: context { if here().page() == 1 { [header first page] } else { [header other pages] } }

为例如第一页
	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.