[Tex/LaTex] Having a table in an enumerate environment

floatsliststables

I have a problem trying to add a table in an enumerate environment in LaTeX.

I want the table to act as if it were just another piece of text. i.e. showing up exactly right next to the \item in an enumerate environment.

I currently use this to create my table (M is to have a 'math' table):

\begin{enumerate}[(a)]
  \item
  \begin{enumerate}[i.]
  \item
  \begin{table}[h!]
    \begin{tabular}{M M|M M M M M M M}
        \hline
        p & q & (p & \rightarrow & q) & \rightarrow & (q & \rightarrow & p) \\ \hline
        0 & 0 & ~ & 1 & ~ & 1 & ~ & 1 & ~ \\ 
        0 & 1 & ~ & 1 & ~ & 0 & ~ & 0 & ~ \\
        1 & 0 & ~ & 0 & ~ & 1 & ~ & 1 & ~ \\  
        1 & 1 & ~ & 1 & ~ & 1 & ~ & 1 & ~ \\ 
        \hline
        ~ & ~ & ~ & ~ & ~ & \uparrow & ~ & ~ & ~ \\ 
    \end{tabular}
  \end{table}
  \end{enumerate}
\end{enumerate}

But when the code is compiled the table shows up outside of the enumeration.

Best Answer

The table environment is merely a placeholder for a float using the table counter. However, it can contain anything - not necessarilty a tabular (even an image is fine, really). In this instance though, you don't need to let the tabular float, so you should use it as-is:

enter image description here

\documentclass{article}
\usepackage{enumerate}% http://ctan.org/pkg/enumerate
\usepackage{array}% http://ctan.org/pkg/array
\newcolumntype{M}{>{$}c<{$}}
\begin{document}
\begin{enumerate}[(a)]
  \item
  \begin{enumerate}[i.]
  \item
    \begin{tabular}{M M|M M M}
        \hline
        p & q & (p \rightarrow q) & \rightarrow & (q \rightarrow p) \\ \hline
        0 & 0 & 1 & 1 & 1 \\ 
        0 & 1 & 1 & 0 & 0 \\
        1 & 0 & 0 & 1 & 1 \\  
        1 & 1 & 1 & 1 & 1 \\ 
        \hline
         & \multicolumn{1}{c}{} & & \uparrow & \\ 
    \end{tabular}
  \end{enumerate}
\end{enumerate}
\end{document}​

You don't really need the array package here, since you can post the entire tabular as a $\begin{array}{cc|ccc}...\end{array}$ and obtain the same result.

Other packages you might consider include enumitem for list management and booktabs for publishing-quality table constructions. Here's a take using the above suggestions:

enter image description here

\documentclass{article}
\usepackage{enumitem}% http://ctan.org/pkg/enumitem
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\begin{document}
\begin{enumerate}[label=(\alph*)]
  \item
  \begin{enumerate}[label=\roman*.]
  \item
    $\begin{array}{c c c c c}
        \toprule
        p & q & (p \rightarrow q) & \rightarrow & (q \rightarrow p) \\ \midrule
        0 & 0 & 1 & 1 & 1 \\ 
        0 & 1 & 1 & 0 & 0 \\
        1 & 0 & 0 & 1 & 1 \\  
        1 & 1 & 1 & 1 & 1 \\ 
        \bottomrule
         & \multicolumn{1}{c}{} & & \uparrow &
    \end{array}$
  \end{enumerate}
\end{enumerate}
\end{document}​

Vertical alignment is also adjustable using a [t] or [b] optional argument to either tabular or array. This would align the structure to either the top or the bottom line/row.