The main reason I see to use minipage
over \parbox
is to allow verbatim (\verb
, verbatim
, etc.) text inside the box (unless, of course, you also put the minipage
inside a macro argument).
EDIT Here are other differences between minipage
and \parbox
(from the comments to Yiannis' answer and from looking at the source code of both these macros in source2e).
A first difference, as already mentioned by lockstep in his question, is in the footnote treatment: minipage
handles them by putting them at the bottom of the box while footnotes are lost in a \parbox
(to avoid this, you must resort to the \footnotemark
/footnotetext
trick):
\documentclass{article}
\begin{document}
\parbox[t]{3cm}{text\footnote{parbox footnote}}
\begin{minipage}[t]{3cm}text\footnote{minipage footnote}\end{minipage}
\end{document}
A second difference is in that minipage
resets the \@listdepth
counter, meaning that, inside a minipage
, you don't have to worry about the list
nesting level when using them. Here's an example which illustrates the point:
\documentclass{article}
\begin{document}
\begin{list}{}{}\item\begin{list}{}{}\item\begin{list}{}{}\item\begin{list}{}{}\item
\begin{list}{}{}\item\begin{list}{}{}
\item %\parbox{5cm}{\begin{list}{}{}\item \end{list}}% error
\item %\begin{minipage}{5cm}\begin{list}{}{}\item \end{list}\end{minipage}% no error
\end{list}\end{list}\end{list}\end{list}\end{list}\end{list}
\end{document}
A third difference is that minipage
sets the boolean \@minipagefalse
which in turn deactivates \addvspace
if it's the first thing to occur inside a minipage
. This means that minipage
will have better spacing and allow better alignment compared to \parbox
in some cases like the following (left is minipage
, right is \parbox
):
\documentclass{article}
\begin{document}
Pros: \begin{minipage}[t]{3cm}\begin{itemize}\item first \item second%
\end{itemize}\end{minipage}
Cons: \parbox[t]{3cm}{\begin{itemize}\item first \item second\end{itemize}}
\end{document}
Using the suggestion in Fitting and centering text (both!) in a constrained area, together with Martin's answer that uses the environ
package, the following provides the environment
\begin{fitbox}{<width>}{<height>}
<stuff>
\end{fitbox}
which typesets <stuff>
using a form of binary search to fit the text within the given height <height>
while under a fixed width <width>
constraint set by a minipage
. This is required in order to maintain a proportionate scaling of the font and leading (\baselineskip
).
\documentclass{article}
\usepackage{lmodern}
\usepackage{environ}% http://ctan.org/pkg/environ
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newdimen\fontdim
\newdimen\upperfontdim
\newdimen\lowerfontdim
\newif\ifmoreiterations
\fontdim12pt
\makeatletter
\NewEnviron{fitbox}[2]{% \begin{fitbox}{<width>}{<height>} stuff \end{fitbox}
\def\buildbox{%
\setbox0\vbox{\hbox{\minipage{#1}%
\fontsize{\fontdim}{1.2\fontdim}%
\selectfont%
\stuff%
\endminipage}}%
\dimen@\ht0
\advance\dimen@\dp0
}
\def\stuff{\BODY}% Store environment body
\buildbox
% Compute upper and lower bounds
\ifdim\dimen@>#2
\loop
\fontdim.5\fontdim % Reduce font size by half
\buildbox
\ifdim\dimen@>#2 \repeat
\lowerfontdim\fontdim
\upperfontdim2\fontdim
\fontdim1.5\fontdim
\else
\loop
\fontdim2\fontdim % Double font size
\buildbox
\ifdim\dimen@<#2 \repeat
\upperfontdim\fontdim
\lowerfontdim.5\fontdim
\fontdim.75\fontdim
\fi
% Now try to find the optimum size
\loop
%\message{Bounds: \the\lowerfontdim\space
% \the\fontdim\space \the\upperfontdim^^J}
\buildbox
\ifdim\dimen@>#2
\moreiterationstrue
\upperfontdim\fontdim
\advance\fontdim\lowerfontdim
\fontdim.5\fontdim
\else
\advance\dimen@-#2
\ifdim\dimen@<10pt
\lowerfontdim\fontdim
\advance\fontdim\upperfontdim
\fontdim.5\fontdim
\dimen@\upperfontdim
\advance\dimen@-\lowerfontdim
\ifdim\dimen@<.2pt
\moreiterationsfalse
\else
\moreiterationstrue
\fi
\else
\moreiterationsfalse
\fi
\fi
\ifmoreiterations \repeat
\box0% Typeset content
}
\makeatother
\begin{document}
\lipsum[1]
\begin{fitbox}{.5\textwidth}{0.5\textheight}
\lipsum[1-2]
\end{fitbox}
\lipsum[2]
\clearpage
\lipsum[1]
\begin{fitbox}{300pt}{300pt}
\lipsum[1-2]
\end{fitbox}
\lipsum[2]
\end{document}
In the figure below, two pages are typeset, each starting with \lipsum[1]
and ending with \lipsum[2]
to provide some frame of reference. The left page has a fitbox
of dimension .5\textwidth x .5\textwidth
while the page on the right is set at 300pt x 300pt
(square).
Interestingly enough, I'm having trouble compiling this under TeXLive 2011. Although, there is no problem compiling it using the online LaTeX compiler ScribTeX, which runs TeXLive 2009. I don't know what the cause behind this is... This has been fixed by the replacement of \protected@edef\stuff{\BODY}
with \def\stuff{\BODY}
. The original code used this form since it provided two macros - one for parsing the content (called \fillthepage{<stuff>}
) and another for updating a resized version of the content (called \buildbox
). I assume the coding structure required this. However, with everything contained in a single environ
ment fitbox
above, this is not needed anymore.
Best Answer
I do not know, what you want with
\minibox
(packageminibox
), because\framebox
is already adding a frame and\minibox
requires manually line breaks. But you could use environmentminipage
or\parbox
: