Skip to content

Center alignment broken in lists #529

Closed

Description

@queensferryme

Not sure if this a bug or actually an expected behavior. It seems the width of a numbered list is not always 100% but instead restricted to the maximum width of its lines.

Therefore, if you don't have a long (>=100%) line in a numbered list, your math blocks in that list will not be placed at the horizontal center of the page. These math blocks are instead centered relative to the the parent numbered list. This is some what conterintuitive (at least from a previous LaTeX user's perspective).

With a long line:

Without a long line:

To reproduce:

= Question 1

+ #set enum(numbering: "(a)")
  + The constructed sequent is given as follows:
    $ q arrow p, r arrow not p, q and r tack.long ((p and q) arrow not r) or ((p and r) arrow not q) $
    //The constructed sequent has assumptions $X={A_1,A_2,A_3}$ and a conclusion $C$ where:
    Without the long line:
    $ A_1 &= q arrow p\
      A_2 &= r arrow not p\
      A_3 &= q and r\
      C space &= ((p and q) arrow not r) or ((p and r) arrow not q)
    $

Activity

  1. queensferryme commented on Apr 2, 2023

    @queensferryme
    Author

    A quick workaround would be finding some line and wrap it with #box(width: 100%).

  2. added
    bug
    layout
    on Apr 2, 2023
  3. changed the title [-]Unable to center math blocks in a numbered list[/-] [+]Center alignment broken in lists[/+] on Aug 27, 2023
  4. vihanb commented on Sep 2, 2023

    @vihanb

    For anyone else who is encountering this issue. I added the following to my template to resolve this issue:

    show enum: a => box(a, width: 100%)
    
  5. isaacholt100 commented on Feb 13, 2024

    @isaacholt100

    Wrapping the list with box(width: 100%, ...) didn't work for me, I had to wrap the equations in a box and set alignment to center:

    #show math.equation.where(block: true): e => [
        #box(width: 100%, inset: 0em, [
            #set align(center)
            #e
        ])
    ]
  6. Enter-tainer commented on Feb 27, 2024

    @Enter-tainer
    Contributor

    Should we make enum and list's width 100% by default?

  7. laurmaedje commented on Feb 27, 2024

    @laurmaedje
    Member

    Should we make enum and list's width 100% by default?

    Then you can't have a list in an automatically sized block anymore.

  8. Asta-QQ commented on Mar 17, 2024

    @Asta-QQ

    Wrapping the list with box(width: 100%, ...) didn't work for me, I had to wrap the equations in a box and set alignment to center:

    #show math.equation.where(block: true): e => [
        #box(width: 100%, inset: 0em, [
            #set align(center)
            #e
        ])
    ]

    I find that this will make the following content a new paragraph, maybe use #block to replace #box is better.

  9. Andrew15-5 commented on Oct 1, 2024

    @Andrew15-5
    Contributor

    This issue was mentioned in https://forum.typst.app/t/how-to-make-nested-bulleted-lists-where-content-includes-centered-equations/846/3.

    Wouldn't this be enough?

    #show math.equation.where(block: true): block.with(width: 100%)

    Strange that changing the width of the existing block element doesn't change anything:

    #show math.equation.where(block: true): set block(width: 21cm)

    impl Show for Packed<EquationElem> {
    fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
    if self.block(styles) {
    Ok(BlockElem::multi_layouter(self.clone(), layout_equation_block)
    .pack()
    .spanned(self.span()))
    } else {
    Ok(InlineElem::layouter(self.clone(), layout_equation_inline)
    .pack()
    .spanned(self.span()))
    }
    }
    }

    Edit:

    Since this is not an ideal solution, I simplified the previous one instead (which I guess should be better):

    #show math.equation.where(block: true): eq => {
      block(width: 100%, inset: 0pt, align(center, eq))
    }
  10. laurmaedje commented on Oct 2, 2024

    @laurmaedje
    Member

    That would break lists in automatically sized containers.

  11. Andrew15-5 commented on Oct 2, 2024

    @Andrew15-5
    Contributor

    That would break lists in automatically sized containers.

    Are you referring to the top snippet or the bottom one?

  12. laurmaedje commented on Oct 2, 2024

    @laurmaedje
    Member

    The top one. Sorry I was on my phone and too lazy to quote.

  13. asd-a commented on Oct 22, 2024

    @asd-a

    That would break lists in automatically sized containers.

    Can we use width as a parameter of list to meet the both?

  14. laurmaedje commented on Oct 22, 2024

    @laurmaedje
    Member

    I think that wouldn't be a great UX as you couldn't easily adjust the width parameter from list syntax.

    The issue can be fixed just with changes to the internals of the layout engine.

  15. Andrew15-5 commented on Mar 21, 2025

    @Andrew15-5
    Contributor
  16. LRoInT commented on Jul 4, 2025

    @LRoInT

    This issue was mentioned in https://forum.typst.app/t/how-to-make-nested-bulleted-lists-where-content-includes-centered-equations/846/3.https://forum.typst.app/t/how-to-make-nested-bulleted-lists-where-content-includes-centered-equations/846/3 中提到了这个问题。

    Wouldn't this be enough?这还不够吗?

    #show math.equation.where(block: true): block.with(width: 100%)
    Strange that changing the width of the existing block element doesn't change anything:

    #show math.equation.where(block: true): set block(width: 21cm)
    typst/crates/typst/src/math/equation.rs

    Lines 163 to 175 in 022f34c

    impl Show for Packed {
    fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult {
    if self.block(styles) {
    Ok(BlockElem::multi_layouter(self.clone(), layout_equation_block)
    .pack()
    .spanned(self.span()))
    } else {
    Ok(InlineElem::layouter(self.clone(), layout_equation_inline)
    .pack()
    .spanned(self.span()))
    }
    }
    }
    Edit:  编辑:

    Since this is not an ideal solution, I simplified the previous one instead (which I guess should be better):

    #show math.equation.where(block: true): eq => {
    block(width: 100%, inset: 0pt, align(center, eq))
    }

    The problem is 100% isn't equal the page.width, you can see it from this:

    #show math.equation.where(block: true): eq => {
      block(width: 100%,align(center, $eq$))
    }
    #page.
    - #lorem(5)$ 111111 $
      #block(width: 100%, fill: red, "1")
    + #lorem(5)$ 111111 $
    #lorem(5)
    $ 1111111 $
    #block(width: 100%, fill: red, "1")
    
    #show math.equation.where(block: true): eq => {
      block(width: page.width, fill: red, align(center, $eq$))
    }
    #page.
    - #lorem(5)$ 111111 $
    + #lorem(5)$ 111111 $
    #lorem(5)
    $ 1111111 $

    Image
    So the way to solve it is to use page.width and solve how much space dose the numbering and indent use

  17. PgBiel commented on Jul 8, 2025

    @PgBiel
    Contributor

    I've made a more comprehensive fix which reimplements lists and enums using terms, which should also work on width: auto pages. Just paste the function code below and use show: fix-lists at the top of your document.

    It supports nearly all list and enum features (as of Typst 0.13), except for
    1. #set enum(numbering: "1.a."), i.e. using a string for numbering with more than one level with full: false. That requires parsing the numbering and would be a bit too much for this snippet, so I've kept it simple here. Otherwise, using full: true, using custom numbering functions and stuff is supported.
    2. #set enum(number-align: bottom). That would break the implementation. I suppose the implementation could be conditionally disabled based on whether number-align was customized.

    As a disclaimer, visuals can potentially change in an unexpected way with this fix. If you try it and the output isn't desirable, you may wish to look into a different solution.

    My fix is available here: https://gist.github.com/PgBiel/c530ae8ac937469510ab382e03a6ba2b

    For example:

    #page(width: 300pt, height: auto, margin: 1cm)[
      Before:
      + $ a^2 + b $
      + abc
      - $ a $
        - b #line(length: 50%)
    
      After:
      #show: fix-lists
      + $ a^2 + b $
      + abc
      - $ a $
        - b #line(length: 50%)
    ]

    Output:

    properly centers after applying the fix
    Full test document (note that it still works with `width: auto` pages)

    Note: the tests in this document use some sample text given by another user

    #let lists = [
       - abc
       - nseted
         - nested
           - nested
             - hello world
       - def
       - $ Gamma(x) = integral_0 ^infinity e^(-t)t^(x-1) dif t $
         - stuna
           - auff
    
       #set list(marker: [bbb])
       - hello
         - hi
    
       #set list(marker: n => [L#n])
       - aststas
       - satnrsut
          - rusntaust
    ]
    
    #let enums = [
    #set enum(numbering: (a, ..b) => [#a - #b.pos().join()], full: true)
    = Mathematical Methods
    + Perform the following integrals:
      + $integral q t^2 dif t$
      + $integral_0 ^t (v_0 + a t')dif t'$
    + Prove that the Gamma function, given as $ Gamma(x) = integral_0 ^infinity e^(-t)t^(x-1) dif t $ has the property $Gamma(n+1) = n Gamma(n)$.
    7. a
    + Evaluate $ integral_(-oo) ^oo e^(-a x^2 + b x) dif x $ (Hint: for $a>0$ and $b=0$, the integral is $sqrt(pi\/a)$.)
    
    #[
      #set enum(body-indent: 3em)
      + abc
      + def
      
      #set enum(indent: 3em)
      + lol
      
      + woah
    ]
    
    + nested test
      earsetast
      tt
    
      setatesat
      + abc
      + st
    + tttt
    
    #set enum(reversed: true)
    + abc
    + def
    + rest
    40. esrtna
    8. hi
    + bye
    ]
    
    #lists
    #enums
    #page(width: auto)[
      #lists
      #enums
    ]
    
    #show: fix-lists
    
    #lists
    #enums
    #page(width: auto)[
      #lists
      #enums
    ]

  18. szdytom commented on Jul 11, 2025

    @szdytom
    Contributor

    A simple workaround is manually place a #h(1fr) somewhere in the list

  19. Jollywatt commented on Nov 30, 2025

    @Jollywatt
    Contributor

    That would break lists in automatically sized containers.

    Isn't it possible with Typst's layout algorithm for a list to know whether its parent is automatically versus absolutely sized?

    If the parent of a list is absolutely sized, keep the current behaviour. If the parent of a list has a defined size, I think it is pretty much unanimously expected that the list should inherit its parent's width, like a paragraph of text would.

  20. tjbcg commented on Nov 30, 2025

    @tjbcg
    Contributor

    Isn't it possible with Typst's layout algorithm for a list to know whether its parent is automatically versus absolutely sized?

    I can't claim to know the codebase enough to fully acertain that it's not possible, but my understanding is:

    • During layout, each layout function has no knowledge of the parent container of what it's laying out (layout functions are agnostic to the position of an element in the AST, they only take in the actual element along with a style object).
    • The current behavior is to simply lay out a grid with automatic sizing.
    • Fixing the issue outside of the layout function isn't really possible, because only a grid is laid out (the information about the list elem it comes from is forgotten, i.e. you can't make exceptions for lists outside of the list layout function).
    • Implementing .within selectors in the compiler would solve this issue (I think?), and might be the simplest way forward. Obviously this is an enormous undertaking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglayout

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions