How to make appendix optional in template, and how to hide it from "main" contents?

One way to easily detect whether the #show: appendix rule has been applied is to make the appendix function insert metadata into the document, which you can then query . Note that queries should be inside contextual blocks. Finally, you can use conditionals to choose what should be displayed.

Something like this:

#let conf(
  title: none,
  doc,
) = {
  // stuff here

  context {
    if query(metadata.where(value: "appendix")).len() == 0 {
      outline(indent: auto)
    }
    else {
      let appendix-pos = query(metadata.where(value: "appendix")).first().location()
      
      outline(
        indent: auto,
        target: selector(heading).before(appendix-pos)
      )
      pagebreak()
      outline(
        target: heading.where(supplement: [Appendix]), 
        title: [Appendix]
      )
    }
  }

  pagebreak()

  doc
}

#let appendix(body) = {
  // stuff here

  metadata("appendix")

  body
}

4 Likes