3

I'm having trouble with my table and don't have any idea how to fix it. I just don't know why the lines do not meet... It looks like this:

enter image description here

I used the following code:

\documentclass{article}
\usepackage{tabular}
\begin{document}

\begin{table}[H]
    \centering
    \begin{adjustbox}{width=0.6\textwidth}
      \begin{tabular}{|l |l ||c |c|}
      \toprule
      \multicolumn{2}{|l||}{} & a & b\\
      \midrule
      \midrule
\multicolumn{2}{|l||}{c} &  d & e \\  
\hline
\multicolumn{2}{|l||}{f} 
 & g &  \\  
\hline
\multicolumn{2}{|l||}{h} 
 & i & j \\  
\hline
 & k & l & m \\  
\cline{2-4} 
n & o & p & q\\
\cline{2-4} 
 & r & s & t \\   
\hline 
 & u & v & w \\   
\cline{2-4} 
x & y & z & aa \\   
\cline{2-4} 
 & ab & ac & ad \\   
\hline  
 & ae & af & ag \\   
\cline{2-4} 
ah & ai & aj & ak \\   
\cline{2-4}  
 & al & am & an \\    
      \bottomrule 
      \end{tabular}
      \end{adjustbox}
\end{table} 

\end{document}

I'm sure it can't be that hard, although for me, it is. So thanks for any helping comments! :)

  • 3
    booktabs is designed for not using vertical rules, and doesn't work well with them. If you really need the vertical rules, the simples fix is to use \hline instead of the rules from booktabs (\toprule,\midrule,\bottomrule). – Torbjørn T. Nov 30 '16 at 12:38
6

If you want to use vertical rules in a tabular environment, don't use the rule-drawing macros of the booktabs package. If you do have to use vertical rules and hence can't use \toprule, \midrule, etc but still want the well-spaced appearance of horizontal rules that the booktabs macros provide, I suggest you resort to (typographic) struts. That way, the vertical lines will still be continuous.

The following example implements this idea, using code I gleaned from an article written by Claudio Beccari that was published in TeX & TUG News in 1993. (Yes, I really have been using TeX and LaTeX that long...) Use \Tstrut to create extra space to the line above, \Bstrut for extra space to the line below, and \TBstrut ("top and bottom strut") for more spacing to both the lines above and below the macro's insertion. In the code, notice the \TBstrut in the first line, the \Tstrut in the second line, and the \Bstrut in the bottom line of the tabular.

enter image description here

\documentclass{article}
\usepackage{array}
%% code from article by Claudio Beccari in TeX&TUG News, 1993
\newcommand\Tstrut{\rule{0pt}{2.9ex}}      % "top" strut
\newcommand\Bstrut{\rule[-1.2ex]{0pt}{0pt}}% "bottom" strut
\newcommand\TBstrut{\Tstrut\Bstrut}    % "top and bottom" strut

\begin{document}

\begin{table}
\centering
\begin{tabular}{|l |l ||c |c|}
\hline
\multicolumn{2}{|l||}{}  &  a & b\TBstrut\\
\hline
\multicolumn{2}{|l||}{c} &  d & e\Tstrut \\
\hline
\multicolumn{2}{|l||}{f}
 & g &  \\
\hline
\multicolumn{2}{|l||}{h}
 & i & j \\
\hline
 & k & l & m \\
\cline{2-4}
n & o & p & q\\
\cline{2-4}
 & r & s & t \\
\hline
 & u & v & w \\
\cline{2-4}
x & y & z & aa \\
\cline{2-4}
 & ab & ac & ad \\
\hline
 & ae & af & ag \\
\cline{2-4}
ah & ai & aj & ak \\
\cline{2-4}
 & al & am & an\Bstrut \\
\hline
\end{tabular}
\end{table}

\end{document}
2

I assume, that you like to have more vertical space above/below cells content and also like to have vertical rules. This combination, is clearly explained in Mico's answer, is not possible by use of ˙booktabs` rules. Beside solution proposed by Mico also exist packages, which can add more vertical space without use of struts:

  • mdwtab, an old not maintained package, not compatible with colortbl package (rows, columns, cells can't be colored), which has own syntax for determined additional space above/below cells' contents

  • tabu, unfortunately also not maintained package, which provide macro \tabulinesep for additional vertical space above and/or below cells content (many people not recommend to use it because it is quite buggy, but my experiences with it is bad)

  • makecell, which provide macros \setcellgapes{<gap size>} and \makegapedcells. Later macro is not compatible with colortbl package.

Beside mentioned also exist other packages with which I'm less familiar. Example of their use:

\begin{document}
    \begin{table}
\centering
\begin{tabular}{|l | l || c | c |}
    \hlx{hv}
\mcc{\texttt{mdwtab}}
                &   a   &   b       \\  \hlx{vhv}
\mcc{c}         &   d   &   e       \\  \hlx{vhv}
\mcc{f}         &   g   &           \\  \hlx{vhv}
\mcc{h}         &   i   &   j       \\  \hlx{vhv}
        &   k   &   l   &   m       \\  \hlx{vc{2-4}v}
n       &   o   &   p   &   q       \\  \hlx{vc{2-4}v}
        &   r   &   s   &   t       \\  \hlx{vhv}
%
        &   u   &   v   &   w       \\  \hlx{vc{2-4}v}
x       &   y   &   z   &   aa      \\  \hlx{vc{2-4}v}
        &   ab  &   ac  &   ad      \\  \hlx{vhv}
%
        &   ae  &   af  &   ag      \\  \hlx{vc{2-4}v}
ah      &   ai  &   aj  &   ak      \\  \hlx{vc{2-4}v}
        &   al  &   am  &   an      \\  \hlx{vh}
\end{tabular}
    \end{table}
\end{document}


\documentclass{article}
\usepackage{tabu}
\newcommand\mcc[1]{\multicolumn{2}{|l||}{#1}}

\begin{document}
    \begin{table}
\centering
\tabulinesep =_5pt^5pt
\begin{tabu} {|l | l || c | c |}
    \hline
\mcc{}%\texttt{tabu}}
                &   a   &   b       \\  \hline
\mcc{c}         &   d   &   e       \\  \hline
\mcc{f}         &   g   &           \\  \hline
\mcc{h}         &   i   &   j       \\  \hline
        &   k   &   l   &   m       \\  \cline{2-4}
n       &   o   &   p   &   q       \\  \cline{2-4}
        &   r   &   s   &   t       \\  \hline
%
        &   u   &   v   &   w       \\  \cline{2-4}
x       &   y   &   z   &   aa      \\  \cline{2-4}
        &   ab  &   ac  &   ad      \\  \hline
%
        &   ae  &   af  &   ag      \\  \cline{2-4}
ah      &   ai  &   aj  &   ak      \\  \cline{2-4}
        &   al  &   am  &   an      \\  \hline
\end{tabu}
    \end{table}
\end{document}


\documentclass{article}
\usepackage{makecell}
\newcommand\mcc[1]{\multicolumn{2}{|l||}{#1}}

\begin{document}
    \begin{table}
\centering
\setcellgapes{5pt}
\makegapedcells
\begin{tabular}{|l | l || c | c |}
    \hline
\mcc{\texttt{makecell}}
                &   a   &   b       \\  \hline
\mcc{c}         &   d   &   e       \\  \hline
\mcc{f}         &   g   &           \\  \hline
\mcc{h}         &   i   &   j       \\  \hline
        &   k   &   l   &   m       \\  \cline{2-4}
n       &   o   &   p   &   q       \\  \cline{2-4}
        &   r   &   s   &   t       \\  \hline
%
        &   u   &   v   &   w       \\  \cline{2-4}
x       &   y   &   z   &   aa      \\  \cline{2-4}
        &   ab  &   ac  &   ad      \\  \hline
%
        &   ae  &   af  &   ag      \\  \cline{2-4}
ah      &   ai  &   aj  &   ak      \\  \cline{2-4}
        &   al  &   am  &   an      \\  \hline
\end{tabular}
    \end{table}
\end{document}

enter image description here

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.