[Tex/LaTex] Understanding minipages – aligning at top

graphicsminipagevertical alignment

I would like to be able to align two minipage at the top, without worrying about what is in the minipage. I am aware of the optional [t] parameter, but still have some confusion as to how this all works. I tend to think of a minipage as a regular page in that whatever is placed on it starts at the top and fills the page going across and then down the page, but this does not appear to be the case.

In the MWE example below, I have color coded the sections for reference purposes.

  1. The first case (black) works just as I would expect. The two minipages are aligned exactly as I want with the [t] specifier. No question here.

    The next case (red) only works if the inner minipage also has the [t] specifier. Why is that necessary? I would have thought that any content would be placed at the top of this page, and since both the outer minipage had a [t] things should have aligned at the top.

  2. I would like to place a graphic in the second mini page, and still have the two aligned at the top.

    The simple case (blue) of using \includegraphics directly does not result in the content being aligned at the top.

    So, then I attempt to put this in a \parbox (green) and this too does not yield the desired results.

    I also try to place the graphic inside an inner minipage (brown) and specify the [t] alignment, and even though this worked in the red section, it does not here. The only difference between the red and brown being that the red section had text, whereas the brown section has a graphic.

    I am aware of question regarding Aligning image and text on top, with minipages which suggests to place \vspace{0pt} at the top of the minipage, and that does provide the desired alignment (cyan), but adds vertical space at the top.

    So, my second question is why is the \vspace{0pt} necessary when a graphic is included, but not when it is text. This appears to be a hack and wondering if there is a replacement for minipage that works better without having to worry about exactly what is in the minipage?

    Also, why did wrapping the figure in a parbox (green) or another minipage (brown) not solve this problem?

Here is the MWE:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{xcolor}

\begin{document}
\section{This Works - Aligned at top as desired}
\begin{minipage}[t]{0.6\linewidth}
    \lipsum[1]
\end{minipage}
\hfill
\begin{minipage}[t]{0.3\linewidth}
    \lipsum[2]
\end{minipage}

\color{red}
\section{Why does inner mini page required the [t]?}
\label{Minipage-in-minipage}
\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}
\hfill
\begin{minipage}[t]{0.3\linewidth}
    \begin{minipage}[t]{\linewidth}% Why is [t] required here
        \lipsum[4]
    \end{minipage}
\end{minipage}

\color{blue}
\section{Why are These not aligned at top?}
\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}
\hfill
\begin{minipage}[t]{0.3\linewidth}
    \includegraphics[width=\linewidth]{foo}
\end{minipage}

\color{green}
\section{Why are These not aligned at top?}
\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}
\hfill
\begin{minipage}[t]{0.3\linewidth}
    \parbox{\linewidth}{\includegraphics[width=\linewidth]{foo}}
\end{minipage}

\newpage
\color{brown}
\section{Why does this not work with the [t] as it did in red section?}
This worked fine in Section \ref{Minipage-in-minipage}

\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}
\hfill
\begin{minipage}[t]{0.3\linewidth}
    \begin{minipage}[t]{\linewidth}% Even with [t] here, this does not work
        \parbox{\linewidth}{\includegraphics[width=\linewidth]{foo}}
    \end{minipage}
\end{minipage}


\color{cyan}
\section{Adding a \textbackslash{}vspace\{0pt\} works}
\begin{minipage}[t]{0.6\linewidth}
    \vspace{0pt}% Hack!! But does do the job
    \lipsum[3]
\end{minipage}
\hfill
\begin{minipage}[t]{0.3\linewidth}
    \vspace{0pt}% Hack!! But does do the job
    \begin{minipage}[t]{\linewidth}% Why is [t] required here
        \parbox{\linewidth}{\includegraphics[width=\linewidth]{foo}}
    \end{minipage}
\end{minipage}
\end{document}

Best Answer

It's always a question of reference points. A minipage is, in TeX primitive terms, a vertical box that comes into three flavors:

  • a \vbox
  • a \vtop
  • a \vcenter

They differ in how their reference point is computed; the reference point will always be on the baseline where the box will finally be typeset.

In the first case the reference point lies on the last baseline in the box (corresponding to [b] minipages); in the second case the reference point lies on the first baseline in the box ([t] minipages); in the third case it is midway from the top to the bottom of the box (\vcenter is actually only allowed in math mode and indeed LaTeX builds the minipage inside a pair of $ signs).

Your second example shows the effect: if you omit the inner [t], the reference point of the nested minipage is midway and it aligns perfectly with the reference point of the other minipage

Example 3. The baseline of an included graphics is just the bottom of the graphics: TeX is performing exactly its duty. Example 4 is just the same as Example 2 with [t] omitted, as the two constructs

\parbox{<dimen}{...}
\begin{minipage}{<dimen>}...\end{minipage}

are mostly equivalent (and a [c] specifier is implied).

Example 5 shows a \parbox inside a minipage inside another minipage: since \parbox implies [c] alignment, the behavior is exactly the same as in 4.

Example 6 is subtler: the first item in the two minipages is a \vskip (primitive corresponding to \vspace). Take it as an exception (it isn't really, but a thorough discussion on vertical boxes would be too long): if a vertical box starts with glue a zero height empty line is assumed at the top and so the real top will be the line containing the reference point. The inner \parbox does nothing and can be eliminated. Similarly, if the box ends with glue, a zero height empty line is implied at the bottom.

Related Question