# Align columns in multiple subfigure tables in figure environment

Here's my code:

\documentclass{article}
\usepackage{booktabs,caption,subcaption}
\begin{document}

\begin{table}[h!]
\caption{Main figure}

\begin{subfigure}{\linewidth}
\caption{Table 1}
%Table 1
\begin{tabular}{|c|c|c|}
\toprule
\midrule
Blablabla & Bliblablabla & Blublasdad \\
\bottomrule
\end{tabular}
\end{subfigure}

\begin{subfigure}{\linewidth}
\caption{Table 2}
%Table 2
\begin{tabular}{|c|c|c|}
\toprule
\midrule
A & B & C\\
\bottomrule
\end{tabular}
\end{subfigure}

\end{table}

\end{document}


Which gives me this:

The tables are of different widths. I would like for the columns in both tables to be aligned pair-wise with one another (i.e. "Header 3" column of table 1 is aligned with that of table 2), and have them all centered. How can I do this?

-

You could define a newcolumntype using the array package to make a centered column that accepts a width

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}


as demonstrated in Center column with specifying width in table (tabular enviroment)?

I've also used the calc package for the \widthof command.

\documentclass{article}
\usepackage{booktabs,caption,subcaption}
\usepackage{array}
\usepackage{calc}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}

\begin{table}[h!]
\caption{Main figure}
\begin{subfigure}{\linewidth}
\caption{Table 1}
%Table 1
\toprule
\midrule
Blablabla & Bliblablabla & Blublasdad \\
\bottomrule
\end{tabular}
\end{subfigure}

\begin{subfigure}{\linewidth}
\caption{Table 2}
%Table 2
\toprule
\midrule
A        & B        & C        \\
\bottomrule
\end{tabular}
\end{subfigure}
\end{table}

\end{document}


As a final note, the booktabs author (and a few folks on this site) would recommend against using vertical lines in your tabular environments- of course, this is personal preference, but for reference have a look at Why not use vertical lines ('|') in a tabular?

-
I used vertical lines to have the width of the columns be more clear visually. I don't display them in my document. Is there another way that doesn't involve me inputting the text in the \widthof function? I have many tables with many rows, and it's not a priori clear which row will have the largest element for a given column. I need something that takes the width of the column, as calculated automatically by the system. –  lodhb Dec 2 '12 at 23:45
I'm working on a pgfplotstable idea... –  cmhughes Dec 3 '12 at 1:15

Probably I would just choose the widths in advance as in

\documentclass{article}
\usepackage{array,booktabs,caption,subcaption}
\begin{document}

\begin{table}[htp!]
\caption{Main figure}

\begin{subfigure}{\linewidth}
\centering
\caption{Table 1}
%Table 1
\begin{tabular}{|*3{>{\centering\arraybackslash}p{.25\linewidth}|}}
\toprule
\midrule
Blablabla & Bliblablabla & Blublasdad \\
\bottomrule
\end{tabular}
\end{subfigure}

\bigskip

\begin{subfigure}{\linewidth}
\centering
\caption{Table 2}
%Table 2
\begin{tabular}{|*3{>{\centering\arraybackslash}p{.25\linewidth}|}}
\toprule
\midrule
A & B & C\\
\bottomrule
\end{tabular}
\end{subfigure}

\end{table}

\end{document}


However if you definitely want single line entries and the widths chosen dynamically thaen you can make it a single table:

\documentclass{article}
\usepackage{array,booktabs,caption,subcaption}
\begin{document}

\begin{table}[htp!]
\caption{Main figure}
\centering

%Table 1
\begin{tabular}{|*3{>{\centering\arraybackslash}p{.25\linewidth}|}}
\multicolumn{3}{c}{%
\makebox[0pt][c]{\parbox{\textwidth}{%
\subcaption{Table 1}
}}}\\
\toprule
\midrule
Blablabla & Bliblablabla & Blublasdad \\
\bottomrule
\multicolumn{3}{c}{%
\makebox[0pt][c]{\parbox{\textwidth}{%
\bigskip
\subcaption{Table 2}
}}}\\
\toprule