# Resize box to fit text

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}
}

-
Do you mean something like \fbox{text}? –  Andrey Vihrov Feb 20 '12 at 18:08
I do mean exactly that, thank you. Is there a way to make it oval as well? –  reseter Feb 20 '12 at 18:13

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}


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.

-
If you want line breaking, you could put a \parbox inside the \fbox. –  Ryan Reich Feb 20 '12 at 18:30
@RyanReich: Yes, but this does not solve the problem of integrating the text in a paragraph, such that it begins on one line and ends on another. (This only makes sense for the frame, not the ellipse.) –  Andrey Vihrov Feb 20 '12 at 18:33
Ah, I understood the breaking request in the question to mean that the box would constitute an independent paragraph. It seems like it would be very difficult to get a flowed frame inside a paragraph; for example, how would it look if there were a gap between the end of the text on the second line and the beginning of the text on the first line? –  Ryan Reich Feb 20 '12 at 18:36

You should also consider the mdframed package. In the case where you have large passages this will actually work across page breaks, and a lot of flexibility in terms of line styles, fill color, etc...

\documentclass{article}
\usepackage[framemethod=TikZ]{mdframed}

\begin{document}
\begin{mdframed}[roundcorner=15pt, linecolor=red, outerlinewidth=2pt,backgroundcolor=yellow!30]
\begin{minipage}[t]{0.85\columnwidth}
\resizebox{\textwidth}{!}{Some variable-length text}
\end{minipage}
\end{mdframed}
\end{document}

-
Ahh, how can I forget the obvious! –  Andrey Vihrov Feb 20 '12 at 19:01
I have gotten so used to using mdframed, that using a simple fbox would probably not have occurred to me. :-) –  Peter Grill Feb 20 '12 at 19:08

You may also have a look at the varwidth package. It allows to define a environment {varwidth} which behaves like a {minipage} but takes a maximum width as argument.

\documentclass{article}

\usepackage{varwidth}

% just for this demo
\usepackage{parskip}

\begin{document}
\fbox{%
\begin{varwidth}{0.8\textwidth}%
Text
\end{varwidth}%
}

Text that deomenstrates the \verb|\textwidth|, an for that is
longer than one line or maybe two.

\fbox{%
\begin{varwidth}{0.8\textwidth}%
Very long text, that breaks a line.
Very long text, that breaks a line.
Very long text, that breaks a line.
\end{varwidth}%
}
\end{document}


-