Skip to content

Behavior of first line indentation in paragraphs seems limiting #311

Closed

Description

@araruna

As I gathered from the docs (as seen on the reference for the first-line-indent argument), the first paragraph on a page is special-cased to never have it's first line indented.

If I understand this correctly, that means not all paragraphs will have indentation applied, just like LaTeX does by default. If this is correct, then some styles will not be able to be represented with Typst. For example, in Brazilian Portuguese documents we are used to apply indentation to every paragraph, regardless of it's length or position in the text, and as such we use the LaTeX package indentfirst pervasively.

Did I understand it correctly?

Activity

  1. laurmaedje commented on Mar 25, 2023

    @laurmaedje
    Member

    We'll add some configuration for this, so that both ways are possible.

  2. added
    bug
    layout
    text
    on Apr 1, 2023
  3. chris-morgan commented on Apr 7, 2023

    @chris-morgan

    Possibly related, paragraphs starting with something other than unformatted text don’t get indented (e.g. #super[x]). All of this makes the feature feel temperamental.

  4. laurmaedje commented on Apr 10, 2023

    @laurmaedje
    Member

    The bug with unformatted text not being indented should be fixed since the last release. Can you still reproduce it? More generally, I tried to fix the first line indent, but its not as simple as it seems. As currently all text is layouted as a paragraph in Typst, removing the rule would indent everything. That includes headings, code blocks, captions, words in boxes, etc. Since paragraphs are not clearly demarked (as they would be in, say HTML), we first need some automatic way to distinguish these two kinds of texts (possibly through the context they appear in).

  5. zhonghes commented on Apr 11, 2023

    @zhonghes

    this problem is still exist
    image

  6. laurmaedje commented on Apr 11, 2023

    @laurmaedje
    Member

    Yes, I know. As mentioned in my previous response, the overall issue is difficult to fix. The only thing that was fixed so far is that formatted text wasn't even indented when it wasn't in the first paragraph.

  7. wychwitch commented on May 3, 2023

    @wychwitch

    As a work-around, I found that setting an horizontal spacing before the start of the paragraph works for my case.

    something like

    #let annotation(body)={
    par(
      [#h(3em)#body]
    )
    }
    

    Where body is the paragraph. This could be set as a show rule so you wouldn't have to use it in a function every time. Again, I'm not sure if it works in every case but for something quick and dirty it's something!

  8. DawnEver commented on May 21, 2023

    @DawnEver

    I found another para hanging-indent
    if you want to indent first line in 1 em

    set par(justify: false, leading: 1.5em, first-line-indent:2em )

    you can do like this:

    set par( hanging-indent:-1em )
  9. wychwitch commented on May 24, 2023

    @wychwitch

    @DawnEver oh wow, I never thought if using a negative hanging indent, great idea! Though if you have a border around the text you'd have to adjust the the block's padding so it wouldn't clip out of it. However it looks like you got around that by setting the first-line-indent to be 2em already! I'll try it out sometime, thanks for sharing :)

  10. pdcxs commented on Jul 26, 2023

    @pdcxs

    I found another para hanging-indent if you want to indent first line in 1 em

    set par(justify: false, leading: 1.5em, first-line-indent:2em )
    

    you can do like this:

    set par( hanging-indent:-1em )
    

    This works anyway. But it will also affect the raw texts/code blocks.

  11. quantrung9 commented on Aug 5, 2023

    @quantrung9

    This one also does the trick (thanks shuosc/SHU-Bachelor-Thesis-Typst#12 (comment))

    show heading: it =>  {
        it
        par()[#text(size:0.5em)[#h(0.0em)]]
    }
    
  12. tcztzy commented on Aug 15, 2023

    @tcztzy

    This one also does the trick (thanks shuosc/SHU-Bachelor-Thesis-Typst#12 (comment))

    show heading: it =>  {
        it
        par()[#text(size:0.5em)[#h(0.0em)]]
    }
    

    so quick and so dirty, but I like it 👍

  13. teenjuna commented on Aug 15, 2023

    @teenjuna
    Contributor

    @laurmaedje sorry for bothering, but do you know if there are plans to resolve this?

  14. 48 remaining items

  15. added a commit that references this issue on Dec 23, 2024
  16. SandaruKasa commented on Dec 27, 2024

    @SandaruKasa

    Let me add my two cents of a workaround as well

    #show heading: it => [ #it #v(-par.spacing) #{ "" } ]
    #show figure: it => [ #it #v(-par.spacing) #{ "" } ]
    #show terms: it => [ #it #v(-par.spacing) #{ "" } ]
    #show list: it => [ #it #v(-par.spacing) #{ "" } ]
  17. PgBiel commented on Dec 28, 2024

    @PgBiel
    Contributor

    Let me add my two cents of a workaround as well

    For future reference, I've posted a similar solution in the forum as well, with examples: https://forum.typst.app/t/how-to-indent-every-paragraph-excluding-headings/2137/4

    It boils down to:

    // Add more elements below as necessary, e.g. list
    #show selector.or(heading, table, grid, figure): it => {
        it
        ""
        context v(-par.spacing - measure("").height)
    }

    It's worth noting that you can unify the show rules by using selector.or as above. Also, for consistency with the original heading spacing, you should exclude the spacing for "".

  18. Andrew15-5 commented on Dec 28, 2024

    @Andrew15-5
    Contributor

    Are there any known or potential issues with this particular method? Perhaps if some set of settings is changed, meaning the state is nothing like default settings? I'm thinking of a fuzzy testing of sorts. All because it's so simple yet so powerful/robust (unlike all others), at least on the paper. I'll put it to the test with my next documents.

  19. fruzitent commented on Dec 29, 2024

    @fruzitent

    Let me add my two cents of a workaround as well

    #show heading: it => [ #it #v(-par.spacing) #{ "" } ]
    #show figure: it => [ #it #v(-par.spacing) #{ "" } ]
    #show terms: it => [ #it #v(-par.spacing) #{ "" } ]
    #show list: it => [ #it #v(-par.spacing) #{ "" } ]

    I had to wrap the figure in a #block[] (#box[] prevents breakable tables) because large image can create an extra blank page.

    image_2024-12-29_19-51-56
    image_2024-12-29_19-51-56 (2)

  20. Andrew15-5 commented on Dec 30, 2024

    @Andrew15-5
    Contributor

    Are there any known or potential issues with this particular method?

    I found that it doesn't work after #pagebreak():

    #let indent = 1.25cm
    #set list(indent: indent)
    #set par(first-line-indent: indent)
    #show heading: set align(center)
    #let complex-title-page(title: none) = align(center + horizon)[#title]
    
    #show selector.or(heading, list, enum, figure, page): it => {
      it
      ""
      context v(-par.spacing - measure("").height)
    }
    
    #complex-title-page(title: "My title")
    #pagebreak()
    
    #lorem(20)
    
    #lorem(20)
    
    - #lorem(5);
    - #lorem(5);
    - #lorem(5).
    
    #lorem(20)
    
    == Heading
    #lorem(20)
    #figure(rect(), caption: [])
    #lorem(20)

    image

  21. PgBiel commented on Dec 30, 2024

    @PgBiel
    Contributor

    Correct; in general, it requires something to be before the paragraph.

    It isn't very easy to solve that without a native implementation. But I think it's OK to manually handle those specific cases for the time being.

  22. Andrew15-5 commented on Dec 30, 2024

    @Andrew15-5
    Contributor

    But I think it's OK to manually handle those specific cases for the time being.

    #show "〜": h(indent) is like:

  23. danilasar commented on Jan 4, 2025

    @danilasar

    I started a new project which is a good reason to finally try and stop using 〜 as a manual intent after every heading, list, figure etc. and instead test workarounds that were posted here. Maybe I missed something, but the only good one was posted by @HPDell, because other workarounds mess up the layout with vertical or horizontal spacing.

    I initially used it as is, and it was already very nice to have automatic indentation everywhere. But soon I faced my first problem: when inserting medium [] it is not flattened and therefore adds a nested structure ("subdocument"). But thankfully by adding 2 lines of code I was able to fix it using a recursive show rule function.

    And soon enough, some places started misbehaving. In the end, one place where I can test several situations started to work as expected no matter what I did. But I still don't have anything besides paragraphs and headings, so my current solution will probably break again in the future. But for now this looks robust enough:

    snippet

    P.S. There is little logic to it: I was just checking the output and trying to make a bunch of conditions, so that there will be just one indentation at a single place in a document (paragraph).

    Are you sure this code works? It looks strange when element type is sequence or styled. My friend used this solution with @preview/codelst:2.0.2 and that led to an error text does not have field "children" at the cycle declaration line (for (i, elem) in doc.children.enumerate()). It looks like when something element was styled, its child was text and recursion call of turn-on-first-line-indentation led to this error.

  24. Andrew15-5 commented on Jan 4, 2025

    @Andrew15-5
    Contributor

    That workaround is very cursed and highly depends on your document structure. It uses a brute-force like approach of checking every scenario in a document. Just don't use it, or create your own from scratch for your document. Or good luck fixing my mess! ;)

    For now, I switched to the superior in most ways solution: #311 (comment). Although I do still need my regular solution in some rare cases: #311 (comment). Now my setting preamble and document content are both much more clean: the show rule is super short and almost never need to prefix any paragraph.

  25. danilasar commented on Jan 5, 2025

    @danilasar

    Thanks for reply. I think it would be nice to have more than one solution so I'll try to do something with your code anyway. #311 (comment) looks sad, but I liked #311 (comment)

  26. jtchen2k commented on Jan 14, 2025

    @jtchen2k

    Returning to this in 2025. Just curious, if the new layout engine released in 0.12 simplifies implementing this feature? I assume many non-English users are anticipating this(The hack of appending a fake paragraph under the headings could lead to orphan headings).

  27. laurmaedje commented on Jan 14, 2025

    @laurmaedje
    Member

    There is progress on the feature. I can't make promises, but we are optimistic that this should be fixed this soon.

  28. Enivex commented on Jan 27, 2025

    @Enivex
    Collaborator

    Closed by #5768

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

    buglayouttext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions