How to Detect a Paragraph break?

I want to add some indentation after the equation conditionally. For example, If the paragraph is ended by a equation, add h(2em).

Somebody answerd this question, but I forgot which questin it is.
This is his code(maybe have changed):

#show math.equation: it => {it + [#[ #[]<eq-end>]]}
#show par: it => {
  if it.first-line-indent.amount == 0pt {
    // Prevent recursion.
    return it
  }
  context {
    let eq-end = query(selector(<eq-end>).before(here())).at(-1, default: none)
    if eq-end == none { return it }
    if eq-end.location().position() != here().position() { return it }
    // Paragraph start aligns with end of last equation, so recreate
    // the paragraph, but without indent.
    let fields = it.fields()
    let body = fields.remove("body")
    return par(
      ..fields,
      first-line-indent: 0pt,
      body,
    )
  }
}

That example was from @Eric I believe. Here I’ve updated it for this case, where we add indentation after an equation.

But we should say, first we need to know: what are your indent settings for the document in general? Specify the paragraph indent settings completely. Then we can work out how to tweak the result.

If you can, just use the paragraph settings with/without all: true as needed.


It is a bit fraught to work with show rules on paragraphs. If we replace an element we need to avoid processing the replaced item again. I used my function from setrowrules/recursionstop.typ · main · bluss / typst-recipes · GitLab for this, without that you would need some paragraph setting dependent logic to avoid recursion. (That probably has better performance but requires tweaking if you tweak the indent rules.)

add indent
/// Apply a style function, like for a show rule, but block recursive application of it
///
/// - markerlabel (label): unique label for this style function
/// - func (function): the style function; signature function(content) -> content
/// - body (content): the style function argument
#let recursion-stop-nonesting(markerlabel, func, body) = {
  // this is the simple version, it never allows nesting
  // copy this version if you need only some simple code
  let sentinel = [#str(markerlabel)]
  let orig-title = bibliography.title
  [#context {
    let ctx-title = bibliography.title
    set bibliography(title: orig-title)
    if ctx-title != sentinel {
      // set simple recursion marker, just the sentinel
      show markerlabel: set bibliography(title: sentinel)
      func(body)
    } else {
      // we arrived back at the recursive case; the same thing again or a descendant
      body
    }
  }#markerlabel]
}


#show math.equation: it => {it + [#[ #[]<eq-end>]]}
#show par: recursion-stop-nonesting.with(<_par_indent>, it => {
    let eq-end = query(selector(<eq-end>).before(here())).at(-1, default: none)
    if eq-end == none { return it }
    if eq-end.location().position() != here().position() { return it }
    // Paragraph start aligns with end of last equation, so recreate
    // the paragraph, but with indent.
    let fields = it.fields()
    let body = fields.remove("body")
    par(
      ..fields,
      first-line-indent: (amount: 2em, all: true),
      body,
    )
})


#lorem(20)

$ x=1 $

#lorem(20)


For a different approach also see the second part of this comment: Why does a display equation break a paragraph? - #14 by bluss

Two cases need to be handled correctly.

The first case is that:

for example, by the symbol $mathbf(v)_k$, which has two components behind it:
#nonumber($ 
  mathbf(v)_k equiv mat(alpha_k; beta_k). 
$)

A vector defines the direction in the chosen coordinate system, a point determines the 
position in the chosen coordinate system. In the affine coordinate system, a point in 
space can be described with the help of a radius-vector.

The first line: ‘A vector defines the direction’ should indented.

The second case is that:

 Consider a point of space having coordinates $p_1, p_2, p_3$ in this system. 
Let's construct a vector
$ mathbf(p) = p_1 mathbf(e)_1 + p_2 mathbf(e)_2 + p_3 mathbf(e)_3, $ <en-eq:vector>
coming from the origin of the affine coordinate system.

The line: ‘coming from the origin’ shouldn’t indented.

They both count as new paragraphs but only the first case has an explicit parbreak element inserted. So you can totally distinguish them, even if that situation is a bit confusing.

I’d need to think about how to best do that…