# Align minipage to baseline of first or last line inside minipage?

The minipage environment has options for making the top, bottom, or center of the minipage box line up with the baseline of the surrounding text. Is there a way to make it line up to the baseline of the first or last line of text inside the minipage?

I imagine I'd use \raisebox, but how do I calculate the distance from the top of the minipage to the first line's baseline (or the bottom of the minipage to the last line's baseline)?

## Update:

As indicated in the excellent answer below, minipage already does what I want. The problem I had was that the \color command was messing with the minipage in all sorts of bizarre ways. Putting a \strut at the beginning and end of the minipage didn't fix it, but everything started working once I changed \color to \textcolor:

\documentclass{article}
\usepackage{color}
\usepackage{varwidth}
\newcommand{\sampletext}{testing (1)\\testing (2)}
\newcommand{\tmp}[2]{\fbox{\begin{varwidth}[#1]{0.8in}#2\end{varwidth}}}
\newcommand{\broken}[1]{\tmp{#1}{\color[rgb]{0,0,0.5}\sampletext}}%
\newcommand{\fixed}[1]{\tmp{#1}{\textcolor[rgb]{0,0,0.5}{\sampletext}}}%
\setlength{\parindent}{0pt}\setlength{\parskip}{\baselineskip}%
\begin{document}
with \verb|\color|:\\
before \broken{t} between \broken{c} between \broken{b} after

with \verb|\textcolor|:\\
before \fixed{t} between \fixed{c} between \fixed{b} after
\end{document}

-

The [t] and [b] optional arguments of minipage already use the baselines of the first and last line, and not the top or bottom of the surrounding box. AFAIK most of such vertical positioning options in LaTeX address baselines. Here three minipages with [t], [c] (or without) and [b] options (code below):

If this wouldn't be the case I would use \raisebox{\dimexpr-\height+\ht\strutbox\relax}{<content>} and \raisebox{\dimexpr\depth-\dp\strutbox\relax}{<content>} for the first and last baseline, respectively. This depends on the content, but would be accurate if both lines contain a \strut.

### Update:

About the issue with \color: This macro apparently adds some vertical space because at the beginning of minipage TeX is still in vertical mode. To avoid this use \leavevmode before \color (as \textcolor does internally). This will change to horizontal mode and avoid the misalignment. Added a \strut before \color should actually do the same, because it also starts horizontal mode.

\documentclass{article}

\usepackage{lipsum}

\begin{document}

before
\begin{minipage}[t]{1cm}
First\\
Middle\\
Last
\end{minipage}
between
\begin{minipage}[c]{1cm}
First\\
Middle\\
Last
\end{minipage}
between
\begin{minipage}[b]{1cm}
First\\
Middle\\
Last
\end{minipage}
after

\end{document}

-
Wow, you're absolutely right. Something else must be messing with my minipage then, because it's aligning to the absolute top/bottom of the box, not the baseline of the first or last line. I'll have to figure that out. –  Richard Hansen Jul 1 '11 at 19:53
@Richard: You might have something in it which creates vertical space, e.g. \vspace*{0pt} leads to your described behavior. See [Aligning image and text on top, with minipages(tex.stackexchange.com/questions/11630/…) where this technique is used for vertical alignment. My tip: Try to start and/or end the minipage with a \strut. –  Martin Scharrer Jul 1 '11 at 20:02
It was \color's fault. Switching to \textcolor fixed it. Adding \strut to the beginning and end of the minipage didn't work. –  Richard Hansen Jul 1 '11 at 20:31
@Richard: Coincidently I ran into the exact same issue of a broken minipage alignment with an included \color at the beginning when I was answering this question. There a \strut before the \color actually fixed it. –  Martin Scharrer Jul 1 '11 at 20:50
@Richard: Found it: \textcolor is basically only \leavevmode {\color #1{#2}#3}. The \leavevmode is the important here. Without TeX is still in vertical mode when \color is processed and this adds some vertical space somehow. –  Martin Scharrer Jul 1 '11 at 20:54