Boxes – How to Resize Box to Fit Text

boxes

I would like to have a box which changes width depending on the text in it. Shorter sentences should have a narrow box, and as the sentence gets longer the box should grow (until it is as wide as the text, then I would like a line break). Font size should not be affected. The best solution I've got so far (see below) scales the text when it is short.

\ovalbox{
  \begin{minipage}[t]{0.85\columnwidth}
  \resizebox{\textwidth}{!}{Some variable-length text}
  \end{minipage}
}

Best Answer

You can put a frame around text with the \fbox command:

\fbox{text}

To put text in an ellipse, you can use the TikZ/PGF package and its shapes.geometric library:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes.geometric}

\begin{document}

% You can also make a \newcommand for this
\tikz[baseline=(e.base)] \node[ellipse,draw] (e) {text};

\end{document}

enter image description here

Note that in both cases the text acts as one "big" box relative to the paragraph and will not be broken in separate lines.

If you wish to typeset whole paragraphs in a frame that does not expand to full line width if the paragraph fits on one line, then the following approach (inspired by \@makecaption) might do:

\documentclass{article}

\usepackage{lipsum}

\makeatletter
\newcommand{\varbox}[1]{%
  \setlength{\@tempdima}{\dimexpr\textwidth-2\fboxsep-0.8pt\relax}%
  \sbox{\@tempboxa}{#1}%
  \ifdim\wd\@tempboxa>\@tempdima
    \fbox{\parbox{\@tempdima}{\strut #1}}%
  \else
    \fbox{\strut #1}%
  \fi}
\makeatother

\begin{document}

\lipsum[1]

\noindent\varbox{\lipsum[2]}

\noindent\varbox{not too much text}

\end{document}

Typesetting large blocks inside ellipses does not seem practical to me.