#Affecting values of the enclosing scope

17 messages · Page 1 of 1 (latest)

small yew
#

I'm trying to create a chapter function, similar to Latex's \chapter{title}
The idea is this function performs a few tasks to start a new chapter, such as starting a new page, creating the title with a certain font and adornment, etc.
So far, I have something like the attached image.

The chapter function looks like this.

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

Additionally, there is a #show heading: ... that performs some styling on the chapter heading (level 1).

The #Chapter() function is defined as a heading of level 1. Then, headings with = start at level 2 (This is for the sake of TOC and PDF bookmarks).

The issue I'm having is that I have to modify the heading explicitly after the use of the #Chapter() function. Like this:

#Chapter[Set Theory] 
#set heading(offset: 1) // move this info Chapter()

I would like to put that set call, or its equivalent, within #Chapter() to have all the functionality self contained.
Is there some way to affect that heading.offset value from within the #Chapter() function?

willow cove
#

#quick-questions message

#

What about just explicitly setting the offset for chapters à la heading(offset: 0, title) like I've been suggesting …?

small yew
#

I want to create a single function that takes care of things.

willow cove
small yew
#

Are you saying that I would only need to set the offset once per chapter, or once per book? Because I'm already setting it once, on the main index file, but I still need to set up explicitly each chapter.

#

btw, I break down each chapter as an included file, so I'm not sure how that affects things.

#

Something like this:

willow cove
#

Once per document in the main file; mind the scoping.

small yew
#

I do set it in the main file, but it still have to set it again. Otherwise, things get messed up.

#

Do you know if there is a mechanism to affect values in the outer scope?

willow cove
#

Consider providing a minimal demonstrative example renderable via ?r.

small yew
#

Thanks for the help, bro. Don't worry about it.

small yew
#

Ok, I was able to do it. Now, the chapter's code is clean (besides needing to explicit import the "Library.typ", but that's a diferent issue). The following screenshot renders the output as attached to the initial image.

#

The insight I had is that the evaluation of the code is likely done with a stack of arrays, where each array has the values of the current state. So, as the evaluation goes down a scope, it would push an new state-array onto the stack. Therefore, start with the heading at level 2, not 1, in the main file, and then set the heading to level 1 explicitly in the Chapter function. Since that function is a scope below the file where its used then, upon returning, it would pop the state-array and thus restore the previous state values. Since the last (initial) value for the level was 2, then it would be restored to that.