1

I have a table that I need to put in a paper. The horizontal and vertical lines are broken lines but my supervisor asked me to have closed lines. I am not able to fix it.

\usepackage{tabularx}

\begin{table}[h] \centering \caption{my table with broken lines} \resizebox{\textwidth}{!}{% \begin{tabular}{@{}|c|c|c|c|c|c|@{}} \toprule \multirow{3}{}{A} & \multirow{3}{}{B} & \multicolumn{4}{|c|}{M} \ \cmidrule(l){3-6} & & \multicolumn{2}{|c|}{C} & \multicolumn{2}{|c|}{D} \ \cmidrule(l){3-6} & & \multicolumn{1}{|c|}{e} & \multicolumn{1}{|c|}{f} & \multicolumn{1}{|c|}{e} & \multicolumn{1}{|c|}{f} \ \midrule \multicolumn{1}{|c|}{1} & \multicolumn{1}{|c|}{a} & \multicolumn{1}{|c|}{\multirow{4}{}{100}} & \multicolumn{1}{|c|}{91.02} & \multicolumn{1}{|c|}{\multirow{4}{}{90}} & \multicolumn{1}{|c|}{209.2} \ \cmidrule(r){1-2} \cmidrule(lr){4-4} \cmidrule(l){6-6} \multicolumn{1}{|c|}{2} & \multicolumn{1}{|c|}{b} & \multicolumn{1}{|c|}{} & \multicolumn{1}{|c|}{133.83} & \multicolumn{1}{|c|}{} & \multicolumn{1}{|c|}{239.66} \ \cmidrule(r){1-2} \cmidrule(lr){4-4} \cmidrule(l){6-6} \multicolumn{1}{|c|}{3} & \multicolumn{1}{|c|}{b} & \multicolumn{1}{|c|}{} & \multicolumn{1}{|c|}{159.73} & \multicolumn{1}{|c|}{} & \multicolumn{1}{|c|}{373.47} \ \cmidrule(r){1-2} \cmidrule(lr){4-4} \cmidrule(l){6-6} 4 & \multicolumn{1}{|c|}{c} & \multicolumn{1}{c|}{} & \multicolumn{1}{|c|}{144.1} & \multicolumn{1}{|c|}{} & 317.01 \ \bottomrule \end{tabular} } \label{tbl:lbl1} \end{table}

It appears like this. enter image description here

hope
  • 35
  • 1
    Horizontal rules from booktabs package doesn't work well with vertical lines. replace them with \hline or cline{...} accordingly. – Zarko Aug 17 '20 at 06:02
  • I addition to the before mentioned booktabs incompatibility, please also stay away from resizebox. This will only result in I consistent font sizes. Also, the \multicolumn{1}{|c|} commands in the first column are redundant since your column is already centered. – leandriis Aug 17 '20 at 06:06
  • @Zarko I was just about to ask you to put your comment as answer because that fixed my problem. Now I see that you put an answer I will accept it. Thanks a lot!! – hope Aug 17 '20 at 06:22

2 Answers2

4

Like this?

enter image description here

\documentclass{article}
\usepackage{makecell, multirow}

\begin{document} \begin{table}[h] \centering \setcellgapes{3pt} \makegapedcells \caption{my table with broken lines} \label{tbl:lbl1} \begin{tabular}{|{6}{c|}} \Xhline{1pt} \multirow{3.4}{}{A} & \multirow{3.4}{}{B} & \multicolumn{4}{c|}{M} \ \cline{3-6} & & \multicolumn{2}{c|}{C} & \multicolumn{2}{c|}{D} \ \cline{3-6} & & e & f & e & f \ \hline 1 & a & \multirow{4.5}{}{100} & 91.02 & \multirow{4.5}{*}{90}
& 209.2 \ \cline{1-2} \cline{4-4} \cline{6-6} 2 & b & & 133.83 & & 239.66 \ \cline{1-2} \cline{4-4} \cline{6-6} 3 & b & & 159.73 & & 373.47 \ \cline{1-2} \cline{4-4} \cline{6-6} 4 & c & & 144.1 & & 317.01 \ \Xhline{1pt} \end{tabular} \end{table} \end{document}

or if you like to have table wide as \textwidth, than you can use tabularx table environment (not recommended):

enter image description here

\documentclass{article}
\usepackage{makecell, multirow, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document} \begin{table}[h] \centering \setcellgapes{3pt} \makegapedcells \caption{my table with broken lines} \label{tbl:lbl1} \begin{tabularx}{\linewidth}{|{6}{C|}} \Xhline{1pt} \multirow{4}{}{A}
& \multirow{4}{*}{B}
& \multicolumn{4}{c|}{M} \ \cline{3-6} & & \multicolumn{2}{c|}{C} & \multicolumn{2}{c|}{D} \ \cline{3-6} & & e & f & e & f \ \hline 1 & a & \multirow{5.5}{=}{\centering 100} & 91.02 & \multirow{5.5}{=}{\centering 90}
& 209.2 \ \cline{1-2} \cline{4-4} \cline{6-6} 2 & b & & 133.83 & & 239.66 \ \cline{1-2} \cline{4-4} \cline{6-6} 3 & b & & 159.73 & & 373.47 \ \cline{1-2} \cline{4-4} \cline{6-6} 4 & c & & 144.1 & & 317.01 \ \Xhline{1pt} \end{tabularx} \end{table} \end{document}

Addendum: after two years ... :-) I would consider tabularray package (version 2022C) with library siunitx (which load siunutx package) which enable to write shorter table code and by used library aligned numbers at decimal points:

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}

\begin{document} \begin{table}[h] \caption{my table with broken lines} \label{tbl:lbl1} \begin{tblr}{hline{1,Z} = 1pt, hline{4} = 0.8pt, hline{2,3,5-Y}={solid}, vlines, colspec = {{2}{X[c]} {4}{X[c, si={table-format=3.2}]}}, row{1-3} = {mode=text, guard} } \SetCell[r=3]{c} A & \SetCell[r=3]{c} B & \SetCell[c=4]{c} M
& & & \ & & \SetCell[c=2]{c} C
& & \SetCell[c=2]{c} D & \ \cline{3-6} & & e & f & e & f \ 1 & a & \SetCell[r=4]{c, guard} 100 & 91.02
& \SetCell[r=4]{c, guard} 100 & 209.2 \

2 & b & & 133.83
& & 239.66 \ 3 & b & & 159.73
& & 373.47 \ 4 & c & & 144.1
& & 317.01 \ \end{tblr} \end{table} \end{document}

enter image description here

Zarko
  • 296,517
2

Here is a way to construct that table with {NiceTabular} of nicematrix (although a design in the spirit of booktabs, without any vertical rule, would be a better design).

In {NiceTabular}, you merge cells both horizontally and vertically with the command \Block. For the rows, you give the number of logical rows (and not the number of physical lines as with \multicolumn).

With the key hvlines, all the rules are drawn, excepted in the blocks (constructed by \Block).

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{nicematrix}
\usepackage{siunitx}

\begin{document} \begin{table}[h] \caption{my table with broken lines} \label{tbl:lbl1} \centering \begin{NiceTabular}[hvlines, columns-width=auto, cell-space-limits=2pt] {cccS[table-format=3.2]cS[table-format=3.2]} \Block{3-1}{A} & \Block{3-1}{B} & \Block{1-4}{M} \ & & \Block{1-2}{C} & & \Block{1-2}{D} \ & & e & {f} & e & {f} \ 1 & a & \Block{4-1}{100} & 91.02 & \Block{4-1}{100} & 209.2 \ 2 & b & & 133.83 & & 239.66 \ 3 & b & & 159.73 & & 373.47 \ 4 & c & & 144.1 & & 317.01 \ \end{NiceTabular} \end{table} \end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250