Change the behavior of columns

I am developing a template in which I want to change the behavior of the columns based on a template parameter. I already have the column method working; my problem comes when I use #colbreak(), which breaks to a new page instead of to the next column.

The code I use is similar to this.

#let my-columns(
  count,
  gutter: relative,
  body
  ) = {
     context {
      let condition = read-condition.final()

      if condition  == false {
        body
      }
      else {
        columns(count)[#body]
      }
    }
}

#let my-colbreak(
  weak: false
  ) = {
  context {
      let condition = read-condition.final()

      if condition  == false {
          colbreak(weak: weak)
    }
  }
}

Would you be able to provide a minimum working example (MWE) that we could try, this in order to help you best? Something that shows what is the issue you experience?

Here you have a fully functional example.

#let show-solution = false

#let question-columns(
  count,
  show-solution: bool,
  gutter: 4% + 0pt,
  content,
  ) = {
     context {
      // let show-solution = __g-show-solution.final()

      if show-solution == false {
        [#content]
      }
      else {
        columns(count, gutter:gutter, content)
      }
    }
}

#let question-colbreak(
  show-solution,
  weak: false
  ) = {
  context {
    // let show-solution = __g-show-solution.final()

    if show-solution == false {
      colbreak(weak: weak)
    }
  }
}

#question-columns(2, show-solution: show-solution,
[
  $display(limits("lim")_(x->+infinity) (3x^2+5x-3)/4)$

  $display(limits("lim")_(x->+infinity) x+e^x)$

  #question-colbreak(show-solution)

  $display(limits("lim")_(x->+infinity) 4/(3x^2+5x-3))$

  $display(limits("lim")_(x->-infinity) x-e^(-x))$
])

Thanks for adding an example. In this example, show-solution becomes false so there are no columns in the document. Thus colbreak acts like a pagebreak by default. (The example can also be made more minimal by removing context and some further changes.)

Not important in the example but note that show-solution: bool does not make sense, this probably intends to say show-solution: false (providing default value false).

The example is to implement a much larger template. I need the context to read values from the template; for the example, I already know it is not necessary.

What I want is, based on template values, for the list of questions to appear in multiple columns or in a single column.

Maybe this doesn’t come across as very helpful, but if you are able to show the problem you have in a way we can reproduce with code, then this forum can very quickly find a solution for you. It works very efficiently that way.

I would advise you to use the hover information in the editor, it can tell you which values you’re getting from the state, and which values it’s actually using in your conditionals. Maybe it helps you find the problem.

I think this should be possible. We are all waiting for better custom types support in Typst, because then we could make better templates without needing state - because it’s better to avoid state and use typst’s style functionality as far as possible.

In this case, do you have a working version of this configuration where it’s not using state (just taking an extra function parameter?) I think that would be a good place to start.

1 Like