Can alignment be changed from the 2nd line on in a paragraph?

I’d like to achieve the following:

  • The first line is left-aligned
  • If the text spills onto the second line, it should be right-aligned from the second line on.

Something like that:

Forever young
I want to be forever young
Do you really want to live 
                   forever?
Forever and ever

Is this possible (without inserting linebreaks etc. manually)?

Why would the first line stop at “young” if you don’t have a manual linebreak? (I’m trying to understand the desired output)

Ah yes. My example has four lines of the chorus of that song. It happens to be that the third line is too long to fit the container width, so the line is broken. What I want is that the broken part is right-aligned. So basically similar to a hanging-indent but a “hanging-alignment”.

Edit: I have tried to work with lists here (but with an insivible marker) but for those not even set par(hanging-indent: true) works.

For justified text you could do something like

#set page(width: 6cm)
#set par(justify: true)

#let f(it) = layout(size => {
   it.children.split(linebreak()).map(array.join).map(x => {
      if measure(x).width > size.width {
         box(width: 100%, align(right, x))
      } else {
         x + linebreak()
      }
   }).join()
})

#f[
   Forever young\
   I want to be forever young\
   Do you really want to live forever?\
   Forever and ever
]

image

but for ragged text I don’t know… It seems that this would require hooking up into the paragraph shaping algorithm, after the linebreaks have been decided but before the final result. You can do this in LuaTeX but I don’t think Typst supports that yet.

4 Likes

Thank you, this works for me!

1 Like