How to check if content contains multiple paragraphs?

Hi, I have a function answer(it) which takes some content as its parameter. It should typeset its content with the leading text “Answer”. I’d like to be able to check whether it contains one or several paragraphs, so as to put the header on its own line if there are several paragraphs, and within the paragraph if there is only one. Essentially, I’d like to do this:

if is-single-paragraph(it) {[ *Answer:* it]
} else {
[*Answer*

 it]
}

How can I write is-single-paragraph? Alternatively, is it possible to check whether the first variants fits on one physical line and replace it with the second otherwise?

Here’s a function that can check this:

#let is-multiple-paragraph(txt) = {
  return "children" in txt.fields() and parbreak() in txt.children
}

If you’re interested in how I figured this out, I first used repr[*Answer* it] and another one with a paragraph break to see what was there. Then I used [*Answer* it].fields() to see what attributes could be accessed. I saw that the children attribute included parbreak() if there was a paragraph break.

Also, quick tip, you don’t need curly braces {} around markdown brackets [] in an if statement if that’s what you’re returning :slight_smile:

3 Likes

The function works great in my quick testing, but the name doesn’t match the logic of it. It should either be renamed to something like is-multiple-paragraphs(), or the output should be negated.

1 Like

duh, sorry. Will edit my answer!