How to typeset two‑line text in a mathematical formula?

Hello everyone! When I want to typeset a formula with two-line text in Typst, I can’t find a simple and easy way. I have to use:

$ std.stack(dir: #ttb, spacing: #0.3em, "一定时期内流通中", "所需要的货币量")
= frac(
  "商品价值总额",
  std.stack(dir: #ttb, spacing: #0.3em, "同一单位货币流通的", "平均速度(次数)")
) $

But this makes the spacing between the fraction line and the denominator text too small. If I abandon the two-line text, the formula becomes too long.

Is there a simpler way to do this in Typst?

And here is the formula with the Chinese terms translated into English:

$ std.stack(dir: #ttb, spacing: #0.3em, "the required quantity of money", "in circulation over a given period")
= frac(
  "total commodity value",
  std.stack(dir: #ttb, spacing: #0.3em, "the average velocity of circulation", "of the same monetary unit (times)")
) $

this is surprisingly difficult!

I was able to acheive decent results using grid, as the inset there also applies on the outside and not only between the lines.

#let mstack(..args) = std.grid(inset: (top: 0em, bottom: 0.25em), gutter: 0em, ..args)
$
  mstack("同一单位货币流通的同", "平均速度(次数)同")
  = frac(
    mstack("同一单位货币流通的同", "平均速度(次数)同"),
    mstack("同一单位货币流通的同", "平均速度(次数)同")
  )
  = frac("同", "同")
$

However, the values for top, bottom, gutter are very much dependent on the font you use, the above is with the typst default. If you use a different font, you will have to fiddle with these until it looks right. (Note that the space between lines is top + gutter + bottom, you can also use negative values if neccesary.) (I have used frac("同", "同") to compare the vertical alignment.)

For example,


#show math.equation: set text(font: ("Luciole Math", "Harano Aji Gothic"))
#let mstack(..args) = std.grid(inset: (top: 0em, bottom: 0.08em), gutter: 0.16em, ..args)
$ ... $

The easiest way to do math mode text typesetting is via “\n” and move(), as for east asia cjk, watch out their special points. Try:

#show math.equation: set par(leading:0.1em)
$ "一定时期内流通中\n所需要的货币量"
= frac(
  "商品价值总额",
  "同一单位货币流通的\n平均速度(次数)") $

I tested “\n” and “\”.This does not work; the two lines of text overlap one another, and increasing the line spacing(leading) does not improve the result significantly.

leading is not spacing, they are two different parameters

Screenshot_20260809_102606

I’m sure there are No Fonts or other Issue @Rivulet_Cedar , please try two parameters, leading and spacing, at the same time, keep leading:0.1em.

If u can not reproduce, try my script in one new blank file.

I know. It might be due to the font or some other previous settings, so I can’t achieve that effect.

Applying this show rule makes it so that $x^2$ inline math $"一定时期内流通中\n所需要的货币量" = frac("商品价值总额", "同一单位货币流通的\n平均速度(次数)")$ is broken now!

And manually placing #par() or std.par() inside math does not have the indended effect, it leads to subsequent lines hanging below, instead of centering the block of text
image
(compare the alignment with the =)

As far as i can tell, stack, grid (and thus also table) are the only containers which don’t align the baseline like this.

Perhaps it’s much easier to use content blocks #[…] in math. You can tweak every spacing with box.baseline (new in typst v0.15) and v. You can even set par(leading: …) for each part separately.

#set page(height: auto, width: auto, margin: 1em)

#show math.equation: set text(
  weight: "regular", // The default weight in math is 450, a bit thicker than regular (400)
  font: (
    "New Computer Modern Math",
    "Noto Serif CJK SC",
  ),
)

$
  #box(baseline: -25%)[一定时期内流通中\ 所需要的货币量]
  = frac(
    #text[商品价值总额],
    #box[
      #v(0.25em)
      同一单位货币流通的\ 平均速度(次数)
    ]
  )
$

By the way, you can refer to 如何修改公式里的中文字体? | Typst 中文社区导航 or Document math font features introduced in v0.14 by YDX-2147483647 · Pull Request #7118 · typst/typst · GitHub on how to set Han font in math.

1 Like