5

I realise that the booktabs package does not want me to use vertical lines in my tables. However, I like vertical lines in my tables. Therefore, I am hoping there is a workaround to a problem that booktabs clearly does not think should even be an issue.

Here is my table:

Table with borken up vertical lines

Is it possible to join all the vertical lines?

Here is the code:

\begin{table}[h]
\begin{centering}
\scalebox{.8}
\small
{\begin{tabular}{| c | l | c c c |}
\hline
{\bf HD Level}  &           &   &   &   \\
\midrule[2pt]
\multirow{2}*{1} & {\it Rep-tile}:  & $\{1,2\}$ & $\{1,3\}$ & $\{2,3\}$ \\
\cmidrule{2-5}
        & {\it Encoding}:\ \ \ \    & 1     & 2         & 3 \\
\midrule[2pt]
\multirow{2}*{2} & {\it Rep-tile}:  & $\{2\}$   & $\{3\}$   & \\
\cmidrule{2-5}
        & {\it Encoding}:   & 1     & 2     & \\
\hline
\end{tabular}}
\caption{\small\bf Bla bla}
\label{tab:SMuTexEncoding}
\end{centering}
\end{table}

Many thanks.

13
  • 5
    That's the nature of the beast... when using booktabs, you need to consider reading the documentation as well. It states: "You will not go far wrong if you remember two simple guidelines at all times: 1. Never, ever use vertical rules. 2. Never use double rules."
    – Werner
    Oct 16, 2014 at 16:48
  • 2
    I did @Werner (and implied as much at the beginning of my question)! But what if I happen to disagree? Perhaps there is another package you can suggest? I need the \cmidrule{2-5} function...
    – pdini
    Oct 16, 2014 at 16:49
  • 2
    You may disagree, of course, but then you can't use booktabs. I invite you to look at my realization of your table in this picture. I would probably remove the \cmidrule{2-5} commands too. The superheavy rules are out of the question.
    – egreg
    Oct 16, 2014 at 16:55
  • 2
    You don't have to use booktabs. Basic LaTeX provides cline. Please note, that \it is deprecated for about 20 years now.
    – Johannes_B
    Oct 16, 2014 at 17:16
  • 2
    Just use a standard \cline if you are using vertical rules. Also \scalebox{.8}\small makes a box with just the argument \small which is odd?? Oct 16, 2014 at 17:36

3 Answers 3

4

You can have thick \hlines with the makecell package and its \Xhline{width} and \Xcline{col1-col2}{width} commands. You can replace the vertical spacing added by booktabs around horizontal rules (that's responsible for non joining vertical rules) with the cellspace package; it lets you define minimal vertical spacingabove and below cells of a given column, prefixing its specifier with the letter S.

I also took the opportunity to define a set command, based on an example in the documentation of the mathtools package, that produces better horizontal spacing and variable-sized braces

\documentclass{article}
\usepackage{makecell}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\usepackage{mathtools}
\DeclarePairedDelimiterX\set[1]\{\}{\nonscript\,#1\nonscript\,}

\begin{document}

\begin{table}[!htb]
\centering
\small
\begin{tabular}{| Sc | >{\itshape}l | Sc c c |}
\hline
{\bfseries HD Level} & & & & \\
\Xhline{2pt}
\multirow{2}{*}{1} & Rep-tile: & $\set{1,2}$ & $\set{1,3}$ & $\set{2,3}$ \\
\cline{2-5}
        & Encoding:\ \ \ \ & 1 & 2 & 3 \\
\Xhline{2pt}
\multirow{2}{*}{2} & Rep-tile: & $\set{2}$ & $\set{3}$ & \\
\cline{2-5}
        & Encoding: & 1 & 2 & \\
\hline
\end{tabular}
\caption{\small\bf Bla bla}
\label{tab:SMuTexEncoding}
\end{table}

\end{document}

enter image description here

If you want to have coloured rules, it's more complelex: the \arrayrulecolor command (from the colortbl package) doesn't work. A workaround consistsin using the hhline package, setting \arrayrulewidth to 0pt and colouring the interrule space of a double rule. Demo:

\begin{table}[!htb]
\centering
\small\arrayrulecolor{red}
\begin{tabular}{| Sc | >{\itshape}l | Sc c c |}
\hline
{\bfseries HD Level} & & & & \\
\Xhline{2pt}
\multirow{2}{*}{1} & Rep-tile: & $\set{1,2}$ & $\set{1,3}$ & $\set{2,3}$ \\
\cline{2-5}
        & Encoding:\ \ \ \ & 1 & 2 & 3 \\
\Xhline{2pt}
\multirow{2}{*}{2} & Rep-tile: & $\set{2}$ & $\set{3}$ & \\
\cline{2-5}
        & Encoding: & 1 & 2 & \\
\hhline{>{\arrayrulewidth = 0pt\doublerulesep = 2pt \doublerulesepcolor{red}}=====}
\end{tabular}
\caption{\small\bf Bla bla}
\label{tab:SMuTexEncoding}
\end{table}

enter image description here

5
  • Very nice, that's what I was looking for. Thank you.
    – pdini
    Oct 16, 2014 at 21:44
  • @pdini: Just in case, note it doesn't work for coloured rules. A workaround consists in using hhline. You can take a look at my updated answer.
    – Bernard
    Oct 16, 2014 at 22:08
  • 1
    Needed to say, I would reject both these tables as a copy editor. But I know, it's not the question here. :(
    – yo'
    Oct 17, 2014 at 7:45
  • @tohecz. I agree with. The second one is only to illustrate the problem of \Xh/cline with colour. However, I've some special cases where a table required vertical and horizontal lines — I mean they were meaningful.
    – Bernard
    Oct 17, 2014 at 8:12
  • Thanks @Bernard, that looks very nice too. Good to know I can create coloured tables.
    – pdini
    Oct 17, 2014 at 9:32
6

I would never do what I did in the first table, but it is your choice, not mine. I just offer a much nicer alternative. However, please:

  • Do not use \it and \bf: Does it matter if I use \textit or \it, \bfseries or \bf, etc
  • Do not manually modify the size of the table nor the style of the caption, it's not semantic. What if you later decide that the captions should not be bold, but you've already add \bfseries to 50 captions? Are you going to remove them by hand? Well, you'll have to. Or you use packages that modify the styles globally. (Both things can be found on this website, however, simply look into the documentation of packages caption and float.)
  • Do not specify [h] float placement alone, you can expect bad things happening.
  • centering is not an environment, it is a command: \centering.
  • Your \scalebox has no effect whatsoever since the thing you scale is the macro \small, which doesn't output anything. Use either \scalebox or \small (or rather none of them).

\documentclass{article}

\usepackage{array}
\newcolumntype{C}{>{\rule[-1.5ex]{0pt}{4.5ex}}c}

\makeatletter
\def\Hline{% a thicker \hline
  \noalign{\ifnum0=`}\fi\hrule \@height 1pt \futurelet
   \reserved@a\@xhline}
\makeatother

\usepackage{booktabs}
\newcommand\Midrule{\midrule[\heavyrulewidth]}

\begin{document}

\begin{table}
\centering
\begin{tabular}{| c | l @{\qquad} | c c C |}
\Hline
  \bfseries HD Level  &&&&\\
\hline
  1 & \itshape Rep-tile:  & $\{1,2\}$ & $\{1,3\}$ & $\{2,3\}$ \\
\cline{2-5}
    & \itshape Encoding: & 1       & 2       & 3 \\
\Hline
  2 & \itshape Rep-tile: & $\{2\}$ & $\{3\}$ & \\
\cline{2-5}
    & \itshape Encoding: & 1       & 2       & \\
\Hline
\end{tabular}
\caption{Bla bla}
\label{tab:SMuTexEncoding}
\end{table}


\begin{table}
\centering
\begin{tabular}{ c >{\itshape}l c c c }\toprule
\bfseries HD Level\\\Midrule
1 & Rep-tile: & $\{1,2\}$ & $\{1,3\}$ & $\{2,3\}$ \\\cmidrule{2-5}
  & Encoding: & 1         & 2         & 3 \\\Midrule
2 & Rep-tile: & $\{2\}$   & $\{3\}$   & \\\cmidrule{2-5}
  & Encoding: & 1         & 2         & \\\bottomrule
\end{tabular}
\caption{Bla bla}
\label{tab:SMuTexEncoding}
\end{table}

\end{document}
3
  • Thanks very much @tohecz. I guess you can probably date me from my coding 'style': i started using TeX in 1986, and never quite got used to LaTeX! I will keep in mind the other points you made, although I don't quite understand them (what's so bad about \bf? etc). I was able to do a table without vertical lines too, and using the multirow function, and I have to admit I am getting used to the look. It's nicer than the one you kindly put together for me and have very clearly disowned :)
    – pdini
    Oct 16, 2014 at 19:40
  • I see! Interesting. I had in fact wondered why it was not possible to get bold italicised text. I'll start using the proper commands then. Thanks @tohecz
    – pdini
    Oct 17, 2014 at 9:30
1

If you actually want vertical rules compatible with the rules of booktabs, you should use {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix,booktabs,caption}

\begin{document}

\begin{table}[h]
\centering
\small
\begin{NiceTabular}{| c | >{\itshape}l | c c c |}
\hline
\bfseries HD Level  & &   &   &   \\
\midrule[2pt]
\Block{2-1}{1} & Rep-tile  & $\{1,2\}$ & $\{1,3\}$ & $\{2,3\}$ \\
\cmidrule{2-5}
               & Encoding  & 1         & 2         & 3 \\
\midrule[2pt]
\Block{2-1}{2} & Rep-tile  & $\{2\}$   & $\{3\}$   & \\
\cmidrule{2-5}
               & Encoding  & 1         & 2         & \\
\hline
\end{NiceTabular}
\caption{\small\bfseries Bla bla}
\label{tab:SMuTexEncoding}
\end{table}

\end{document}

Output of the above code

1
  • Thank you, @F. Pantigny, I'll keep that in mind.
    – pdini
    Apr 24, 2021 at 15:36

You must log in to answer this question.

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