1

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.

output of MWE

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

1 Answers1

1

Perhaps this is what you want:

\documentclass{minimal}

\newenvironment{problem}{%
    \bigskip 
    \textbf{Problem}
    \begin{trivlist}
    \item[]
}{
    \linebreak[2] \mbox{}\hspace*{\fill}\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}

enter image description here

The \hspace* prevents the line break from occurring within \mbox{}\hspace*{\fill}\fbox{$>>$~Solution}. But you also need to provide a place for the line break to occur before you the space after \linebreak[2] is necessary. I'm not sure why you might feel the need for \mbox{}\hfill before the line break.

A.Ellett
  • 50,533