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

share|improve this question
    
You might check Martin Scharrer's reply on LaTeX table capabilities (Merge cells operation spanning multiple rows and columns, respecting borders) –  ipavlic Mar 29 '11 at 9:11
    
@ipavlic: Thanks for adding the link. I was planning to add it by myself after seeing the question title. I took the liberty to change the displayed link URL to the link title (see Can we have some linking etiquette and guidelines? on meta). –  Martin Scharrer Mar 29 '11 at 10:33
add comment

1 Answer

up vote 7 down vote accepted

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}
share|improve this answer
    
Why use a figure environment for a table? Edit: Sorry, I see that the OP has used this as well. –  Torbjørn T. Mar 29 '11 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)? –  Joseph Sturtevant Mar 29 '11 at 14:26
    
Since it's in a p-column, you can just use centering. –  Michel Mar 29 '11 at 14:30
add comment

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.