Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

3 Answers 3

up vote 15 down vote accepted

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.

share|improve this answer
    
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 Oct 16 '11 at 17:40

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).

share|improve this answer
    
very thorough answer, really helped me resolve several issues with my tables. –  Reb.Cabin May 25 at 12:40

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

share|improve this answer
    
The first two answer suggest using table. –  dustin Apr 20 '14 at 0:38
    
@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 Apr 20 '14 at 4:45
    
@JakeDrew this places the table at the end and not where I want it to be –  Rishika Aug 13 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 Aug 13 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.