5

I have a simple table:

\begin{table}[H]
    \renewcommand{\arraystretch}{1.3}
  \centering
  \caption{Add caption}
    \begin{tabular}{|c|c|c|c|c|c|}
    \hline
    \multirow{2}{*}{nr fragmentu} & \multicolumn{3}{c|}{wymiary [cm]} & \multicolumn{2}{c|}{wymiary fragmentu} \\
\cmidrule{2-6} \multicolumn{1}{|c|}{} & \multicolumn{1}{c|}{dł.} & \multicolumn{1}{c|}{szer.} & \multicolumn{1}{c|}{powierzchnia} & \multicolumn{1}{c|}{dł.} & \multicolumn{1}{c|}{szer.} \\
    \hline
    1     & 6,8   & 4,9   & 33,6  & 7,0   & 11,8 \\
    2     & 4,8   & 3,4   & 16,1  & 10,0  & 4,5 \\
    3     & 3,4   & 2,4   & 7,9   & 6,5   & 3,5 \\
    4     & 7,8   & 5,5   & 43,3  & 12,5  & 10,1 \\
    5     & 4,8   & 3,4   & 16,4  & 9,7   & 4,9 \\
    6     & 3,6   & 2,5   & 9,1   & 5,5   & 4,8 \\
    7     & 3,3   & 2,3   & 7,8   & 6,7   & 3,3 \\
    8     & 6,2   & 4,4   & 27,2  & 12,5  & 6,2 \\
    \hline
    \end{tabular}%
  \label{tab:tabela3}%
\end{table}%

and I don't understand why I have line break in first row like on the picture: line break

How can a merge horizontal line?

3
  • 2
    Welcome to TeX.SE.
    – Mico
    Nov 25 at 6:03
  • 10
    You're getting the interruptions in the vertical lines because you're using the macros of the booktabs package inappropriately. The user guide of that package states unmistakably and unequivocally that if you want to use the package's macros, you should not employ any vertical lines. Conversely, if you feel you simply must use vertical lines, don't use the booktabs package. There is no in-between compromise.
    – Mico
    Nov 25 at 6:10
  • 1
    For the table at hand, if you decide that having continuous vertical lines is more important to you than having a pleasing table layout, you would need to replace \cmidrule{2-6} with \cline{2-6}.
    – Mico
    Nov 25 at 6:23
5

As I've noted in a comment, don't employ the booktabs package (and its \cmidrule macro) if you (a) want to employ vertical lines and (b) insist on the vertical lines not suffer interruptions. Just replace \cmidrule{2-6} with \cline.

But do please give some thought to how you could improve the "look" of the table significantly by getting rid of all vertical lines andy by using few but well-spaced horizontal lines (by using the macros of the booktabs package).

I would also like to encourage you to improve the table's appearance -- and hence the likelihood that your readers will actually linger and take in the contents of the table -- by aligning the numbers in the five data columns on their respective decimal markers. This may be done with the help of the siunitx package and its S column type. The package also lets you specify which type of decimal marker -- period or comma -- you'd like to use without editing the numbers directly.

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[polish]{babel}
\usepackage{newtxtext,newtxmath}
\usepackage{booktabs,multirow}
\usepackage[output-decimal-marker={,}]{siunitx} % <-- new

\begin{document}

\begin{table}
    %\renewcommand{\arraystretch}{1.3} % not needed
  \centering
  \caption{Add caption\strut}
  \label{tab:tabela3}
  
    \begin{tabular}{@{} c *{2}{S[table-format=1.1]} *{3}{S[table-format=2.1]} @{} }
    \toprule
    nr fragmentu & \multicolumn{3}{c}{wymiary [cm]} & \multicolumn{2}{c@{}}{wymiary} \\
    & & & & \multicolumn{2}{c@{}}{fragmentu} \\
    \cmidrule(lr){2-4} \cmidrule(l){5-6}
    & {dł.} & {szer.} & {powierzchnia} & {dł.} & {szer.} \\
    \midrule
    1     & 6,8   & 4,9   & 33,6  & 7,0   & 11,8 \\
    2     & 4,8   & 3,4   & 16,1  & 10,0  & 4,5  \\
    3     & 3,4   & 2,4   & 7,9   & 6,5   & 3,5  \\
    4     & 7,8   & 5,5   & 43,3  & 12,5  & 10,1 \\ \addlinespace
    5     & 4,8   & 3,4   & 16,4  & 9,7   & 4,9  \\
    6     & 3,6   & 2,5   & 9,1   & 5,5   & 4,8  \\
    7     & 3,3   & 2,3   & 7,8   & 6,7   & 3,3  \\
    8     & 6,2   & 4,4   & 27,2  & 12,5  & 6,2  \\
    \bottomrule
    \end{tabular}
\end{table}

\end{document}
4
  • Great, thanks for help and advices
    – Chey
    Nov 25 at 10:00
  • Thank you also for {newtxtext,newtxmath} package
    – Chey
    Nov 26 at 4:07
  • Just for a nicer look, it would be better if the first cell nr fragmentu was centered
    – Chey
    Nov 26 at 4:13
  • @Chey - I think you know how to work the \multirow command. :-) (FWIW, I actually disagree with the view that the header would look better if it were centered vertically across the three rows. One can't argue about taste, though, right?)
    – Mico
    Nov 26 at 4:34
6

Similar to @Mico answer (+1), but columns are locked in "jail" of vertical lines and for horizontal lines are used Xhline defined in the makecell package:

\documentclass{article}
\usepackage{siunitx}
\usepackage{makecell, multirow}
\NewExpandableDocumentCommand\mcc{O{1}m}%
                                 {\multicolumn{#1}{c|}{#2}}
\begin{document}
\begin{table}[ht]
    \sisetup{table-format=2.1,
             output-decimal-marker={,}
             }   
    \renewcommand{\arraystretch}{1.3}
    \centering
\caption{Add caption}
\label{tab:tabela3}%
    \begin{tabular}{|c |*{2}{S[table-format=1.1]|}
                        *{3}{S|} }
    \Xhline{1pt}
\multirow{2}{*}{\makecell{nr.\\ fragmentu}} 
    & \multicolumn{3}{c|}{wymiary [cm]}
        & \multicolumn{2}{c|}{\makecell{wymiary\\ fragmentu}}    \\
    \cline{2-6} 
    & \mcc{dł.} & \mcc{szer.} & \mcc{powierzchnia} & \mcc{dł.} &  \mcc{szer.} \\
    \Xhline{0.6pt}
1   & 6,8   & 4,9   & 33,6  &  7,0  & 11,8 \\
2   & 4,8   & 3,4   & 16,1  & 10,0  &  4,5 \\
3   & 3,4   & 2,4   &  7,9  &  6,5  &  3,5 \\
4   & 7,8   & 5,5   &  43,3 & 12,5  & 10,1 \\
5   & 4,8   & 3,4   & 16,4  &  9,7  &  4,9 \\
6   & 3,6   & 2,5   &  9,1  &  5,5  &  4,8 \\
7   & 3,3   & 2,3   &  7,8  &  6,7  &  3,3 \\
8   & 6,2   & 4,4   & 27,2  & 12,5  &  6,2 \\
    \Xhline{1pt}
    \end{tabular}%
\end{table}
\end{document}

enter image description here

Edit: As suggested @Mico in his comment, decimal dots are now replaced with decimal comma.

Addendum Your table without vertical lines but with use of the tabularray package and X column type. For fun and exercise, but you may liked:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs,siunitx}

\begin{document}
\begin{table}[ht]
    \sisetup{table-format=2.2,
             output-decimal-marker={,},
             }
\caption{Add caption}
\label{tab:tabela3}%
\begin{tblr}{width=3\linewidth/4,
             colspec = {c *{2}{X[c, si={table-format=1.1}]}
                          *{3}{X[c, si]}
                        },
             rowsep=1pt,
             }
    \toprule
\SetCell[r=2]{c}    {nr.\\ fragmentu}  
    & \SetCell[c=3]{c}  {{{wymiary\\ {[cm]} }}}
            &       &       & \SetCell[c=2]{c}  {{{wymiary\\fragm}}}%{{{\\ tu}}}
                                    &               \\
    \cmidrule[r=-0.5]{2-4}
    \cmidrule[l=-0.5]{5-6}
    & {{{dł.}}}
            & {{{szer.}}}
                    & {{{powier.}}}
                            & {{{dł.}}}
                                    &  {{{szer.}}}  \\
    \midrule
1   & 6,8   & 4,9   & 33,6  &  7,0  & 11,8          \\
2   & 4,8   & 3,4   & 16,1  & 10,0  &  4,5          \\
3   & 3,4   & 2,4   &  7,9  &  6,5  &  3,5          \\
4   & 7,8   & 5,5   &  43,3 & 12,5  & 10,1          \\
5   & 4,8   & 3,4   & 16,4  &  9,7  &  4,9          \\
6   & 3,6   & 2,5   &  9,1  &  5,5  &  4,8          \\
7   & 3,3   & 2,3   &  7,8  &  6,7  &  3,3          \\
8   & 6,2   & 4,4   & 27,2  & 12,5  &  6,2          \\
    \bottomrule
    \end{tblr}
\end{table}
\end{document}

enter image description here

6
  • +1. :-) I think you should provide the option output-decimal-marker={,}, though, as the OP's locale would favor the use of , (comma) as the output decimal marker.
    – Mico
    Nov 25 at 10:07
  • 1
    @Mico, thank you very much! You are right. I will correct ASAP.
    – Zarko
    Nov 25 at 10:10
  • I add my approvation :-) :-) :-) :-) :-).
    – Sebastiano
    Nov 25 at 13:07
  • 1
    @Sebastiano, thank you very much! You make my day more bright :-)
    – Zarko
    Nov 25 at 13:35
  • 1
    @Imran, of course. People say, that such design is "professional". It is almost the same as in the accepted answer, but table code is quite different [therefore I mentioned that addendum is for fun and exercise ;-) ]
    – Zarko
    Nov 25 at 13:58
3

For information. The vertical rules of {NiceTabular} of nicematrix are compatible with the rules of booktabs. However, as said in other answers, the best design is probably with the rules of booktabs in the spirit of booktabs (without vertical rules).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{float}

\begin{document}

\begin{table}[H]
    \renewcommand{\arraystretch}{1.3}
  \centering
  \caption{Add caption}
    \begin{NiceTabular}{|c|c|c|c|c|c|}
    \hline
    \Block{2-1}{nr fragmentu} & \Block{1-3}{wymiary [cm]} &&& \multicolumn{2}{c}{wymiary fragmentu} \\
\cmidrule{2-6} & dł. & szer. & powierzchnia & dł. & szer. \\
    \hline
    1     & 6,8   & 4,9   & 33,6  & 7,0   & 11,8 \\
    2     & 4,8   & 3,4   & 16,1  & 10,0  & 4,5 \\
    3     & 3,4   & 2,4   & 7,9   & 6,5   & 3,5 \\
    4     & 7,8   & 5,5   & 43,3  & 12,5  & 10,1 \\
    5     & 4,8   & 3,4   & 16,4  & 9,7   & 4,9 \\
    6     & 3,6   & 2,5   & 9,1   & 5,5   & 4,8 \\
    7     & 3,3   & 2,3   & 7,8   & 6,7   & 3,3 \\
    8     & 6,2   & 4,4   & 27,2  & 12,5  & 6,2 \\
    \hline
    \end{NiceTabular}%
  \label{tab:tabela3}%
\end{table}%

\end{document}

Output of the above code

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.