How to include a file with #set page without causing a pagebreak?

I have a template file template.typ that I import into all my other .typ documents in the project. Then I have a file all.typ, which includes a chapter chapter.typ. template.typ contains a #set page() rule, that induces page breaks when chapter.typ is #included in all.typ document.

Some minimal example:

// template.typ
#let template_func(body) = {
  set page(
    paper: "a4",
    numbering: "1",
    columns: 1,
  ) 
  body 
}
// chapter.typ
#import "./template.typ": template_func
#show: template_func

== One chapter of the book
Some text of this one chapter. It should directly follow the foreword without pagebreak.
// all.typ
#import "./template.typ": template_func
#show: template_func

= All chapters
Foreword for the whole book
#include "./chapter.typ"
Conclusion of the whole book - this text is page-broken, but should follow directly without break.

I narrowed the problem down to the #page function that causes those pagebreaks. Is there a way how to include chapter.typ without the extra break? Thanks a lot

There is no need to apply the template show rule in every chapter. You only need this at the top of your main file all.typ. In chapter.typ you can directly start with the heading, and the show rule will be applied when the chapter is included in all.typ.

1 Like

You’re right, I realised that too. Though is this pagebreak behaviour a bug? I don’t see why the pagebreak should appear if I import the template in both documents.

Because of each call to set page(...) is creating a page break:

The set rule of the page element is where you control all of the page setup. If you make changes with this set rule, Typst will ensure that there is a new and conforming empty page afterward, so it may insert a page break. Therefore, it is best to specify your page set rule at the start of your document or in your template.

Therefore it is not a bug.

2 Likes