3

I want to stack three tabular environments vertically inside the same table environment. The following code is my effort. I find that the last table is mis-aligned. Here it is the third, but if I add another the first three will align, but not the fourth. Since the code for the second and third are identical I'm at a loss. (BTW: ignore the mathmode coding on the numbers - the table is inside an example environment and I want to override its effect on the numbers.)

\documentclass{amsbook}
\begin{document}
\begin{table}[h]
    \begin{tabular}{ |c|c|c|c||c| } 
        \hline
        k / m &$1$ &$2$ &$3$ &$p_M$\\ 
        \hline
        $1$ &$0$ &$0.15$ &$0$ &$0.15$ \\
        \hline
        $2$ &$0.3$ &$0.45$ &$0.1$  &$0.85$\\ 
        \hline
        \hline
        $p_K$ &$0.3$ &$0.6$ &$0.1$ &$1.0$ \\
        \hline
    \end{tabular}
    \newline
    \vspace{0.5cm}
    \newline
\begin{tabular} { |c|c|c|c|c|}
    \hline
    $k$ &$1$ &$2$ &$3$ &Total \\
    \hline
    $p_K$ &$0.3$ &$0.6$ &$0.1$  &$1.0$ \\
    \hline
\end{tabular}
    \newline
    \vspace{0.25cm}
    \newline
\begin{tabular} { |c|c|c|c|}
    \hline
    $m$ &$1$ &$2$ &Total \\
    \hline
    $p_M$  &$0.15$ &$0.85$  &$1.0$ \\
    \hline
\end{tabular}
    \caption{Marginal Distributions} \label{tab:Marginals}
\end{table}
\end{document}

Thanks for any help.

1
  • Apologies to all. I realized I'd neglected to check the answers. Done now.
    – TonyK
    Dec 2, 2023 at 21:27

3 Answers 3

1

In amsbook table captions should go at the top.

I suggest to avoid \newline, but also a different method that uses much less space.

\documentclass{amsbook}
\usepackage{booktabs}% for the second table

\begin{document}

\begin{table}[htp]

\caption{Marginal Distributions} \label{tab:Marginals}

\begin{tabular}{ |c|c|c|c||c| } 
    \hline
    $k / m$ &$1$ &$2$ &$3$ &$p_M$\\ 
    \hline
    $1$ &$0$ &$0.15$ &$0$ &$0.15$ \\
    \hline
    $2$ &$0.3$ &$0.45$ &$0.1$  &$0.85$\\ 
    \hline
    \hline
    $p_K$ &$0.3$ &$0.6$ &$0.1$ &$1.0$ \\
    \hline
\end{tabular}

\vspace{2ex}

\begin{tabular} { |c|c|c|c|c|}
    \hline
    $k$ &$1$ &$2$ &$3$ &Total \\
    \hline
    $p_K$ &$0.3$ &$0.6$ &$0.1$  &$1.0$ \\
    \hline
\end{tabular}

\vspace{2ex}

\begin{tabular} { |c|c|c|c|}
    \hline
    $m$ &$1$ &$2$ &Total \\
    \hline
    $p_M$  &$0.15$ &$0.85$  &$1.0$ \\
    \hline
\end{tabular}

\end{table}

\begin{table}[htp]

\caption{Marginal Distributions} \label{tab:Marginals-better}

\begin{tabular}{ @{}ccccc@{} } 
    \toprule
    $k / m$ &$1$ &$2$ &$3$ &$p_M$\\ 
    \midrule
    $1$ &$0$ &$0.15$ &$0$ &$0.15$ \\
    $2$ &$0.3$ &$0.45$ &$0.1$  &$0.85$\\ 
    \midrule
    $p_K$ &$0.3$ &$0.6$ &$0.1$ &$1.0$ \\
    \bottomrule
\end{tabular}\qquad
%%% measure the top table
\sbox0{%
  \begin{tabular} { @{}ccccc@{} }
    \toprule
    $k$ &$1$ &$2$ &$3$ &Total \\
    \midrule
    $p_K$ &$0.3$ &$0.6$ &$0.1$  &$1.0$ \\
    \bottomrule
  \end{tabular}}
\begin{tabular}{@{}c@{}}
\usebox{0}\\
\addlinespace[2ex]
\begin{tabular*}{\wd0} { @{\extracolsep{\fill}}cccc@{} }
    \toprule
    $m$ &$1$ &$2$ &Total \\
    \midrule
    $p_M$  &$0.15$ &$0.85$  &$1.0$ \\
    \bottomrule
\end{tabular*}
\end{tabular}
\end{table}

\end{document}

enter image description here

Note how the third table is made to have the same width as the second one.

With \sbox0{\begin{tabular}...\end{tabular}} we store the second subtable in a box so we can measure its width: I chose this one because the first attempt shows it's wider than the third table.

Now we can make the third subtable as wide as the second one with the help of tabular*. The two of them are placed in an outer tabular to ensure vertical alignment with the first subtable. Instead or retyping the second subtable, we can “use” the already stored one.

3
  • Thanks for weighing in. Advice about formatting always welcome. I confess there are parts of your code I don't understand -- that seems to be my lot in life!
    – TonyK
    Nov 30, 2023 at 19:03
  • @TonyK I added some comments on the magic trick
    – egreg
    Nov 30, 2023 at 20:45
  • Thanks for the added comments/explanation. It helps a lot. Some folks are clearly light years ahead of me, which is fine when they help so freely!
    – TonyK
    Dec 2, 2023 at 0:11
3

amsbook centers table environments, but \newline forces a short left aligned line

enter image description here

\documentclass{amsbook}
\begin{document}
\begin{table}[h]
X
    \newline
    \vspace{0.5cm}
    \newline
X
    \newline
    \vspace{0.25cm}
    \newline
X
\end{table}
\end{document}

Just use a blank line (you almost always need a blank line before \vspace)

\documentclass{amsbook}
\begin{document}
\begin{table}[h]
X
 
    \vspace{0.5cm}
X

    \vspace{0.25cm}

X
\end{table}
\end{document}
1
  • Thanks for the explanation -- it always helps to know why things don't work out as expected.
    – TonyK
    Nov 29, 2023 at 19:48
1

Just get rid of the \newline instructions.

enter image description here

\documentclass{amsbook}
\begin{document}
\begin{table}[h]

    $\begin{array}{ |c|c|c|c||c| } 
        \hline
        \text{k / m} &1 &2 &3 &p_M \\ \hline
        1   &0   &0.15 &0    &0.15 \\ \hline
        2   &0.3 &0.45 &0.1  &0.85 \\ \hline\hline
        p_K &0.3 &0.6  &0.1  &1.0  \\ \hline
    \end{array}$
    
    \vspace{2.5mm}
    $\begin{array} { |c|c|c|c|c|}
    \hline
    k   &1   &2   &3    &\text{Total} \\ \hline
    p_K &0.3 &0.6 &0.1  &1.0 \\ \hline
    \end{array}$
    
    \vspace{2.5mm}
    $\begin{array} { |c|c|c|c|}
    \hline
    m &1 &2 &\text{Total}  \\ \hline
    p_M  &0.15 &0.85  &1.0 \\ \hline
    \end{array}$

\bigskip
\caption{Marginal Distributions} 
\label{tab:Marginals}

\end{table}
\end{document}
1
  • 1
    Thanks. Simpler is better! I added \newline in response to a similar post about spacing tables. But knowing as little as I do about LaTeX, I just slavishly followed the example. AND, thank you for the $$ hack. I would never have thought of putting the entire env. in mathmode.
    – TonyK
    Nov 29, 2023 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .