Skip to content

Improved list layout - #7895

Merged
laurmaedje merged 35 commits into
mainfrom
list-exp
May 27, 2026
Merged

laurmaedje merged 35 commits into
mainfrom
list-exp

Conversation

@PgBiel

@PgBiel PgBiel commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #529, #1204

Depends on #8150.

Reworks list (and enum, but not terms) layout by 1. adding a form of baseline alignment between the list item's marker and its body, allowing proper alignment between e.g. the marker and a math equation, and 2. allowing the list to expand to the full surrounding width when it isn't infinite, which fixes center-aligned things inside it.

The example image below shows the core of the changes: a centered equation in a list with baseline alignment (the marker matches the baseline height of the math text).

image

Behavior changes

  1. Baseline alignment: The list marker for each item will now be moved downwards in attempt to match the baseline of the body. For example, if the text starts a bit below, the marker will move a bit below. However, if it starts a bit above, the text will be moved down instead of the marker.

It is worth noting that baseline alignment will consider the very first (visible) object inside the body. This would correspond to an equation's first row, or to a rectangle around text, etc., so the results won't always be what one might expect. Still, we can always improve the heuristics for specific use cases.

  1. Centering and expansion: Lists now always occupy the full page width (unless it has width: auto), ceasing the need for hacks like #h(1fr) or show list: block.with(width: 100%) etc. from Center alignment broken in lists #529. This means that anything that is center-aligned inside the list body, such as equations, will still be center-aligned. (But also right-aligned, and so on.) Of course, the marker itself isn't aligned.

There is an important consequence of this. #align(right)[- my list] will no longer align the whole list to the right - the marker remains to the left (start), because the list always occupies the full width, so alignment doesn't affect the marker. To move the whole list to the right, you need to restore the old list behavior of not expanding to the full width. To do this, one can just surround it with a block: #align(right, block[- my list]).

Implementation details

  • Lists are no longer laid out using grids. Instead, they use a stack combined with a custom layouter (for marker horizontal alignment and baseline alignment). stack, differently from a grid with auto columns, propagates to its children whether they can expand to fill the page width or not. This automatically fixes centering in lists (Center alignment broken in lists #529).
  • The custom layouter measures all list markers to assign the same (max) width to all of them, allowing horizontal centering of enum numbers to start or end.
  • For baseline alignment of frames, we calculate the baseline of both (how far down from the top the text / math baseline is) and come to a difference. If this difference is positive, we move the marker down. If it is negative, we move the body down instead.
  • To calculate the baseline of each frame, we first check if the frame has a natural baseline already and align with it (this should match the baseline of the first text/math content in the list item, per Expose baselines for box and block #8150). Otherwise (if there is no text or the content is "too weird"), the marker just goes to the top.

TODOs

  • Reimplement layout with stack, fixing centering
  • Horizontal marker alignment
  • Initial baseline alignment impl
  • Initial tests
  • Support vertical alignment through enum.number-align
  • Some edge-case tests

@mewmew

mewmew commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

Great to see work on this @PgBiel!

As a small reminder, the following Typst roadmap bullet point can be crossed out once this is merged.

  • Fix issues with list (in particular baselines & alignment)

@laurmaedje laurmaedje added layout Related to the layout category, which is about composing, positioning, etc. waiting-on-author Pull request waits on author labels Mar 2, 2026
illusionaries added a commit to illusionaries/typst-template that referenced this pull request Mar 15, 2026
illusionaries added a commit to illusionaries/typst-template that referenced this pull request Mar 15, 2026
@PgBiel
PgBiel force-pushed the list-exp branch 4 times, most recently from d6932b1 to f49fe8a Compare March 29, 2026 14:33
@PgBiel PgBiel mentioned this pull request Apr 22, 2026
1 task done
@PgBiel
PgBiel force-pushed the list-exp branch 5 times, most recently from 4d6126e to 7193c7f Compare April 26, 2026 03:44
@PgBiel
PgBiel force-pushed the list-exp branch 5 times, most recently from 3810905 to ecdeb0c Compare May 7, 2026 02:51
@PgBiel
PgBiel force-pushed the list-exp branch 3 times, most recently from df07e6c to fd9ce28 Compare May 11, 2026 04:17
Comment thread crates/typst-layout/src/lists.rs Resolved
@typst typst deleted a comment from gbchu May 11, 2026
@PgBiel
PgBiel force-pushed the list-exp branch 2 times, most recently from e8e99d1 to daf68d1 Compare May 13, 2026 03:02
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels May 24, 2026

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few minor nitpicks on the code, but more importantly, I found a bug that is not present on main:

When wrapping a list in an auto-sized container like a block, it seems that the body region is not computed correctly. List or body indent or a very wide marker then lead to overflowing content while negative indent leads to a gap at the right end.

Here's an example:

#set page("a5")
#set list(indent: 100pt)
// here are more things to try
// #set list(indent: -50pt)
// #set list(marker: [AAAAAAAAAAAAAAAAAAAAA])
#block[
  - #lorem(20)
]

I think the measurement code path does not take into account the indents and markers at all.


I had already merged with main in preparation of merge, but feel free to force push that away if you prefer to rebase.

Comment thread crates/typst-layout/src/lists.rs Resolved
Comment thread crates/typst-layout/src/lists.rs Resolved
Comment thread crates/typst-layout/src/lists.rs Resolved
Comment thread crates/typst-layout/src/lists.rs Resolved
Comment thread crates/typst-layout/src/lists.rs Resolved
Comment thread crates/typst-layout/src/lists.rs Resolved
Comment thread crates/typst-library/src/model/enum.rs Resolved
Comment thread crates/typst-library/src/model/enum.rs Resolved
Comment thread crates/typst-library/src/model/enum.rs Resolved
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels May 25, 2026
@PgBiel

PgBiel commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

When wrapping a list in an auto-sized container like a block, it seems that the body region is not computed correctly.

Nice catch. I missed the need for auto column width redistribution when mimicking grid column measurement 😅

@PgBiel
PgBiel force-pushed the list-exp branch 2 times, most recently from 5db39f7 to 02fbbe2 Compare May 27, 2026 02:46
@PgBiel

PgBiel commented May 27, 2026

Copy link
Copy Markdown
Contributor Author

I've added the same algorithm as grids now: if the sum of marker width + body width surpasses the available width (total - indents, i.e. total - fixed-width columns), then each is given (up to) half of the available width as needed.

I've also refactored the ListLayouter, splitting measurement into methods as well.

Please take a look at the latest commit to confirm. :)

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels May 27, 2026
@laurmaedje
laurmaedje enabled auto-merge May 27, 2026 09:58
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label May 27, 2026
@laurmaedje
laurmaedje added this pull request to the merge queue May 27, 2026
Merged via the queue into main with commit b241417 May 27, 2026
17 checks passed
@PgBiel PgBiel linked an issue May 27, 2026 that may be closed by this pull request
1 task done
@laurmaedje
laurmaedje deleted the list-exp branch May 27, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

layout Related to the layout category, which is about composing, positioning, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List and enum markers are not aligned with the baseline of the item's contents Center alignment broken in lists

4 participants