2

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:

Table rendering 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?

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

3 Answers 3

5

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.

enter image description here

\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}%
\qquad
\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}
10
  • 1
    @albert - Good catch. Please see the revised answer.
    – Mico
    May 7, 2020 at 9:41
  • 1
    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
  • 1
    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
  • 1
    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
  • 1
    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
2

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}
2
  • Please make a complete example out of it and show the output in an image.
    – albert
    May 7, 2020 at 9:46
  • I have made the example complete as as it is in the question.
    – wipet
    May 7, 2020 at 9:51
1

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} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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