# In beamer class, how do I remove the additional vertical space before an itemize environment?

On a beamer slide, I want to produce something like

I use the following LaTeX code, which works well in LaTeX with the article class.

\begin{minipage}[t]{1.4cm}
Questions:
\end{minipage}\nobreak
\begin{minipage}[t]{9cm}
\begin{itemize}
\item 1st question;
\item 2nd question.
\end{itemize}
\end{minipage}


With the beamer class, however, I don't get the horizontal alignment correct: There is some additional vertical space above the itemize environment, and I don't manage to remove it. (Is it some \topsep or \partopsep? I tried the package enumitem, but this only made things worse.)

What is a good way to solve this problem? Are there fixes for the itemize, or should I use a totally different approach?

EDIT1:

OK, with Juan's help I have father pinpointed the problem. Let me first elaborate on what the remaining alignment problem is: Yes, Juan, you're right, at the first sight it seems to be pixel perfect. But if you really magnify (3000% or so), then you see that it isn't. It gets worse and much easier visible if you change "1st question" to "1st question $(*)$". You get perfect alignment if you change "1st question" to "Questions:" (really helpful ...). So the contents of the first \item really matters.

I've checked with article class; there the alignment is 100% perfect. A very funny thing: In this case you mustn't use the \vspace{0pt} hack since it destroys the desired alignment.

EDIT2:

Juan's answer provides a great fix for my problem, but to me it doesn't seem like the final solution. Is this strange behaviour of the beamer class a bug or a feature?

-
What is the purpose of using a minipage here? Seems like the first one can be handled by LaTeX's \makebox. The second one seems like a good candidate for \parbox. That said, I can't get beamer's itemize to behave in a sane way, no matter what I do. – TH. Sep 22 '10 at 18:18
Good question. This is just the way I'm doing it, without thinking why. It's never clear to me what the difference between a \parbox and a minipage is. Using minipage for both is just that I like it to be parallel, I guess. I'm quite undereducated in what the "correct" LaTeX way of doing things is. – Hendrik Vogt Sep 22 '10 at 18:52
minipage seems to be people's go to for give me a box of width x. It's overkill and breaks things like footnotes. Most of the time, if you want a horizontal box of width x, you can use \makebox or simply \hbox to x. If you want a vertical box of width x, you can use a \parbox (or a \vbox or \vtop, but that tends to take more work to get everything correct). I have never used a minipage. I'm sure there is a situation that requires it, but I think that requirement is far less common than its use. – TH. Sep 22 '10 at 20:18
Have you tried \makebox here? I don't get horizontal alignment with it. As for \parbox, this does work, but it needs the same fixes that Juan provided below. – Hendrik Vogt Sep 23 '10 at 7:23

It took me two years :-) but now I know what's happening and how to fix it. The main point is that \begin{minipage}[t] creates a \vtop box, which is responsible for alignment at the top. Now one needs to read the TeXbook, bottom of page 81:

The final height x [of the \vtop box] is defined to be zero unless the very first item inside the new vbox is a box; in the latter case, x is the height of that box.

This means: If the minipage starts with some text (possibly broken into lines, which TeX has put into \hboxes), then the vertical alignment of the \vtop box doesn't take place at the very top of the box but at the baseline of the first line of text in the box. And this is exactly what one wants here, alignment of the baselines (red):

However, if the minipage does not start with a box, but e.g. with a color change, then one doesn't obtain alignment of the baselines; instead, the baseline of the word "Questions" is aligned with the top of the second minipage, like this:

## How to fix the problem

According to the above explanation of the behavior of \vtop, the solution will be to make sure that the very first item inside the \vtop box is the \hbox containing the first line of text. Thus, we have to avoid any \kerns, color changes etc. at the top of the minipage. In the code below I roughly explain the main ideas. The code is tested with various combinations of overlay commands and manual color changes.

\documentclass{beamer}
\makeatletter
\def\beamer@callorigitem{%
\@ifnextchar[
\beamer@@callorigitem
{\beamer@origitem
\if@minipage\else
\kern0pt  % don't put the \kern at the top of a \minipage
\fi
\ignorespaces
}%
}
\newcommand*\if@minipage@and@vmode[2]{%
\let\mini@vmode 0%
\if@minipage\ifvmode
\let\mini@vmode 1%
\fi\fi
\if\mini@vmode 1 #1\else #2\fi
}
% Redefine \beamerorig@set@color, \pgfsys@begininvisible and \pgfsys@endinvisible
% such that they don't act at the top of a \minipage;
% instead save the corresponding commands in \set@minipage@color ...
\def\set@minipage@color{}%
\let\save@beamerorig@set@color\beamerorig@set@color
\def\beamerorig@set@color{%
\if@minipage@and@vmode{%
\begingroup
\def\reset@color{\relax}%
\endgroup
\aftergroup\reset@color
}{%
\save@beamerorig@set@color
}%
}
\let\save@pgfsys@begininvisible\pgfsys@begininvisible
\let\save@pgfsys@endinvisible\pgfsys@endinvisible
\def\pgfsys@begininvisible{%
\if@minipage@and@vmode
{\save@pgfsys@begininvisible}%
}
\def\pgfsys@endinvisible{%
\if@minipage@and@vmode
{\save@pgfsys@endinvisible}%
}
% ... and execute them at the beginning of the first paragraph of the minipage
% (where \everypar issues \@minipagefalse).
\let\save@minipagefalse\@minipagefalse
\def\@minipagefalse{%
\set@minipage@color
\gdef\set@minipage@color{}%
\save@minipagefalse
}
\makeatother
\begin{document}
\begin{frame}
\begin{minipage}[t]{1.6cm}
Questions:
\end{minipage}\nobreak
\begin{minipage}[t]{9cm}
\begin{itemize}
\item 1st question;
\item 2nd question.
\end{itemize}
\end{minipage}
\end{frame}
\end{document}


If one compiles this with

\tracingoutput=2


in the preamble, then in the log file one finds the lines

.....\vbox(7.43904+18.72917)x256.0748
......\hbox(7.43904+2.12917)x234.17474, glue set 87.88736fil, shifted 21.90005


This tells that the second minipage is a \vbox with a height of 7.43904pt and a depth of 18.72917, and that the first item inside the \vbox is an \hbox. Without my redefinitions of \beamer@callorigitem, \beamerorig@set@color, \pgfsys@begininvisible and \pgfsys@endinvisible, one will find

.....\vbox(0.0+26.16821)x256.0748
......\pdfcolorstack 0 push {0 g 0 G}
......\kern 0.0


in the log file instead. This means that the second minipage is a \vbox with height 0pt (and one sees the color change and the \kern responsible for this!).

## How to "officially" fix it

The problem isn't really beamer specific, except for the \kern0pt in \beamer@callorigitem from beamerbaseoverlay.sty. One gets the same misalignment with the article document class if one uses, e.g., \color{red} at the top of the minipage. The changes to \beamerorig@set@color (which is \let to \set@color by beamer) and to \@minipagefalse would best fit into the xcolor package; the changes to \pgfsys@begininvisible and \pgfsys@endinvisible should go to pgfsys.code.tex.

-
Nice analysis of the problem and its solution! – Gonzalo Medina Oct 6 '12 at 14:46
@Gonzalo: Thanks :-) – Hendrik Vogt Oct 6 '12 at 16:18

I have no clue why, but the following seems to fix the problem

\begin{minipage}[t]{2cm}\vspace{0pt}
Questions:
\end{minipage}\nobreak
\begin{minipage}[t]{5cm}
\begin{itemize}
\item 1st question;
\item 2nd question.
\end{itemize}
\end{minipage}


Note the added \vspace at the beginning of the first minipage. The problem isn't really a space before the itemize (you can see this by wrapping each minipage on an \fbox), but somehow beamer gets different “tops” for the minipage with the itemize and the one starting with a regular paragraph, making them look misaligned. Maybe someone with more experience in beamer's internals can figure out what is going on?

Edit: The above solution almost works, but not quite. It seems to be bitten by what I call “Beamer's vertical alignment problem”. The problem seems to be that, in some situations, beamer aligns the absolute “tops” of pieces of text. For example the tops of “aaa” and “ttt” lie at very different levels, and if one aligns the tops, the baselines get misaligned. A solution is to use \vphantom and fake the “top” of each piece of text to a common maximum. For example, the following seems to work.

\begin{minipage}[t]{1.4cm}\vspace{0pt}
Questions:\vphantom{(}
\end{minipage}\nobreak
\begin{minipage}[t]{9cm}
\begin{itemize}
\item 1st question $(*)$;
\item 2nd question.
\end{itemize}
\end{minipage}

-
Just found a fix and edited the answer! – Juan A. Navarro Sep 21 '10 at 14:58
The effect of the \vspace is a miracle to me. (Where did you get the idea?) It seems, however, that this almost solves the problem but not quite: The alignment is a lot better, but not perfect. The tip with the \fbox was great since this really helps pinpointing the problem. It seems that with your "hack" the fboxes are aligned, but the itemize is still causing a bit of trouble. – Hendrik Vogt Sep 21 '10 at 15:56
Can you elaborate on what's the problem? on my screen+pdf viewer the alignment seems to be pixel perfect. Btw, I just "stumbled" upon the solution a bit by trial and error, I was fiddling with the alignments trying to add \vspace to shift things up/down. – Juan A. Navarro Sep 21 '10 at 16:26
I've edited the question accordingly. – Hendrik Vogt Sep 22 '10 at 16:59
Ahh.. that's the dreadful Beamer's vertical alignment problem! I've edited my answer with a possible solution. – Juan A. Navarro Sep 22 '10 at 17:58

I just encountered this problem only with a slight variant. I didn't have two minipages that I wanted to line up, but one minipage that I wanted to line up with ordinary text. Thus the sample was text\begin{minipage}[t]{3cm}\begin{itemize}\item text\end{itemize}\end{minipage}. Using \fbox, I could see that the top of the minipage was being aligned with the midpoint of the text. Juan's answer doesn't apply because that messes with the first minipage, which doesn't exist in my scenario. Also, Juan's answer works by pushing the first minipage down to match the second, rather than bringing the second up to match the first.

Here's my solution. I don't know how robust it is. It works by noting that the problem goes away if the minipage doesn't start with something like an itemize environment. So we start with something else to get the external alignment correct. But then that changes the internal alignment so we have to remove that. But since we have full control over the "something else" we started with, we know exactly how much to remove.

The implementation is simply to put \vphantom{}\vskip-\baselineskip at the start of the minipage. It is possible to put something substantial in the \vphantom. What this does is change TeX's opinion of the height of the minipage. This might be desirable if the contents of the minipage has greater height (above the baseline) than the surrounding text.

Here's some sample code with that in place:

\documentclass{beamer}

\begin{document}
\begin{frame}

\fbox{\begin{minipage}[t]{.5cm}Alg\rule{50pt}{1pt}\end{minipage}}\fbox{\begin{minipage}[t]{2.5cm}\begin{itemize}\item\rule{20pt}{1pt} Alg
\item Alg
\end{itemize}\end{minipage}}
\fbox{\begin{minipage}[t]{2.5cm}\begin{itemize}\item\rule{20pt}{1pt} Alg
\item Alg\end{itemize}\end{minipage}}

\fbox{\begin{minipage}[t]{.5cm}Alg\rule{27pt}{1pt}\end{minipage}}\fbox{\begin{minipage}[t]{2.5cm}\vphantom{X}\vskip-\baselineskip\begin{itemize}\item\rule{20pt}{1pt} Alg
\item Alg\end{itemize}\end{minipage}}
\fbox{\begin{minipage}[t]{2.5cm}\vphantom{}\vskip-\baselineskip\begin{itemize}\item\rule{20pt}{1pt} a
\item Alg\end{itemize}\end{minipage}}

\fbox{Alg\makebox[0pt][l]{\rule{50pt}{1pt}}}\fbox{\begin{minipage}[t]{2.5cm}\begin{itemize}\item\rule{20pt}{1pt} Alg
\item Alg
\end{itemize}\end{minipage}}
\fbox{\begin{minipage}[t]{2.5cm}\begin{itemize}\item\rule{20pt}{1pt} Alg
\item Alg\end{itemize}\end{minipage}}

\fbox{g\makebox[0pt][l]{\rule{28pt}{1pt}}}\fbox{\begin{minipage}[t]{2.5cm}\vphantom{X}\vskip-\baselineskip\begin{itemize}\item\rule{20pt}{1pt} Alg
\item Alg\end{itemize}\end{minipage}}
\fbox{\begin{minipage}[t]{2.5cm}\vphantom{}\vskip-\baselineskip\begin{itemize}\item\rule{20pt}{1pt} a
\item Alg\end{itemize}\end{minipage}}

\end{frame}
\end{document}


Result:

You can remove the \fboxes and the rules and change the letters to see that this is reasonably robust, but I don't know what other nightmare combinations should be used to test against.

This even seems to work if the minipage doesn't start with an itemize so, with the same qualifiers above, one could redefine the minipage environment to insert this code automatically (I guess that the \vskip puts in in vertical mode so the \vphantom\vskip-\baselineskip some text works out as two paragraphs but the second shifted to be placed on top of the first).

In article mode, this (naturally) is wrong. So it should be combined with some sort of \ifclassisbeamerbutbeamerarticlenotloaded conditional. Actually, \mode<presentation> would do!

-
It took a while :-) but finally I read your answer in detail. You're right, the disadvantage of Juan's answer is that the first minipage is pushed down, too. For "usual" heights of the first line in the itemize, your solution works great. But I don't know what to do if that line contains something of larger height, like \rule{1pt}{4ex}. Adding this to the \vphantom doesn't help ... – Hendrik Vogt Sep 29 '12 at 10:58

For the vertical spacing try the enumitem package:

\begin{itemize}[noitemsep,nolistsep]
\item 1st question;
\item 2nd question.
\end\{itemize}


you can adjust the vertical and horizontal spacing with the directives in that package

-
As I wrote, I already tried enumitem: What I meant by "only made things worse" is that it doesn't remove the space, but instead it removes the bullets of the itemize environment ... – Hendrik Vogt Sep 21 '10 at 14:23
@Hendrik you can get back the bullets of the itemize environment by writing it as \begin{itemize}[label=\textbullet] – Pranav M May 24 '13 at 9:45