# Align equations horizontally with related vertical cells

I have a table like this:

\begin{tabular}{|l|c|r|}
\hline
\multicolumn{3}{|c|}{\textbf{Etumerkillinen}} \\
\hline
Tyyppi & Minimi & Maksimi \\
\hline
i8   & $-2^{7}$ & $2^{7}-1$\\
i16  & $-2^{15}$ & $2^{15}-1$\\
i32  & $-2^{31}$ & $2^{31}-1$\\
i64  & $-2^{64}$ & $2^{63}-1$\\
i128 & $-2^{127}$ & $2^{127}-1$\\
\hline
\end{tabular}


The table is rendered like this:

I would like to align the math equations in a way that the 2's, 1's and the negative signs are aligned horizontally in relation with each other. Is this possible and how would I achieve that?

• Welcome to TeX SX! If I understand well, you want the 2nb and 3rd columns cells to be internally left-aligned ang globally centred? May 7, 2020 at 8:38
• Be aware of the typo in the column Minimi the field -2^{64} May 7, 2020 at 13:01

I don't think you'll go wrong if you choose left-alignment for both data columns. To align the numbers in the second column on 2, - and 1, you could split the single column into three distinct sub-columns.

I would focus my efforts more on giving the table a more open "look", as is done on the right in the following screenshot.

\documentclass{article}
\usepackage{array} % for '\newcolumntype' macro
\newcolumntype{L}{>{$}l<{$}}
\newcolumntype{C}{>{${}}c<{{}$}}
\usepackage{booktabs}
\begin{document}
\begin{tabular}[t]{|l|L|L@{}C@{}L|}
\hline
\multicolumn{5}{|c|}{\textbf{Etumerkillinen}} \\
\hline
Tyyppi & $Minimi$ & \multicolumn{3}{c|}{Maksimi} \\
\hline
i8   &  -2^{7^{\mathstrut}}    &  2^{7} & - & 1   \\
i16  &  -2^{15}   &  2^{15} & - & 1  \\
i32  &  -2^{31}   &  2^{31} & - & 1  \\
i64  &  -2^{64}   &  2^{63} & - & 1  \\
i128 &  -2^{127}  &  2^{127} & - & 1 \\
\hline
\end{tabular}%
\begin{tabular}[t]{@{\kern2pt} l L L@{}C@{}L @{}} % cf barbara beeton's comments below
\toprule
\multicolumn{5}{c}{\textbf{Etumerkillinen}} \\
\cmidrule{1-5}
\multicolumn{1}{@{}l}{Tyyppi} & $Minimi$ & \multicolumn{3}{c@{}}{Maksimi} \\
\midrule
i8   &  -2^{7}    &  2^{7}  & - & 1   \\
i16  &  -2^{15}   &  2^{15} & - & 1  \\
i32  &  -2^{31}   &  2^{31} & - & 1  \\
i64  &  -2^{64}   &  2^{63} & - & 1  \\
i128 &  -2^{127}  &  2^{127} & - & 1 \\
\bottomrule
\end{tabular}
\end{document}

• @albert - Good catch. Please see the revised answer.
– Mico
May 7, 2020 at 9:41
• Nit-pick. In the right-hand image, the first column, beginning with "i", looks squished tightly up against the left edge, especially since the "T" in the column header has some "air" on the left and the "i" is so narrow that it almost looks cut off. May 7, 2020 at 19:01
• Good plan. I'd be tempted to indent the "i"s even a little more, maybe as much as 2pt. May 7, 2020 at 19:45
• It's true that it's an optical illusion, but it's more obvious in the table on the right because the rules delineating the table start at exactly the same place as the table contents. The table on the left has a buffer. May 7, 2020 at 19:58
• Much better now! The "i" still looks skinny, but that can't be helped (and it would be much worse in sans serif). May 7, 2020 at 20:07

You can define \2 macro and use it:

\def\2^#1{\hbox to1.6em{$2^{#1}$\hss}}
\begin{tabular}{|l|c|r|}
\hline
\multicolumn{3}{|c|}{\textbf{Etumerkillinen}} \\
\hline
Tyyppi & Minimi & Maksimi \\
\hline
i8   & $-\2^{7}$ & $\2^{7}-1$\\
i16  & $-\2^{15}$ & $\2^{15}-1$\\
i32  & $-\2^{31}$ & $\2^{31}-1$\\
i64  & $-\2^{64}$ & $\2^{63}-1$\\
i128 & $-\2^{127}$ & $\2^{127}-1$\\
\hline
\end{tabular}

• Please make a complete example out of it and show the output in an image. May 7, 2020 at 9:46
• I have made the example complete as as it is in the question. May 7, 2020 at 9:51

Another possible layout, with the formulæ aligned and centred in their respective columns. It requires eqparbox for the alignment, and collcell to use the former in tables:

\documentclass{article}
\usepackage{array} %

\usepackage{eqparbox}
\newcommand{\eqmathboxM}[1]{\eqmakebox[M][l]{$#1$}}
\newcommand{\eqmathboxm}[1]{\eqmakebox[m][l]{$#1$}}
\usepackage{collcell}

\begin{document}

\setlength{\extrarowheight}{3pt}
\begin{tabular}[t]{|l|>{\collectcell\eqmathboxm}c< {\endcollectcell}|>{\collectcell\eqmathboxM}c< {\endcollectcell}|}
\hline
\multicolumn{3}{|c|}{\textbf{Etumerkillinen}} \\
\hline
Tyyppi & \multicolumn{1}{c}{Maxi Minimi} & \multicolumn{1}{c|}{Mini Maksimi} \\
\hline
i8 & -2^{7} & 2^{7}\hfill- 1 \\
i16 & -2^{15} & 2^{15}\hfill -1 \\
i32 & -2^{31} & 2^{31}\hfill - 1 \\
i64 & -2^{64} & 2^{63}\hfill - 1 \\
i128 & -2^{127} & \eqmakebox[M]{$2^{127}- 1$} \\
\hline
\end{tabular}

\end{document}