4

I'm trying to display a fairly complex table using Latex. Here is what I want it to look like (formatting, alignment, borders, columnspan, and rowspan). I would also like the columns to be fixed width (or at least have all the columns after the first one be the same width). Here is what I want it to look like:

Table-Goal

Here is what I have so far:

Table-Current

And here is the code for it:

\begin{figure}[H]
\centerline{
{\small
\begin{tabular}{|p{1in}|p{.5in}|p{.5in}|p{.5in}|p{.5in}|}
\hline
\multirow{3}{*}{\textbf{A}} & \multicolumn{2}{c}{\textbf{B}} & \multicolumn{2}{c|}{\textbf{C}} \\
& Wrap Text 1 & Wrap Text 2 & Wrap Text 1 & Wrap Text 2 \\
\hline
\textbf{Heading 1}  &  1 &  2 &  3 &  4 \\
\hline
\textbf{Heading 2}  &  2 &  3 &  4 &  5 \\
\hline
\textbf{Heading 3}  &  3 &  4 &  5 &  6 \\
\hline
\textbf{Total}      &  6 &  9 & 12 & 15 \\
\hline
\end{tabular}
}
}
\caption{Still Not Working}
\end{figure}

I'm pretty stuck at this point. Whenever I fix one issue it seems to make it impossible to fix the others. Any help would be greatly appreciated.

2

1 Answer 1

7

Replicating your image:

\begin{figure}[H]
    \centerline{
    {\small
    \begin{tabular}{|p{1in}|p{.5in}|p{.5in}|p{.5in}|p{.5in}|}
        \hline
        \multirow{3}{*}{\textbf{A}} & 
        \multicolumn{2}{c}{\textbf{B}} &
        \multicolumn{2}{|c|}{\textbf{C}} \\
        & 
        \multicolumn{1}{p{1in}}{Wrap Text 1} & 
        \multicolumn{1}{p{1in}}{Wrap Text 2} &
        \multicolumn{1}{|p{1in}}{Wrap Text 1} &
        \multicolumn{1}{p{1in}|}{Wrap Text 1}  \\
        \hline
        \textbf{Heading 1}  &  1 &  2 &  3 &  4 \\
        \hline
        \textbf{Heading 2}  &  2 &  3 &  4 &  5 \\
        \hline
        \textbf{Heading 3}  &  3 &  4 &  5 &  6 \\
        \hline
        \textbf{Total}      &  6 &  9 & 12 & 15 \\
        \hline
    \end{tabular}
    }
    }
    \caption{Working, but with many rules}
\end{figure}

The clue is to check the |'s in the multicolumn format specifiers carefully, to fix any missing rules.

However, I prefer the layout below, it contains no vertical rules, and less horizontal rules. In general, I find it to be preferable to use as few rules as possible in tables. The example uses the booktabs package, for better-looking rules in tables.

\begin{figure}[H]
    \centerline{
    {\small
    \begin{tabular}{p{1in}rrrr}
        \toprule
        & \multicolumn{2}{c}{\bfseries{B}}
        & \multicolumn{2}{c}{\bfseries{C}} \\
        \cmidrule(r){2-3} \cmidrule(l){4-5}
        \bfseries{A}
        & \multicolumn{1}{p{1in}}{Wrap Text 1}
        & \multicolumn{1}{p{1in}}{Wrap Text 2}
        & \multicolumn{1}{p{1in}}{Wrap Text 1}
        & \multicolumn{1}{p{1in}}{Wrap Text 2}  \\ \midrule
        \textbf{Heading 1}  &  1 &  2 &  3 &  4 \\
        \textbf{Heading 2}  &  2 &  3 &  4 &  5 \\
        \textbf{Heading 3}  &  3 &  4 &  5 &  6 \\
        \textbf{Total}      &  6 &  9 & 12 & 15 \\
        \bottomrule
    \end{tabular}
    }
    }
    \caption{Working, with less rules}
\end{figure}
3
  • Why use a figure environment for a table? Edit: Sorry, I see that the OP has used this as well. Mar 29, 2011 at 10:05
  • That's great. Thanks! I notice, however, that the "Wrap Text 1/2" column headers are still aligned right. How would one change their alignment (say, to centered)? Mar 29, 2011 at 14:26
  • Since it's in a p-column, you can just use centering.
    – Michel
    Mar 29, 2011 at 14:30

You must log in to answer this question.

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