How can I draw a horizontal line spanning only some of the table cells?

Instead of nested tables as xport_is_sleeping suggested, you can also use \cline{2-3} which draws a partial line starting on column 2 and ending on column 3 and use \multirow to center the words Foo and Bar on the lines if that's what you want:

alt text

\documentclass{article}
\usepackage{array,multirow}
\begin{document}
\begin{tabular}{|c|cc|}\hline
\multirow{4}{*}{Foo} & 1 & 2 \\
    & 1 & 2 \\\cline{2-3}
    & 1 & 2 \\
    & 1 & 2 \\\hline
\multirow{4}{*}{Bar} & 1 & 2 \\
    & 1 & 2 \\\cline{2-3}
    & 1 & 2 \\
    & 1 & 2 \\\hline
\end{tabular}
\end{document}

When making tables, you should also always use the array package which offers various additional features and improvements (most notably at the vertical and horizontal lines joins).


Here's a solution using booktabs and getting rid of of the vertical rules, which leads to a prettier result imho. Have a look at the introduction of the booktabs documentation, which offers some insights in basic table design.

The benefits of booktabs are: Much better table spacing, and \toprule, \midrule and \bottomrule, where \midrule is thinner than the other two, which looks nice.

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{table}\centering
\begin{tabular}{lll}
\toprule
Foo & 1 & 2 \\
    & 1 & 2 \\\cmidrule{2-3}% That's the rule you're looking for.
    & 1 & 2 \\
    & 1 & 2 \\
\midrule
Bar & 1 & 2 \\
    & 1 & 2 \\\cmidrule{2-3}% This too. The numbers designate the columns covered.
    & 1 & 2 \\
    & 1 & 2 \\
\bottomrule
\end{tabular}
\caption{1 and 2 in relation to Foo and Bar.}
\end{table}

\end{document}

output


Nested table can solve this beautifully.

alt text


\documentclass{article}
\begin{document}
\begin{tabular}{|c|@{}c@{}|}\hline
Foo
&
\begin{tabular}{cc}
1 & 2 \\
1 & 2 \\\hline
1 & 2 \\
1 & 2 \\
\end{tabular}
\tabularnewline\hline
Bar
&
\begin{tabular}{cc}
1 & 2 \\
1 & 2 \\\hline
1 & 2 \\
1 & 2 \\
\end{tabular}
\tabularnewline\hline
\end{tabular}

\end{document}

Tags:

Rules

Tables