40

I have got codes like the following (generated by Kile). How can I add a caption. I guess I should have used the table environment. Is there any solution now?

\newcommand{\mc}[3]{\multicolumn{#1}{#2}{#3}}
\begin{center}
\begin{tabular}{rccll}
     &   & \mc{3}{c}{Colin}\\
     &   & a & \mc{1}{c}{b} & \mc{1}{c}{c}\\\cline{3-5}
Rose & \mc{1}{c|}{A} & \mc{1}{c|}{(1,2)} & \mc{1}{c|}{(2,5)} & \mc{1}{c|}{(4,4)}\\\cline{3-5}
     & \mc{1}{c|}{B} & \mc{1}{c|}{(7,4)} & \mc{1}{c|}{(3,5)} & \mc{1}{c|}{(0,6)}\\\cline{3-5}
\end{tabular}
\end{center}

3 Answers 3

48

If you don't want to use a table environment, then you could use

\captionof{table}{Your caption here} 

from the caption package.

\documentclass{article}
\usepackage{caption}
\newcommand{\mc}[3]{\multicolumn{#1}{#2}{#3}}

\begin{document}
\begin{center}
\captionof{table}{Your caption here}
\begin{tabular}{rccll}
   &   & \mc{3}{c}{Colin}\\
   &   & a & \mc{1}{c}{b} & \mc{1}{c}{c}\\\cline{3-5}
Rose & \mc{1}{c|}{A} & \mc{1}{c|}{(1,2)} & \mc{1}{c|}{(2,5)} & \mc{1}   {c|}{(4,4)}\\\cline{3-5}
   & \mc{1}{c|}{B} & \mc{1}{c|}{(7,4)} & \mc{1}{c|}{(3,5)} & \mc{1}{c|}{(0,6)}  \\\cline{3-5}
\end{tabular}
\end{center}

 \end{document}

Of course, using this approach means that your tables do not float- depending on the size of your document, and how many tables & figures it contains, this could be a serious issue.

1
  • 3
    I didn't want to include this in my answer, as it is more of an opinion than an answer, but I would highly recommend that you allow your tables (and figures) to float. Floats allow LaTeX to form beautiful pages. Without floats you can get very ugly page breaks.
    – cmhughes
    Commented Oct 16, 2011 at 17:40
22

In LaTeX, a caption is usually associated with a float (like table, figure, ...). And floats are meant to move within the document based on float-specifiers submitted by the user. For example,

\begin{table}[htbp]
  ...
\end{table}

Here the use of htbp provides LaTeX with a preference of where to place the float. First try "here", then try at the "top" of the page, then try at the "bottom" of the page, then try on a "page" of it's own (a so-called "page of floats"). More on this is explained in How to influence the position of float environments like figure and table in LaTeX?

Inserting a \caption{<your caption>} command within this environment implies that it will move with the float, always making sure that they stay together - this is important:

\begin{table}[ht]% Try here, and then top
  ...
  \caption{<your caption>}
\end{table}

You'll notice that the placement of the \caption{<your caption>} command is at the bottom of the table environment, meaning it will appear in that sequence in your output. If you want a caption at the top of the environment, put \caption{<your caption>} above the rest of the contents. The caption package provides some means to automate this placement.

If you're interested in referencing the caption (since it comes with an appropriate number, like Table 2.1), you need to put a \label{<your label>} after \caption{<your caption>} and refer to the table as Table~\ref{<your label>} in your text (compiling your document at least twice on this first go for this to work, of course).

However, if all of this is too much, and you don't care about referencing the table, or even something like a "List of Tables" (a table of contents for your tables), or even a table number, you could just do the following:

\noindent% Insures there's no paragraph indent
\begin{minipage}{\textwidth}% Minipage has width exactly the same as the text block
  \centering% Centers the contents of the minipage
  \begin{tabular}{<col spec>}
    %<tabular contents>
  \end{tabular}

  \medskip% Gives a medium skip between the tabular & caption (also try \smallskip or \bigskip)

  This is an interesting table.% Your caption goes here.
\end{minipage}

This places all the content (tabular and your caption) in a minipage environment of width \textwidth. The minipage will ensure that the contents remains in a fixed block (so that your caption doesn't end up on a page that your tabular is not).

1
  • very thorough answer, really helped me resolve several issues with my tables.
    – Reb.Cabin
    Commented May 25, 2015 at 12:40
15

I think the easiest way is just to wrap the {tabular} block inside a {table} block. The following example has a {tabular} truth table and {tabular} accuracy metrics wrapped inside a table which gives it both a caption and a label:

\begin{table}[h]
    \begin{tabular} {|c|c||>{\centering}p{.9cm}|>{\centering}p{.9cm}|>{\centering}p{.9cm}|c|}
        \hline
        $Truth$ & $Predicted$ & ADA & SVM & GLM & Blended \\ \hline
        T & T & 512 & 463 & 423 & 472 \\ 
        T & F & 19  & 68  & 108 & 0   \\ 
        F & T & 5   & 85  & 67  & 22  \\ 
        F & F & 580 & 500 & 518 & 98  \\ \hline
    \end{tabular} \\ \vskip .5cm

    \begin{tabular} {|>{\centering}p{2.9cm}||c|c|c|c|c|}
        \hline
        $Accuracy Metric$ & ADA & SVM & GLM & Blended \\ \hline
        Precision & 99.0\% & 84.5\% & 86.3\% & 95.5\% \\ 
        Recall    & 96.4\% & 87.2\% & 79.7\% & 100\%  \\
        Accuracy  & 97.8\% & 86.3\% & 84.3\% & 96.3\% \\ \hline
    \end{tabular} 

    \caption{Truth Tables and Accuracy Measures for each modeling library.}
    \label{tab:truthTables}   
\end{table}

Jake Drew www.jakemdrew.com

5
  • The first two answer suggest using table.
    – dustin
    Commented Apr 20, 2014 at 0:38
  • 3
    @Dustin, None of the examples given above show how to nest a {tabular} inside a {table} to gain the benefit of the table's caption and label feature. I don't believe they suggest this either? Rather they suggest using a {table} in place of a {tabular}. If they are in fact suggesting a nested {tabular}, it is very subtle, and I think an actual example certainly adds value to the question.
    – Jake Drew
    Commented Apr 20, 2014 at 4:45
  • @JakeDrew this places the table at the end and not where I want it to be
    – girl101
    Commented Aug 13, 2015 at 9:26
  • Try changing the "h" in "\begin{table}[h]" to a value that places the table in a more desire able location. See table documentation for valid values and placements.
    – Jake Drew
    Commented Aug 13, 2015 at 16:04
  • sharelatex documentation suggests this way! sharelatex.com/learn/Tables#Captions.2C_labels_and_references
    – deps_stats
    Commented Dec 3, 2015 at 0:02

You must log in to answer this question.

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