I want to write down question and want a function which provides some nice formatting for easily adding new questions to a document. One of the this I want it for the questions to be auto-numbered, I have got that working with a counter but it seems the outline isn’t aware of the numbers using this method.
Hi. There is no perfect solution that is short, because headings only have generally one counter. So you either hack together a proper heading with custom counter, or use a figure with custom kind. First provides a Document Outline, but second gives an option to uniquely label questions.
A crucial info that you don’t say is: will there be other headings and which heading will have numbering enabled. Also if there will be nested structure in the outline.
heading + counter hack:
#let question-counter = counter("question")
#let question() = question-counter.step() + [= Question<question>]
#show <question>: it => {
show: block
it.body
[~]
question-counter.display()
}
#show outline.entry: it => {
if it.element.func() != heading or it.element.level != 1 { return it }
if not it.element.has("label") or it.element.label != <question> { return it }
let h = it.element
let inner = {
h.body
[~]
numbering("1", ..question-counter.at(h.location()))
[ ]
box(width: 1fr, it.fill)
[ ]
it.page()
}
it.indented(it.prefix(), inner)
}
#outline()
= Normal heading
#question()
#question()
#question()
A cleaner way of doing this with figures is by abusing the fact that show rules provide implicit context for (some) built-in elements:
#show figure.where(kind: "question"): it => [
#set align(left)
= #it.caption.supplement #it.caption.counter.display()
#it.body
]
#let question() = figure(
kind: "question",
supplement: "Question",
caption: "",
[]
)
#outline()
#question()<q1>
What is $2 + 2$?
#question()
What is $(2+2)^2$? (Hint: use the results you got from @q1)
#question()
Yeah, this is what I meant by “figure (+ heading for ease of styling)”. You would have to separately apply whatever heading styling you have. So, potentially duplicating the styling, which doesn’t feel great, IMO. But in this case, it still uses heading, so it’s an alternative for what I did, but the split of the code is better and as a result less messing around is required.