How does the counter update function works?

Hi,
I used counter update to reset its value to 0 but it does not work. I am quite sure I am missing something. Could you tell me what I need to add to get the counter back to zero when I call the mainFormatQuestions function

Thanks in advance,

#let mainFormatQuestions(arr_input) = {
  let c = counter("question")
  c.update(0)
  let formatQuestion(it) = block[
    #c.step()
    *Question #context c.display():*
    #it  
  ]
  let arr_output = arr_input.map(i => formatQuestion(i))
  return(arr_output)
}

#let myArray = (  
  [3 x 5 = .........],
  [3 x 7 = .........]
)

#table(columns: (1fr, 1fr, 1fr),
  inset: 10pt,
  align: left,  
  ..mainFormatQuestions(myArray)
)

User @gabe gave a good summary about how state works, and the situation for counter is the same:

(Emphasis mine) The problem why your code doesn’t work is that, due to the explicit return, the update is not put into the content of your document. Basically, there are two ways how a function’s return value may form:

  • if the function reaches a return, the value of that statement is returned
  • otherwise, all values that were encountered are “joined”. For example, a function with body { [foo]; [bar] } has as its result [foo]+[bar] = [foobar].

For the update to do anything, your result would need to include the update() content, and that update content needs to end up in the document.

In your case, a fix that doesn’t interfere with the structure of your code too much would be to return the update and the array, so that your function would be called like this:

#let (update, questions) = mainFormatQuestions(myArray)
#update
#table(columns: (1fr, 1fr, 1fr),
  inset: 10pt,
  align: left,  
  ..questions
)

Another option would be to do the whole table, not just constructing the array:

#questions-table(myArray)

Since questions-table() returns content and not an array, you could include the initial update in there.

1 Like

Hi,
I used the second solution, it is even clearer for the user. Thanks a lot for your help !