Typst:根据状态在页面之间变化的标题

Kro*_*ate 2 header typst

我想将当前部分包含在打字稿的标题中。“页面”上的文档显示标题类型必须为“无”或“内容”。如果我插入类似 #counter("heading") 的内容,则我定义标头的值为 0,因此标头始终保持 0。

\n

我虽然可以定义 useshow heading来创建一个函数,该函数在创建新标题时更新标题。

\n
  show heading: it => {\n    set text(blue.darken(40%))\n    set page(header: grid(columns: (1fr, 3fr, 1fr), \n      align(left)[LeftHead], \n      align(center, title), \n      align(right)[Right Head]\n    ))\n    it\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

但是我收到以下错误:

\n
error: page configuration is not allowed inside of containers\n   \xe2\x94\x8c\xe2\x94\x80 my_definitions.typ:81:4\n   \xe2\x94\x82  \n81 \xe2\x94\x82 \xe2\x95\xad     set page(header: grid(columns: (1fr, 3fr, 1fr), \n82 \xe2\x94\x82 \xe2\x94\x82       align(left)[LeftHead], \n83 \xe2\x94\x82 \xe2\x94\x82       align(center, title), \n84 \xe2\x94\x82 \xe2\x94\x82       align(right)[Right Head]\n85 \xe2\x94\x82 \xe2\x94\x82     ))\n   \xe2\x94\x82 \xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80^\n
Run Code Online (Sandbox Code Playgroud)\n

是否可以获得更新的标题?

\n

请参阅上面的详细信息。

\n

Yoh*_* O. 9

我对 Typst 还不够精通,无法解释您遇到的错误背后的原因(也许这与 Typst 函数是“纯”函数有关?)...

然而,无论如何,我发现您可以通过在函数中定义内容来为第一页(或任何页码)设置不同的标题#locate,如下所示:

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

现在,遵循相同的原则,您应该能够从调用中检索节的主体#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()]
            }}
        ]
    )
)
Run Code Online (Sandbox Code Playgroud)

现在每个页面都应该有一个标题:(section-number)Section-title,p。页码。