4

I am trying to create a macro which always places the text in its argument on the right side of the line and then the line should end.

For example:

Left-Text \mymacro{Right-Text} 

should have text on the left and then when the text is done $\box$ should be placed on the right, but on the same line (as long as it fits).

I have tried

\newcommand{\mymacro}[1]{\hfill \mbox{#1}}

which works fine provided that Right-Text fits on the last line of Left-Text.

However, if Right-Text gets moved to a new line then it appears on the left side.

For example:

\documentclass{amsart}
\begin{document}
Doesn't work 

Left Text 0123456789 Left Text 0123456789 Left Text 0123456789
\mymacro{Right Text} 

Works

Left Text 0123456789 Left Text 0123456789 Left Text 0123456789 Left Text 0123456789
\mymacro{Right Text} 

Works

Left Text 0123456789 Left Text 0123456789
\mymacro{Right Text} 

\end{document}
egreg
  • 1,121,712

1 Answers1

3

The star form of \hspace avoids to get canceled at the start of a new line:

\newcommand*{\mymacro}[1]{\hspace*{\fill}\mbox{#1}\penalty-9999\relax}

The penalty allows a line break, but it does not force one (-10000) to avoid an underfull \hbox warning, if the paragraph ends after \mymacro.

Heiko Oberdiek
  • 271,626