2

I'm using subcaption to layout two tables of different widths side by side, but I do not understand how I am supposed to know the width of the tables beforehand. What am I supposed to write in place of those *s?

\begin{table}[p]
    \centering
    \begin{subtable}{*}
        \begin{tabular}{rccc}
        \toprule
        & {\bf\small no}
        & {\bf\small informally}
        & {\bf\small formally} \\ \midrule
        {\bf\small beginners}       & {\bf 4} & 3 & 1 \\
        {\bf\small non-beginners}   & 0       & 0 & {\bf 12} \\
        {\bf\small total}           & 4       & 3 & {\bf 13} \\ \bottomrule
        \end{tabular}
        \caption{Q1: Have you studied music?}
    \end{subtable}
    \qquad
    \begin{subtable}{*}
        \begin{tabular}{ccc}
        \toprule
        {\bf\small no}
        & {\bf\small one}
        & {\bf\small more} \\ \midrule
        {\bf 6} &      2  & 0 \\
             1  & {\bf 6} & 5 \\
        {\bf 7} & {\bf 8} & 5 \\ \bottomrule
        \end{tabular}
        \caption{Q2: Do you play a musical instrument?}
    \end{subtable}
\end{table}
5

You can use \subcaptionbox instead which will have a width equal to the width of the content (unless modified through an optional argument):

\documentclass{article}
\usepackage{array}
\usepackage{subcaption}
\usepackage{booktabs}

\begin{document}

\begin{table}[p]
    \centering
    \subcaptionbox{Q1: Have you studied music?}{%
        \begin{tabular}{>{\bfseries\small}rccc}
        \toprule
        & \bfseries\small no
        & \bfseries\small informally
        & \bfseries\small formally \\ \midrule
        beginners       & \bfseries 4 & 3 & 1 \\
        non-beginners   & 0       & 0 & \bfseries 12 \\
        total           & 4       & 3 & \bfseries 13 \\ \bottomrule
        \end{tabular}}\qquad
    \subcaptionbox{Q2: Do you play a musical instrument?}{%
        \begin{tabular}{>{\bfseries\small}ccc}
        \toprule
        no
        & \bfseries\small one
        & \bfseries\small more \\ \midrule
        6 &      2  & 0 \\
             1  & \bfseries 6 & 5 \\
        7 & \bfseries 8 & 5 \\ \bottomrule
        \end{tabular}}
\end{table}

\end{document}

The result:

enter image description here

Remarks

  • \bf is obsolete, use \bfseries instead.
  • Since each cell forms a group, there's no need for explicit grouping.
  • Using the >{...} syntax from the array package you can use >{\bfseries\small} to apply those settings for a whole column.

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.