223

In a table, how can I draw a horizontal line that goes only through some of the cells? I mean a line like the one in the Foo and in the Bar area of this table:

+----+-----+
|Foo |1 |2 |
|    |1 |2 |
|    +--+--+
|    |1 |2 |
|    |1 |2 |
+----+-----+
|Bar |1 |2 |
|    |1 |2 |
|    +--+--+
|    |1 |2 |
|    |1 |2 |
+----+--+--+
1

6 Answers 6

285

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

5
  • \multirow cannot accept graphics. Commented Jan 9, 2011 at 10:56
  • @xport: Why do you say that? I have no problems when replacing Foo by \includegraphics[height=4em]{image} inside the \multirow in the code above. Commented Jan 9, 2011 at 20:34
  • are you sure the image will be vertically centered? :-) Commented Jan 9, 2011 at 20:36
  • 4
    @xport: with a \raisebox{-\tabcolsep}{\includegraphics[height=4em]{image}}, it will be vertically centered, yes. Commented Jan 9, 2011 at 20:58
  • I see that \cmidrule{2-3} is another option which puts a bit more pleasing space between the lines.
    – Arun Das
    Commented Nov 5, 2021 at 1:52
58

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

4
  • doesn't work with longtable, i guess. Commented May 31, 2013 at 14:56
  • 1
    @VineetMenon A quick check makes me think it does work. Also, the booktabs documentation explicitly mentions compatibility with longtable and provides some hints in section 5. Have a look at that, and if you still can’t work something out, feel free to ask a new question.
    – doncherry
    Commented May 31, 2013 at 18:45
  • oh. my bad, there was a glitch in my source. It's working now. thanks Commented Jun 1, 2013 at 8:28
  • 2
    Can we do that for non consecutive entries? I mean instead of \cmidrule{2-3}, to have \cmidrule{2, 4 and 5} in a bigger table? ANSWER: \cmidrule{2-2}\cmidrule{4-5}
    – gsamaras
    Commented Mar 5, 2015 at 15:15
26

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}
1
  • How can you extend this such that you would have Foo Foo Foo Foo per each row?
    – Athaeneus
    Commented May 1 at 14:16
4

You may also try tabularray package. With tblr environment you can use \cline command to draw a colorful partial horizontal line. Furthermore, hlines can not cross multirow cells in any case, so you can simply write \hline instead of \cline in this example:

\documentclass{article}

\usepackage{tabularray}
\usepackage{xcolor}

\begin{document}

Using \verb!\cline!:
\begin{tblr}{|c|cc|}
\hline
\SetCell[r=4]{c} Foo & 1 & 2 \\
    & 1 & 2 \\
\cline[blue3]{2-3}
    & 1 & 2 \\
    & 1 & 2 \\
\hline
\SetCell[r=4]{c} Bar & 1 & 2 \\
    & 1 & 2 \\
\cline[purple3]{2-3}
    & 1 & 2 \\
    & 1 & 2 \\
\hline
\end{tblr}

\bigskip

Using \verb!\hline!: 
\begin{tblr}{|c|cc|}
\hline
\SetCell[r=4]{c} Foo & 1 & 2 \\
    & 1 & 2 \\
\hline[blue3]
    & 1 & 2 \\
    & 1 & 2 \\
\hline
\SetCell[r=4]{c} Bar & 1 & 2 \\
    & 1 & 2 \\
\hline[purple3]
    & 1 & 2 \\
    & 1 & 2 \\
\hline
\end{tblr}

\end{document}

enter image description here

1
  • Will rowcolor{} also not cross \multirow cells ?
    – RichWalt
    Commented Oct 30, 2021 at 10:08
2

You can do that tabular easily with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{|c|cc|}
\Hline
\Block{4-1}{Foo} & 1 & 2 \\
    & 1 & 2 \\
\Hline
    & 1 & 2 \\
    & 1 & 2 \\
\Hline
\Block{4-1}{Bar} & 1 & 2 \\
    & 1 & 2 \\
\Hline
    & 1 & 2 \\
    & 1 & 2 \\
\Hline
\end{NiceTabular}

\end{document}

Output of the above code

1

I want to add just for completion:

If you want several partial lines within one row (and possibly one for only one column), then \cline{1-1}\cline{3-5} would be the correct solution. \cline{1}\cline{3-5} does not work.

You must log in to answer this question.

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