12

I'm using \hfill to flush some text to the end of the line. However, when the stuff preceding \hfill takes up most of the line, the text at the end is broken over to the next line.

What I am want is this: When the stuff preceding \hfill doesn't leave enough space at the end of the line for the text following \hfill, that text should in its entirety flush to the end of the next line.

A MWE:

\documentclass{article}
\begin{document}
\hrule % show the margins
\rule{28em}{.2ex} \hfill S o m e t h i n g
\end{document}

enter image description here

Sverre
  • 20,729

1 Answers1

15

enter image description here

\documentclass{article}

\def\foo{\hspace{\fill}\mbox{}\linebreak[0]\hspace*{\fill}}
\begin{document}

\hrule % show the margins

\bigskip

\rule{32em}{.2ex}\foo \mbox{S o m e t h i n g}

\bigskip

\rule{28em}{.2ex}\foo \mbox{S o m e t h i n g}

\bigskip

\rule{21em}{.2ex}\foo \mbox{S o m e t h i n g}

\bigskip

\rule{12em}{.2ex}\foo \mbox{S o m e t h i n g}

\end{document}
David Carlisle
  • 757,742
  • you might want to point out that it's necessary that the paragraph end (with either a blank line or \par) for that material to be flush right. – barbara beeton May 13 '13 at 16:46
  • No idea what you did there, but works like a charm. – Sverre May 13 '13 at 16:49
  • @David Is there any way to know that the \mbox{Something} will be put in the next line or at the end of the line without any linebreak?

    You know, In my case I want to put some negative vertical space for the next line if \linebreak happens.

    – javadr Mar 18 '18 at 08:42
  • @javadr it's better to as a new question than comment on really old ones but probably the answer is no (but you can set \baselineskip to adjust the space of the stuff after the break. But ask a new question with a complete example showing the issue and someone will answer. – David Carlisle Mar 18 '18 at 09:10