How can I prevent inline equations from being wrapped?

Hi, apparently, Typst has no problem breaking inline equations at symbols such as ×. Is there a way to protect an equation (or maybe general content) from any line breaks? The problem is that inserting word-joiners sym.wj does not work with equations, at least it seems to have no effect.

1 Like

Note that wrapping it in a box helps with linebreaks at symbols like + or - but not with ×.

2 Likes

Okay, so here is what I figured out.

Wrapping an inline equation in a box does solve the problem as long as the equation is not wider than the container width.

#block(
  width: 3cm,
  stroke: gray
)[------------$a+b+c×d$]

#block(
  width: 3cm,
  stroke: gray
)[------------#box($a+b+c×d$)]

When the container is too small to accommodate the equation at all, breaks cannot be avoided this way (however, for practical purposes in a main text body this should be no problem).

#block(
  width: 2cm,
  stroke: gray
)[------------$a+b+c×d$]

#block(
  width: 2cm,
  stroke: gray
)[------------#box($a+b+c×d$)]

Even here, breaks can be avoided by changing the math class of the binary operators. For example $a class("large","+")b class("large","+")c class("large","×")d$ does not allow any line breaks whatsoever. But of course this is a bit hacky.

1 Like

This suggests the solution of measuring the equation to size the box. One can take into account the available width in the parent container or ignore it, depending on the preferred behavior (break or overflow):

// To break the equation when it doesn't fit
#let no-break(eq) = box(layout(size => {
  let w = measure(eq, ..size).width
  box(width: w, eq)
}))

// To overflow when it doesn't fit
#let no-break(eq) = context {
  let w = measure(eq).width
  box(width: w, eq)
}

#show math.equation.where(block: false): no-break
1 Like

I’m pretty sure you need to wrap layout in box because it’s a block container.

1 Like

Indeed, thanks! (I’ve edited the code)

1 Like