Make all \parbox with same height

lengthsparboxsize;

Running the following MWE will get an ugly typeset which I think is for the reason of different heights of all thees\parboxs.

My question is: As the contents of all \parbox cannot be foreseen, how to make all these \parbox in the same line have the same height(the height of the tallest box, for example) without extra or less vertical space.

MWE:

\documentclass[a4paper]{article}
\usepackage{xcolor}
\begin{document}
\fboxsep=0pt
\fbox{%
  \colorbox{yellow!30}{\fbox{\parbox[c]{.5in}{M}}}
  \colorbox{yellow!30}{\fbox{\parbox[c]{1in}{M\\M}}}
  \colorbox{yellow!30}{\fbox{\parbox[c]{1.5in}{M\\M\\M}}}
  \colorbox{yellow!30}{\fbox{\parbox[c]{2in}{M\\M\\M}}}% 
}
\end{document}

Best Answer

Finding the height of the tallest box can be tricky. One solution is to put all of the boxes inside a savebox. Note that the total height equals the height (above the baseline) plus the depth (below the baseline).

\documentclass[a4paper]{article}
\usepackage{xcolor}

\newlength{\maxheight}

\begin{document}
\sbox0{\parbox[c]{.5in}{M}\parbox[c]{1in}{M\\M}\parbox[c]{1.5in}{M\\M\\M}}%
\setlength{\maxheight}{\dimexpr \ht0+\dp0}%
\fboxsep=0pt
\noindent\fbox{%
  \colorbox{yellow!30}{\fbox{\parbox[c][\maxheight][c]{.5in}{M}}}
  \colorbox{yellow!30}{\fbox{\parbox[c][\maxheight][c]{1in}{M\\M}}}
  \colorbox{yellow!30}{\fbox{\parbox[c][\maxheight][c]{1.5in}{M\\M\\M}}}
  \colorbox{yellow!30}{\fbox{\parbox[c][\maxheight][c]{2in}{M\\M\\M}}}% 
}
\end{document}
Related Question