3

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
Header 1 & Header 2 & Header 3 \\
\midrule
Blablabla & Bliblablabla & Blublasdad \\
\bottomrule
\end{tabular}
\end{subfigure}

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

\end{table}

\end{document}

Which gives me this:

Resulting output

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?

2 Answers 2

3

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.

screenshot

\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
        \begin{tabular}{|C{\widthof{Blablabla}}|C{\widthof{Bliblablabla}} |C{\widthof{Blublasdad}}|}
            \toprule
            Header 1  & Header 2     & Header 3   \\
            \midrule
            Blablabla & Bliblablabla & Blublasdad \\
            \bottomrule
        \end{tabular}
    \end{subfigure}

    \begin{subfigure}{\linewidth}
        \caption{Table 2}
        %Table 2
        \begin{tabular}{|C{\widthof{Blablabla}}|C{\widthof{Bliblablabla}} |C{\widthof{Blublasdad}}|}
            \toprule
            Header 1 & Header 2 & Header 3 \\
            \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?

2
  • 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, 2012 at 23:45
  • I'm working on a pgfplotstable idea...
    – cmhughes
    Dec 3, 2012 at 1:15
3

Probably I would just choose the widths in advance as in

enter image description here

\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
Header 1 & Header 2 & Header 3 \\
\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
Header 1 & Header 2 & Header 3 \\
\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:

enter image description here

\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
Header 1 & Header 2 & Header 3 \\
\midrule
Blablabla & Bliblablabla & Blublasdad \\
\bottomrule
\multicolumn{3}{c}{%
\makebox[0pt][c]{\parbox{\textwidth}{%
\bigskip
\subcaption{Table 2}
}}}\\
\toprule
Header 1 & Header 2 & Header 3 \\
\midrule
A & B & C\\
\bottomrule
\end{tabular}


\end{table}

\end{document}

You must log in to answer this question.

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