How can I place text in the same line on the right side unless there is no space left

I’d like to have a right-aligned text starting in the same line as some variable-width unknown text on the left side, as long as there is enough space.
If the remaing space is too small to fit the whole text on the right, it should go in a new line and still be right-aligned.
I’ve tried several versions with h(1fr) and box() wich all somewhat worked, but not in all cases.

Here are some examples together with a very crude montage whether I like them or not:

#set page(width: 10cm)
#let rhs = [#h(1fr) -- content on the right]
short line #rhs

longer line that looks very bad #rhs

external box long text looks good#box(rhs)

short external box #box(rhs)

#{rhs = [#h(1fr)#box()[-- content on the right]]}
internal box #rhs

longer text with internal box #rhs 

I can’t use layout() since in the “actual” usecase I can’t have block-level content wich layout() forces. I can measure the #rhs, but I have not yet figured out a way to measure the text on the left side (since it too might span several lines which measure will ignore and return “wrong” values then) so even if I knew the pages dimensions I’m stuck.

Any Ideas?

Is the content on the right fixed? Can it have multiple lines? If so, how do you want it to be displayed when there are multiple lines?

unfortunately it’s not fixed. It could in theory be multiple lines, but most likely it’s not.
It’s not that important, but if the right content is multiple lines, it should stay right-aligned and start on it’s own line.

Hi @NNS,

you are looking for a word joiner sym.wj.

#let rhs(body) = {
  box()
  h(1fr) 
  sym.wj
  box(body)
}

short line #rhs[-- content on the right]

external box long text looks good #rhs[-- content on the right]

5 Likes

awesome, thanks!
I’m not exactly sure why this works, though.

1 Like

The word joiner glues the first and second box together. The first box acts as an anchor for the word joiner to attach to, because it seems like the horizontal spacing is invisible to the word joiner.
Now, if there isn’t enough space left for the content in the second box, everything: the first box, the space, and the second box breaks to the next line as a group.

2 Likes