86

Lamport, LaTeX: A document preparation system, states on p. 104:

There are two ways to make a parbox at a given point in the text: with the \parbox command and the minipage environment. They can be used to put one or more paragraphs of text inside a picture or in a table item.

\parbox and minipage share one mandatory argument (width of the parbox) and the optional argument (vertical alignment). (The second mandatory argument of \parbox "is the text to be put in the parbox" [p. 105].) Lamport recommends the use of minipages instead of parboxes in some cases (e.g. a parbox containing a tabbing or a list-making environment), but doesn't substantiate his advice (or at least I skipped that part). Finally, from Hendrik Vogt's comment to this answer, I gather that one reason to prefer minipages is that "[y]ou don't have to wait that long for the matching closing brace".

I'm aware that the \footnote command doesn't work with \parbox; by contrast, it "puts a footnote at the bottom of the parbox produced by the [minipage] environment" (Lamport, p. 105). Are there other differences in applicability between \parbox and the minipage environment?

P.S.: Kopka and Daily, A guide to LaTeX, state on p. 89:

The text in a \parbox may not contain any of the centering, list, or other environments described in Sections 4.2 through 4.5. These may, on the other hand, appear within a minipage environment.

However, I did some tests using center, itemize and tabbing environments within a \parbox, and LaTeX did not throw error messages. Are Kopka and Daly wrong, or did I miss something?

3
  • 1
    Perhaps I meant syntactic? Commented Dec 3, 2010 at 17:16
  • @Hendrik: Perhaps I should only quote statements I get the hang of? (But I do grasp the concept of matching braces, I swear!)
    – lockstep
    Commented Dec 3, 2010 at 17:24
  • I'm rather sure you fully got what I meant. I'm just not so sure which word would have been adequate where I wrote "semantically" ... Commented Dec 3, 2010 at 18:17

5 Answers 5

52

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):

alt text

\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):

alt text

\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}
6
  • 2
    One can (apparently, I've never tried it) use the footnote package to get sane behavior with footnotes in both \parbox and minipage.
    – TH.
    Commented Dec 4, 2010 at 22:23
  • 1
    @TH: the footnote package does the right thing (or did 15 years ago when i tried it), but it's pretty old now and one might expect it to interact badly with other sophisticated packages. Commented Jan 30, 2012 at 19:51
  • @lockstep: I know it's not a common practice, but I did put those braces around environments on purpose (clearer distinction between commands and environments). Oh, and there's still one left (the line beginning with EDIT). Commented Sep 22, 2012 at 14:55
  • @PhilippeGoutet I corrected the one left. If you feel strongly about the braces, please use the "rollback" feature to revert to the former version of the post, and I will refrain from editing it again.
    – lockstep
    Commented Sep 22, 2012 at 14:57
  • 3
    @Christian: not really, no. An additional difference, though: the values of \textwidth and \columnwidth are untouched by \parbox but changed to the {minipage} width. Commented Feb 27, 2013 at 18:42
16

When your text you wish to enclose contains only a small piece of text then use \parbox. It should have nothing fancy inside such as any of the paragraph making environments. For larger pieces of text, such as those that do contain paragraph-making environments you should use a minipage environment.

There are also more subtle differences between the two such as the amount of space left on top etc. In general a minipage acts like a full page, whereas a parbox acts more like a fancy paragraph. Both of them offer very little semantic meaning - in my opinion. You can add semantic meaning by creating new environments with macro names that reflect their function in the document structure. For example you could define one of those boxes that one finds in manuals with a framed warning, a warning box using parbox and call it warning.

6
  • @Yannis: You're right that neither \parbox nor minipage offer semantic meaning. As for "\parbox[...] should have nothing fancy inside such as any of the paragraph making environments", I'd like to hear why this is so.
    – lockstep
    Commented Dec 3, 2010 at 17:05
  • @lockstep It's not as if the fate of the earth depends on it, but if you do you will get all sorts of errors. If there is an answer to the why it can be found in ltboxes.dtx in the source2e (it beat me I don't really make heads or tails out of it!) but from what I can gather the parbox is just a box while the minipage is an environment based on parbox. To simplify my life, I mostly use minipage.
    – yannisl
    Commented Dec 3, 2010 at 18:29
  • @Yannis: I'd like to see an example where \parbox results in an error but minipage doesn't. (That is, an example without using verbatim stuff.)
    – lockstep
    Commented Dec 3, 2010 at 18:33
  • @Yannis: Thanks for pointing me to source2e.
    – lockstep
    Commented Dec 3, 2010 at 18:44
  • 3
    @lockstep: as you can see by looking at source2e, {minipage} resets \@listdepth whereas \parbox does not. So if you nest six \begin{list}{}{}\item...\end{list} inside each other and then use another one in a \parbox, there will be a too deeply nested error which would not appear if you had used {minipage}. Commented Dec 3, 2010 at 19:26
5

If you make a new environment, you must enclose the body of the environment using lrbox first, then use the saved box in \parbox. But if you use minipage instead of \parbox, lrbox is no longer needed.

2
  • depending on what you're doing, that's only partially true. For example, if you want to put the content of an environment inside a macro (let's say \fbox), you need the {lrbox} whether you use \parbox or {minipage}. Commented Dec 3, 2010 at 16:49
  • @Philippe: You can't use \parbox macro to collect the content of an environment in any case. It is true, that you need to box the content first to use it in a macro later, but that is not the fault of minipage. Commented Jun 28, 2011 at 13:37
3

Simplification
(MaestroGlanz's answer)

The minipage resets the textwidth equal its width
while the parbox leaves the textwidth unchanged:

The following code renders a parbox and a minipage
with their textwidth displayed by the \the command:

\documentclass{article}
\textwidth=400pt
\begin{document}
\parbox{100pt}{\the\textwidth}
\begin{minipage}{100pt}\the\textwidth\end{minipage}
\end{document}

Here's the compiled output:

ParboxMinipage

1

Here is a good example, where parbox behaves very different from minipage:

\documentclass[12pt]{book}% 

\begin{document}

\makeatletter

\def\ns{%
    \hskip 0pt plus 1fill minus 1fill%
}%\ns

\newcommand{\notentext}[1]{%
    \linebreak[1]%
    \noindent%
    \makebox[\textwidth]{#1}%
}%

\fbox{
    \begin{minipage}{0.6\textwidth}
        \mbox{}\notentext{Text \ns Text \ns Text}%mbox{} prevents no-line-to-end-error
    \end{minipage} 
}
\fbox{
    \begin{minipage}{0.3\textwidth}%
        \mbox{}\notentext{Text \ns Text \ns Text}%mbox{} prevents no-line-to-end-error
        \notentext{Text \ns Text \ns Text}%
    \end{minipage}%
}



\fbox{
    \parbox{0.6\textwidth}{\mbox{}\notentext{Text \ns Text \ns Text}}
}
\fbox{
    \parbox{0.3\textwidth}{%
    \mbox{} \notentext{Text \ns Text \ns Text}%mbox{} prevents no-line-to-end-error
    \notentext{Text \ns Text \ns Text}
}%
}

\makebox{}


\end{document}

The hskip plus/minus 1fill behaves different. In minipage, it is limited to the minipage (\textwidth != original \textwidth), in parbox it relates to the whole textwidth (\textwidth = original \textwidth).

Output

3
  • Inside minipage, \textwidth refers to the width of the minipage; this is not true in \parbox, where \textwidth continues to refer to the overall width of the type block.
    – egreg
    Commented Jul 7, 2016 at 9:18
  • @egreg That was almost just out of where I developed the code. I simplified it now. If you are interested, why I did some strange stuff: Defining \ns inside \notentext is a good idea, because in that way it wont interfere with other definitions of \ns (It is only used in \notentext). The global assignment is also necessary, because I put the whole thing in extra { }, which make them local. You probably read over them, which isnt your fault, since spaghetti code. Commented Jul 7, 2016 at 9:22
  • @egreg I changed it to less confusing text. Commented Jul 7, 2016 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .