2

I'd like \mycomment in the document below to be such that it automatically puts its argument against the right margin on the current line if it fits and otherwise against the right margin on the next line.

How do I do this?

\documentclass{article}


\newcommand{\mycomment}[1]{\hfill\emph{#1}}
\usepackage[letterpaper,margin=2in]{geometry}


\begin{document}

\begin{enumerate}
 \item Hello, how are you today? \mycomment{alright!}
 \item yayayayayya 4kwerjejffkler ejjkerjgejkrnnjrnejkn ekwjewjrwwejk \mycomment{this does not fit on the same line}
\end{enumerate}
\end{document}
JPi
  • 13,595

1 Answers1

4

You can use the solution form Bump right-aligned text to next line iff no room

enter image description here

Code:

\documentclass{article}

\newcommand*{\mycomment}[1]{% \noindent% \raggedright {\nobreak\hfill\penalty50\hskip1em\null\nobreak \hfill#1% \parfillskip=0pt \finalhyphendemerits=0 \par} }

\usepackage[letterpaper,margin=2in]{geometry}

\begin{document}

\begin{enumerate} \item Hello, how are you today? \mycomment{\emph{alright}!} \item yayayayayya 4kwerjejffkler ejjkerjgejkrnnjrnejkn ekwjewjrwwejk \mycomment{\emph{this does not fit on the same line}} \end{enumerate} \end{document}

Peter Grill
  • 223,288