Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

3 Answers 3

up vote 26 down vote accepted

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.

share|improve this answer
add comment

BTW: There's already a solution for all such problems: package adjustbox.

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

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

\color{red}
\section{No one of the mini pages required the [t]?}
\label{Minipage-in-minipage}
\adjustbox{valign=t}{\begin{minipage}{0.6\linewidth}
    \lipsum[3]
\end{minipage}}%
\hfill
\adjustbox{valign=t}{\begin{minipage}{0.3\linewidth}
    \begin{minipage}{\linewidth}% no [t] required here
        \lipsum[4]
    \end{minipage}
\end{minipage}}

\color{blue}
\section{Even pictures are no problem?}
\adjustbox{valign=t}{\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}}%
\hfill
\adjustbox{valign=t}{\begin{minipage}[t]{0.3\linewidth}
    \includegraphics[width=\linewidth]{foo}
\end{minipage}}

\color{green}
\section{And you may use a parbox}
\adjustbox{valign=t}{\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}}%
\hfill
\adjustbox{valign=t}{\begin{minipage}[t]{0.3\linewidth}
    \parbox{\linewidth}{\includegraphics[width=\linewidth]{foo}}
\end{minipage}}

\newpage
\color{brown}
\section{No brown problems}

\adjustbox{valign=t}{\begin{minipage}[t]{0.6\linewidth}
    \lipsum[3]
\end{minipage}}%
\hfill
\adjustbox{valign=t}{\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{\textbackslash{}vspace\{0pt\} changes nothing}
\adjustbox{valign=t}{\begin{minipage}[t]{0.6\linewidth}
    \vspace{0pt}% Hack!! But does do the job
    \lipsum[3]
\end{minipage}}%
\hfill
\adjustbox{valign=t}{\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}

I've already removed some of the [t], but you can remove the others too. The package is very nice for vertical alignment problems. You may even remove the minipages and parboxes around \includegraphics. And there are new commands to to combine \includegraphics and \adjustbox. You may also export \adjustbox options to \includegraphics, but there's a bug at version 0.7 of package adjustbox. So if you want to use package option export use a version > 0.7.

share|improve this answer
    
I would suggest the environment version of \adjustbox here, e.g. \begin{adjustbox}{..}. Note that the first minipage can also be added by adjustbox keys. –  Martin Scharrer Jul 2 at 18:02
add comment

The short explanation, is that TeX always align on a baseline unless you raise or lower the box explicitly.

enter image description here

Imagine your minipages enclosing only one letter or one image. In a horizontal list they are aligned as per the image below. An image - as far as TeX is concerned is just another box, think of an image consisting of a letter. As a matter of fact images also have depth besides height and width.

\documentclass{article}
\usepackage[demo]{graphicx}
\begin{document}
  \fbox{\large fjord}\fbox{\Huge I}%
  \includegraphics[width=2cm,height=2cm]{test}\fbox{\LARGE J}
\end{document}

LaTeX, just provides a convenient macro to use TeX's vbox, vtop etc., but the underlying mechanism remains the same. Best way to understand it is to read the section in the TeX Book relating to boxes and glue (have a look at the little Knuth boxes enclosing letters).

share|improve this answer
    
Thanks for the MWE here as it is very helpful in understanding baseline alignment. Accepting egreg's answer as it has more detail about the examples I gave. –  Peter Grill Nov 16 '11 at 17:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.