typst / typst/typst

Bizarre behavior of `v` in math equations

Open
#6,113 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug math
Dominant language
Rust
Stars
56.1k
Forks
1.7k
Avg merge
3d 10h
Merged PRs (30d)
22

Description

Description

In math mode, we may want to adjust the vertical spacing between lines to make it look nicer. The immediate idea to archive this is using the v element. However, using v in math mode will not produce what we want, as is shown in the following example:

#set page(
// create a background of 10pt grid
  fill: tiling(size: (10pt, 10pt))[
    #place(line(start: (0%, 0%), end: (0%, 100%), stroke: luma(80%) + 0.5pt))
    #place(line(start: (0%, 0%), end: (100%, 0%), stroke: luma(80%) + 0.5pt))
  ],
  margin: 0pt,
  width: 80pt,
  height: 100pt,
)
#set text(20pt)
#set par(leading: 10pt, spacing: 10pt)

// two lines without v
#box(stroke: red + 1pt)[$
#box(rect(width: 30pt, height: 20pt, fill: gray))\
#box(rect(width: 30pt, height: 20pt, fill: gray))\
$]
// two lines separated by a v(50pt)
#box(stroke: red + 1pt)[$
#box(rect(width: 30pt, height: 20pt, fill: gray))#v(50pt)\
#box(rect(width: 30pt, height: 20pt, fill: gray))\
$]

Image

We can see that after inserting a #v(50pt), instead of creating a 50pt spacing between two lines, the first line is padded with 10pt spacing above and 20pt spacing below (plus 10pt par leading), which is bizarre.

The reason behind this behavior is that the v element is treated as a frame of height 50pt and baseline 30pt. Why 30pt? Because v element is not treated as expected in math mode. It is handled in the else part in layout_realized(), resulting in its baseline set to half of its height (25pt) plus AxisHeight of the math font (5pt).

https://github.com/typst/typst/blob/efdb75558f20543af39f75fb88b3bae59b20e2e8/crates/typst-layout/src/math/mod.rs#L599-L604

Clearly we never want v being handled like this. This problem does not occur in text mode since v automatically creates a new paragraph. But math equations are always treated as a paragraph regardless the presence of v. One workaround is to force the baseline of v element being 0. Or we should v to create a new paragraph?

Reproduction URL

No response

Operating system

Linux

Typst version
  • I am using the latest version of Typst

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied reproduction and inspect layout_realized() in crates/typst-layout/src/math/mod.rs, especially the linked lines 599-604. Determine the intended baseline and paragraph behavior for v in math mode, then verify the example has the expected vertical spacing. Done means the reported bizarre spacing is resolved without regressing ordinary math layout.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.