I want to construct an environment which prints enclosed text and add a predifined box flushright after the text. Here is MWE:
\documentclass{minimal}
\newenvironment{problem}{%
\bigskip
\textbf{Problem}
\begin{trivlist}
\item[]
}{
\mbox{}\hfil
\linebreak[2]\mbox{}\hfill\fbox{$>>$~Solution}
\end{trivlist}
}
\begin{document}
\begin{problem}
Shorttext. Shorttext. Shorttext. Shorttext.
\end{problem}
\begin{problem}
Shorttext. Shorttext. Shorttext. Shorttext.
Shorttext. Shorttext. Shorttext. Shorttext.
\end{problem}
\begin{problem}
Shorttext. Shorttext. Shorttext. Shorttext.
Shorttext. Shorttext. Shorttext. Shorttext.
Short.
\end{problem}
\end{document}
Unfortunately, the box is not always positioned as I want. Sometimes it extends beyond the right boundary of the text column instead of being pulled out to the next line. And the box is not flushed right when it is pulled out alone to the last line as the above MWE shows (see 3rd occurence of the problem environment):
UPDATE: I found a solution in The TeXBook, on page 106.

Please advice how to achived desired positioneng of the end box.
