29

I need a box that fulfils these conditions:

  1. It can contain multiple lines (and paragraphs) of text (unlike \hbox)
  2. A line that is longer then \textwidth is split to the next line (like \parbox, \vbox and unlike \hbox, \mbox)
  3. The content of the box is surrounded closely. That is width of the box is automatically set to width of content (like \hbox, unlike \vbox and \parbox). Because of 3. width of box should actually be minimum of \textwidth and width of box content. Height is height of box content.

How can I get such a behaviour?

31

The environment varwidth from the package of the same name needs only a length which will be the maximum of the allowed box width

\documentclass[a5paper]{article}
\usepackage{varwidth}    
\begin{document}

\fbox{%
\begin{varwidth}{\textwidth}
some text
\end{varwidth}}

\fbox{%
\begin{varwidth}{\textwidth}
some more text to see if there can be a line break at the end
of the line with this nonsense text
\end{varwidth}}

\fbox{%
\begin{varwidth}{0.5\textwidth}
some text
\end{varwidth}}

\fbox{%
\begin{varwidth}{0.5\textwidth}
some more text to see if there can be a line break at the end
of the line with this nonsense text
\end{varwidth}}

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.