Skip to content

Customizing source line break behavior #710

Open

Description

@Erithax

I don't know if this is possible and if there is demand but:

Current line breaking behavior

source compiled
\n [ ]
\ linebreak()
>1 times \n parbreak()

Want to be able to customize it to

source compiled
\n linebreak()
> 1 times \n parbreak()
\\n continue line

or maybe even

source compiled
\n [ ]
\n\n linebreak()
>2 times \n parbreak()
\ linebreak()

Bonus if this behavior can be changed depending on the context (e.g. in a paragraph vs table cell) and if rules for trimming line breaks in certain contexts can be defined.

Doing this in Typst code right now is almost impossible, mostly because

  • show rules like #show regex("\\s"): white_char => ... never capture line breaks in the source
  • \n gets compiled to [ ] (making it indistinguishable from many other things that generate [ ])
  • the content tree is currently not well traverseable and mutable.

Activity

  1. changed the title [-][Feature Request] Customizing default line break behavior[/-] [+]Customizing default line break behavior[/+] on Apr 11, 2023
  2. added
    feature request
    syntax
    on Apr 11, 2023
  3. michalmoc commented on Mar 19, 2024

    @michalmoc

    Regarding the demand: one major use case would be typesetting poems. Those have manual line breaks by definition, and putting '\' at the end of every line quickly gets annoying. I'd gladly do something like

    #let poem(
      text
    ) = {
      set par(join-lines: false)
    
      text
    }
    
  4. laurmaedje commented on Mar 19, 2024

    @laurmaedje
    Member

    I can imagine adding a set rule to respect line breaks in source and turn them into linebreak() instead of [ ].

    Configurable \ for line continuation, in contrast, would in my opinion do more harm than good. It's really the opposite of what it does now and it would be hard to judge at a glance what a piece of code does. This problem already comes up a bit with the hard linebreaks, but there I see more of a use case.

  5. changed the title [-]Customizing default line break behavior[/-] [+]Customizing source line break behavior[/+] on Jul 16, 2024
  6. fhilgers commented on Jul 27, 2024

    @fhilgers

    I really dislike overriding symbol behaviour if not necessary.

    Could a field be added to the Space node in the ast or just a new node be added? The source already has the comment

    /// Whitespace in markup or math. Has at most one newline in markup, as more
    /// indicate a paragraph break.
    

    Maybe it would be best to keep the information about the type of whitespace in the node?

  7. laurmaedje commented on Aug 4, 2024

    @laurmaedje
    Member

    AST nodes already contain their complete text, so it's easy to find out the number of newlines in it.

  8. davystrong commented on Oct 12, 2024

    @davystrong

    I would also be interested in finding a solution to this. I expected that with the recent changes to the realisation subsystem, I could simply #show "\n": linebreak() but that doesn't work.

  9. laurmaedje commented on Oct 13, 2024

    @laurmaedje
    Member

    The show rule doesn't operate on the source text but on the realized text. In the case of your show rule, there are no "\n" in the realized text, so it doesn't match. (If it did match, it would replace a linebreak with a linebreak, to no real effect.)

  10. mikeonly commented on Dec 12, 2024

    @mikeonly

    Regarding the demand: one major use case would be typesetting poems. Those have manual line breaks by definition, and putting '' at the end of every line quickly gets annoying. I'd gladly do something like

    #let poem(
      text
    ) = {
      set par(join-lines: false)
    
      text
    }
    

    I wonder if any work could be done in this direction? I really enjoy using Typst and, in particular, would like to expand its usage to poetry. Such a feature would be of huge help, because so far editing a poem requires manually add a lot of line breaks.

  11. YDX-2147483647 commented on Jan 26, 2025

    @YDX-2147483647
    Contributor

    one major use case would be typesetting poems

    Workaround

    #show raw.where(lang: "poem"): it => {
      set text(font: "Source Serif Pro")  // Override the monospace
      it.text.split("\n\n").map(
        paragraph => paragraph.split("\n").join(linebreak())
      ).join(parbreak())
    }
    #lorem(10)
    
    #quote(block: true, attribution: link("https://en.wikisource.org/wiki/Shakespeare%27s_Songs/Blow,_Blow,_Thou_Winter_Wind")[_As You Like It_])[
      ```poem
      Blow, blow, thou winter wind,
      Thou art not so unkind,
      As man's ingratitude;
      Thy tooth is not so keen,
      Because thou art not seen,
      Although thy breath be rude.
      High-ho! sing, heigh-ho! unto the green holly:
      Most friendship is feigning, most loving mere folly:
      Then heigh-ho, the holly!
      This life is most jolly.
    
      Freeze, freeze, thou bitter sky,
      Thou dost not bite so nigh
      As benefits forgot:
      Though thou the waters warp,
      Thy sting is not so sharp
      As friend remembcr'd not.
      Heigh-ho! sing, heigh-ho! unto the green holly:
      Most friendship is feigning, most loving mere folly:
      Then heigh-ho, the holly!
      This life is most jolly. 
      ```
    ]
    
    #lorem(10)

    Image

  12. michalmoc commented on Jan 26, 2025

    @michalmoc

    @YDX-2147483647

    Nice idea for simple cases, but it's not unusual to add some commands inside, like footnotes and markings.

  13. bluss commented on May 15, 2025

    @bluss
    Contributor

    Here is another poem + raw block workaround that allows most inline markup: https://forum.typst.app/t/how-to-force-line-breaks-without-extra-spaces/4091/10 I think it still speaks for a native "environment" that works similar to this, i.e preserving linebreaks. It would have benefits like proper editor support for the embedded markup.

  14. JeppeKlitgaard commented on Jun 20, 2025

    @JeppeKlitgaard
    Contributor

    I have another use-case for this as well.

    I am rendering a label for use in a laboratory and the users are not expected to be generally familiar with Typst, but may use some of its intuitive typesetting (bold, italics, etc). More advanced users will be able to input chemical equations using #ce from typsium or something similar.

    My approach to this is to eval(..., mode="markup") an input string, but I would like for this to break on newlines without appending a \ to the end of each line. I have so far simply replaced newlines in the input string with #let input_str = input_str.replace("\n", "\\\n"), but this does not work for multiline code, since the resulting \ will be a syntax error in that case.

    My current workaround is to instead do #let input_str = input_str.replace("\n", "\n\n") and add a set par(spacing: ...) to make paragraph breaks be similar size to line breaks. This is okay for my use case since there really isn't room for paragraphs on the labels anyway, but is admittedly a bit of a hack. A native solution would be preferable.

    Edit: My workaround has an unfortunate limitation, which a solution to this issue would alleviate: The user cannot enter empty lines, as they simply are not rendered as linebreaks.

  15. discapes commented on Apr 21, 2026

    @discapes

    When I'm taking notes in math, I find myself writing a \ on almost every line. For example:

    $
    \ 2x + 3y = 7
    \ 4x - y = 5
    \
    \ "multiply second by 3:"
    \ 2x + 3y = 7
    \ 12x - 3y = 15
    \
    \ "add:"
    \ 14x = 22
    \ x = 11/7
    \
    \ "substitute back:"
    \ y = 4x - 5
    \ y = 44/7 - 35/7 = 9/7
    \
    \ "check:"
    \ 2(11/7) + 3(9/7) = 22/7 + 27/7 = 49/7 = 7 #sym.checkmark
    $

    One might argue that I'm not supposed to put everything math mode and the proper way to get that same result would be

    #show math.equation.where(block: true): set block(above: 0.65em, below: 0em)
    \
    $ 2x + 3y = 7 $
    $ 4x - y = 5 $
    \ multiply second by 3:
    $ 2x + 3y = 7 $
    $ 12x - 3y = 15 $
    \ add:
    $ 14x = 22 $
    $ x = 11/7 $
    \ substitute back:
    $ y = 4x - 5 = 44/7 - 35/7 = 9/7 $
    \ check:
    $ 2(11/7) + 3(9/7) = 22/7 + 27/7 = 49/7 = 7 #sym.checkmark $
    

    But that's at least as verbose as the first example.

    Since the value proposition typst has for me is being less verbose and faster to type (for taking lecture notes), it's a shame that I have to type a backslash at the beginning of each line.

  16. discapes commented on Apr 21, 2026

    @discapes

    Now I could as a workaround do the following to get my ideal version of how typst syntax should work:

    #show raw.where(lang: "math"): it => text(1em/0.8, it.text.split("\n").map(l => eval("$ "+l+" $", mode: "markup")).join())
    ```math
    2x + 3y = 7
    4x - y = 5
    
    "multiply second by 3:"
    2x + 3y = 7
    12x - 3y = 15
    
    "add:"
    14x = 22
    x = 11/7
    
    "substitute back:"
    y = 4x - 5
    y = 44/7 - 35/7 = 9/7
    
    "check:"
    2(11/7) + 3(9/7) = 22/7 + 27/7 = 49/7 = 7 #sym.checkmark
    ` ` `

    But of course with that hack I lose all of the LSP features, syntax highlighting, autocomplete, errors at the correct place etc. So it's not really a good solution..

  17. DebianFanatic commented on May 25, 2026

    @DebianFanatic

    I just discovered Typst three days ago. To get a feel for it, I've been manually converting a large LaTeX document I have, and just ran into a need to have "poetry" type lines, where a hard return in the source is actually rendered as a linebreak. My fave AI and I wrestled unsuccessfully with it for about an hour, and then I found this thread (and a similar one), and none of the solutions offered here are acceptable to me for one reason or another.

    I had high hopes for Typst; was enjoying it. But something this simple (preserving newlines), made so hard (impossible), tells me that this is not a tool I can use. Disappointing.

  18. isuffix commented on May 26, 2026

    @isuffix
    Collaborator

    If you can give more details, we'd appreciate it. I'm particularly curious what was hard when using raw blocks with a poem language tag? since that's what I fell back to when typesetting a poem and felt it worked alright.

  19. bluss commented on May 26, 2026

    @bluss
    Contributor

    I didn't know this one while looking at raw blocks, so this is also an interesting workaround:

    #"
    While it's not intended to be a fix-it-all,
      Using a string actually preserves whitespace & and linebreaks too.
    It has the same limitations as raw blocks -- no markup
    "
    Image
  20. DebianFanatic commented on May 26, 2026

    @DebianFanatic

    isuffix asks:

    If you can give more details, we'd appreciate it. I'm particularly curious what was hard when using raw blocks with a poem language tag? since that's what I fell back to when typesetting a poem and felt it worked alright.

    I'm looking for a tool that I can give to writers that is better (for large multi-chaptered, complicated 500-page documents, at least) than the fragile WYSIWYG tools to which they're accustomed (MS-Word, LO Writer, etc).

    LaTeX and most other TeX derivatives have too much of a learning curve, but Typst is really close to a tool I think they could use.

    There's already enough friction in learning to use Typst; this is too big of a hurdle. For example, a section of poetry that quotes someone quoting, with indentations for each quoting, with an italicized underlying Greek word in parentheses mid-quote).

    First off, the writers must have the mental power to remember, "Oh, yeah, poetry isn't done like all other formatted text, with something like #discuss[blah blah blah], but instead requires three backtics." That is putting the burden on the writer, where it does not belong, instead of on the tool, where it does belong.

    Secondly, the raw format and the string format totally destroys that formatting I described, forcing the writers (or me) to do weird janky hacky kludges to make the poetry lines look the way they're supposed to look.

    A simple switch that leaves newlines in the text is all that's needed to keep Typst on the "this may very well work for us" list.

  21. Aitanuqui commented on Aug 20, 2026

    @Aitanuqui

    I want to add that CommonMark, Obsidian and AsciiDoc acknowledge this use case of preserving source line breaks.

    CommonMark

    A conforming parser may render a soft line break in HTML either as a line ending or as a space.

    A renderer may also provide an option to render soft line breaks as hard line breaks.

    Obsidian
    Image

    AsciiDoc per block

    [%hardbreaks]
    Ruby is red.
    Java is beige.
    

    AsciiDoc per document

    = Line Break Doc Title
    :hardbreaks-option:
    
    Rubies are red,
    Topazes are blue.
    
  22. Lege19 commented on Nov 24, 2026

    @Lege19
    Contributor

    Another way this could be customised which I don't think has been suggested yet would be to use separate elements for spaces, parbreaks, and linebreaks constructed by a single source newline, multiple source newlines, and a \ whitespace, which reduce to the usual space, parbreak, and linebreak elements using a show-rule which can then be overriden.
    This is the same idea as from: https://forum.typst.app/t/syntax-overriding/9196/3

    This would avoid creating more weird set rules.

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

    feature requestsyntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions