Bizarre behavior of `v` in math equations
Nobody has claimed this yet.
- 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))\
$]
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).
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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