#Query returns duplicate label copies

17 messages · Page 1 of 1 (latest)

minor crystal
#

I have a function to create chapters, which I call once for each chapter file.
I'm adding a label to the chapter headers in order to query for them.

#let Chapter(title) = {
    set text(size: 24pt)
    pagebreak(weak: true)
    stack(
        v(5mm),        
        heading(level:1)[#title #label("chapter-heading")], // Notice label
        line(length: 100%, stroke: 2pt + luma(50%)),
        v(5mm)    
    )
} 

I now wanted to query the labels, in order to show the current chapter name in the header. For testing, I started returning all the chapters.

#let GetChapterName() = {
    return query(selector(<chapter-heading>))
}

That GetChapterName() function gets called from set page to style the header.

// Style the header
#set page(
  header: context {            
        let display = IsShowHeader()     
        if display {                        
            block(
                width: 100%,        
                inset: 5.5pt,
                fill: luma(15%)
            )[
                #grid(
                    columns: (2fr, 1fr, 2fr),
                    rows: 1,
                    align: (left, center, right),
                    [ax], [#here().position()], [#GetChapterName()] // This displays the header.
                )
            ]                
        }
    }
)

For some reason, the list returned by GetChapterName() function has duplicate chapter names (see attached image).

Why the duplicates?

crimson temple
#

can you post a complete example that reproduces the problem? I tried with the code you gave (replacing IsShowHeader() with true) and don't see any duplicate

minor crystal
#

I work on this using vscode, with Typst LSP and Typst Preview.

rapid root
#

You may want to look into using the hydra package which does something similar

minor crystal
#

no, thanks. I'd like to learn to do it manually.

crimson temple
#

?r

#set page(width: auto, height: auto, margin: 1cm)

#let GetChapterName() = query(selector(<chapter-heading>))

#let Chapter(title) = heading(level:1, [#title <chapter-heading>])

#outline()

#Chapter[Introduction]

#context GetChapterName()
crimson temple
#

the problem is that you have the label inside of the header, rather than next to the header

#

normally the label is placed in content after the element rather than inside of it

#

when you put it inside, it's duplicated because it appears both in the document and in the outline

#

so instead you can do something like this:

#

?r

#set page(width: auto, height: auto, margin: 1cm)

#let GetChapterName() = query(selector(<chapter-heading>)).first().body

#let Chapter(title) = [
    #heading(level:1, title) <chapter-heading>
]

#outline()

#Chapter[Introduction]

#context GetChapterName()
minor crystal
#

Interesting. Thanks for troubleshooting that, must have been annoying to get at the root cause.

#

Appreciate it.