[Tex/LaTex] Formatting caption so it doesn’t run off the table width

captionstables

I created the following table, which showed up nicely in the document on its own:

    \begin{table}[!ht]
    \begin{tabular}{cccc}
    \caption{MyCaption}

    --data goes here--

    \end{tabular}
    \end{table}

However, after I integrated it within my document, it completely runs off the right side of the page. How do I get it to keep its size?

Edit: it's a very small table, 4 columns, so the size isn't the problem.

\documentclass[12pt]{report}
\usepackage{thesis}
\usepackage{apacite}
\usepackage{url}
\usepackage{graphicx}

\begin{document}
%\input{MyChapter}

\begin{table}[!ht]
\caption{Performance scores from assessments of divergent and convergent thinking tests of creativity and non-creativity (control task).}

\begin{tabular}{c{3cm}c{1cm}c{1cm}c{1cm}}
\hline
Variable & \textit{N} & \textit{M} & \textit{SD}\\
\hline
XYZ & \hfill & \hfill & \hfill\\
XXX & 17 & 222 & 2222\\
XXX & 22 & 111 & 11\\
XXX & 22 & 111 & 111\\
XXX & 22 & 111 & 111\\
XYZ & \hfill & \hfill & \hfill\\
XXX & 111 &  & 12\\
XXX & 111 & 2 & 1\\
\hline
\label{table:1}
\end{tabular}
\end{table}

\end{document}

I think it might be that the caption is too long. How do I get it to stay the same width as the table?

Best Answer

I suspect that you looking something like this:

enter image description here

Above image of table is generated by:

\documentclass[12pt]{report}
\usepackage{threeparttable}
\usepackage{showframe}

\begin{document}

\begin{table}[htbp]
    \centering
\begin{threeparttable}
    \caption{Performance scores from assessments of divergent and convergent thinking tests of creativity and non-creativity (control task).}
\label{table:1}
    \begin{tabular}{p{3cm}p{1cm}p{1cm}p{1cm}}
    \hline
Variable    & \textit{N} & \textit{M} & \textit{SD}\\
    \hline
XYZ         &           &           &           \\
XXX         & 17        & 222       & 2222      \\
XXX         & 22        & 111       & 11        \\
XXX         & 22        & 111       & 111       \\
XXX         & 22        & 111       & 111       \\
XYZ         &           &           &           \\
XXX         & 111       &           & 12        \\
XXX         & 111       & 2         & 1         \\
    \hline
    \end{tabular}
\end{threeparttable}
\end{table}
\end{document}

In my MWE I use package showframe only for showing page layout. In real use it had to be removed.

To force caption to be the same width as table, I use package threeparttable. It enables to include caption in table. By this the caption is nod wider than table. Additionally, you can add to table list of (foot)notes, if you have them in table.

In my MWE I use p{...} column type instead unknown c. If you like to have centered cells contents, than you can define new column type, for examplec{...} as

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

I didn't figured out of purpose \hfill in many of your table cells, so I remove them.