\table in \enumerate environment in latex

hellfragger picture hellfragger · Aug 20, 2015 · Viewed 8.4k times · Source

I want to insert a \table in \enumerate environment and I have something like this:

\begin{enumerate}
    \item Item 1.
    \item Item 2.
    \item \begin{table}[htbp]
        \textbf{\caption{Table1 Caption}}
        \centering  
        \begin{tabular}{c c}
        \hline\hline
        Value 1 & Value 2\\
        \hline
        r1c1 & r2c2\\
        r2c1 & r2c2\\
        r3c1 & r3c2\\           
        \hline
        \end{tabular}
        \label{table1}
        \end{table}

    \item \begin{table}[htbp]
        \textbf{\caption{Table 2 Caption}}
        \centering
        \begin{tabular}{ccc}
        \hline\hline
        Value 1 & Value 2 & Value 3\\
        \hline
        r1c1 & r1c2 & r1c3\\
        r2c1 & r2c2 & r2c3\\
        r3c1 & r3c2 & r3c3\\
        \end{tabular}
        \label{table2}
    \end{table}

    \item \ref{table1} and \ref{table2}
\end{enumerate}

But when I compile the latex document the \enumerate number are no where near the table. Also when I refer to label "table1" and "table2" it shows up as 3 and 4 respectively (for extra info this part is in subsection 3.3 and these are the only two tables in the whole document).

How do I use \table environment with \enumerate environment. I have seen people use just \tabular with \enumerate but I want to use \table as it gives me an easy option to define \caption and \label.

Also regarding table labeling I am assuming it has something to do with subsection number but I can't really figure it out.

I would really appreciate any help regarding this matter.

Answer

Roland Smith picture Roland Smith · Aug 20, 2015

A table environment is a float. It is not meant to be included in other environments.

You can use a tabular environment like this:

\documentclass[a4paper, 11pt]{article}

\begin{document}

\begin{enumerate}
    \item Item 1.
    \item Item 2.
    \item  \begin{tabular}[t]{c c}
        Value 1 & Value 2\\
        \hline
        r1c1 & r2c2\\
        r2c1 & r2c2\\
        r3c1 & r3c2\\
        \hline
    \end{tabular}

\item \begin{tabular}[t]{ccc}
        Value 1 & Value 2 & Value 3\\
        \hline
        r1c1 & r1c2 & r1c3\\
        r2c1 & r2c2 & r2c3\\
        r3c1 & r3c2 & r3c3\\
    \end{tabular}
\end{enumerate}

\end{document}

The output will look like this:

result from example code

Note that the vertical alignment will look weird if you try tp put \hline on top of the table.

Edit 1: You can put a \label almost everywhere you like, but it will not refer to itself as a "Table" unless a variable is set. You'd have to delve into the bowels of LaTeX to see how that is done.

There are at least two packages (capt-of and caption) that provide a \captionof command to define a caption outside a figure or table environment.