0

I struggle with a multirow, multicolumn table in latex. So far I have this (messy) code:

\begin{tabular}{cccccccccc}
    \toprule
    \multirow{2}{*}{\textbf {A}} &
    \multirow{2}{*}{\textbf {B}} &
    \multirow{2}{*}{\textbf {C}} &
    \multicolumn{3}{c}{\textbf {D}} && D1 & D2 & D3 &
    \multicolumn{3}{c}{\textbf {E}}   && E1 & E2 & E3 &
    \multirow{2}{*}{\textbf {F}}\\
    \cmidrule(lr){1-10}
    a & b & c & d1 & d2 & d3 & e1 & e3 & e3 & f\\
    aa & bb & cc & dd1 & dd2 & dd3 & ee1 & ee3 & ee3 & ff
    \bottomrule
\end{tabular}

Ideally the table would look like this:

multicolumn, multirow table

Subheader D1-E3 should not be bold....

Any help is appreciated.

1

4 Answers 4

1

I tried to clean your code a little bit.

Here is the result:

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}


\begin{document}

    \begin{tabular}{*{10}{c}}
        \toprule
        \multirow{2}{*}{\textbf{A}} & \multirow{2}{*}{\textbf{B}} & \multirow{2}{*}{\textbf{C}} &
        \multicolumn{3}{c}{\textbf{D}} & \multicolumn{3}{c}{\textbf{E}} & \multirow{2}{*}{\textbf{F}}\\
        \cmidrule{4-9}
        &&&  D1 & D2 & D3 & E1 & E2 & E3 & \\ \midrule
        a & b & c & d1 & d2 & d3 & e1 & e3 & e3 & f\\
        aa & bb & cc & dd1 & dd2 & dd3 & ee1 & ee3 & ee3 & ff\\
        \bottomrule
    \end{tabular}

\end{document}

It renders like this:

enter image description here

However, if you absolutely need the vertical line, I have no idea how to do it on two rows only...

Enjoy!

1
  • No, the vertical line isn't a must. Thank your for your code. I got another solution here: stackoverflow.com/questions/58115402/… which introduces a small gap instead of the vertical line, also very nice layout. Anyway, thanks a lot for your prompt answer.
    – mati
    Commented Sep 26, 2019 at 11:56
1

I more like the following table design:

enter image description here

\documentclass{article}

\usepackage{booktabs, makecell, multirow}
\renewcommand\theadfont{\bfseries\normalsize}
\renewcommand\theadgape{}


\begin{document}
    \begin{tabular}{*{10}{c}}
    \toprule
\multirow{2.5}{*}{\thead{A}} 
    & \multirow{2.5}{*}{\thead{B}} 
        & \multirow{2.5}{*}{\thead{C}} 
            &   \multicolumn{3}{c}{\thead{D}} 
                & \multicolumn{3}{c}{\thead{E}} 
                    & \multirow{2.5}{*}{\thead{F}}    \\
    \cmidrule(l){4-6}   \cmidrule(l){7-9}
    &       &       & D1    & D2    & D3    & E1    & E2    & E3    & \\ 
    \midrule
a   & b     & c     & d1    & d2    & d3    & e1    & e3    & e3    & f\\
aa  & bb    & cc    & dd1   & dd2   & dd3   & ee1   & ee3   & ee3   & ff\\
    \bottomrule
    \end{tabular}
\end{document}
2
  • is the '2.5' first argument of \multirow the reason for which the A, B, C and F are centered regarding the \cmidrule commands, compared to my solution? I used '2' instead and I got a small shift. Commented Oct 4, 2019 at 14:54
  • 1
    @AlexandreQuenon, yes. With this is considered spaces around rules (below of \toprule, around \cmisrule` and above of the \midrule) which sum is about 0.5\baselineskip. This is nice new features of recent ˙multirow` package.
    – Zarko
    Commented Oct 4, 2019 at 15:47
1

If you really want to add vertical rules compatible with the horizontal rules of booktabs (which is not at all in the spirit of booktabs) you should try {NiceTabular} of nicematrix. That environment creates PGF/Tikz nodes under the cells, rows and columns of the tabular and it's possible to use these nodes to draw whatever rule you want with Tikz.

\documentclass{article}

\usepackage{booktabs}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
\begin{NiceTabular}{*{10}{c}}
\toprule
\RowStyle{\bfseries}
\Block{2-1}{A} 
    & \Block{2-1}{B} 
        & \Block{2-1}{C} 
            & \Block{1-3}{D} 
                &&& \Block{1-3}{E} 
                    &&& \Block{2-1}{F}    \\
    \cmidrule(l){4-9}
    &       &       & D1    & D2    & D3    & E1    & E2    & E3    & \\ 
    \midrule
a   & b     & c     & d1    & d2    & d3    & e1    & e3    & e3    & f\\
aa  & bb    & cc    & dd1   & dd2   & dd3   & ee1   & ee3   & ee3   & ff\\
\bottomrule
\CodeAfter \tikz \draw (1-|7) -- (3-|7) ;
\end{NiceTabular}
\end{document}

You need several compilations because nicematrix uses PGF/Tikz nodes.

Output of the above code

-1

Added the vertical lines to @Alexandre Quenon (EDIT:Sorry got confused) solution

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}


\begin{document}

    \begin{tabular}{cccccccccc}
        \toprule
        \multirow{2}{*}{\textbf{A}} & \multirow{2}{*}{\textbf{B}} & \multirow{2}{*}{\textbf{C}} &
        \multicolumn{3}{c|}{\textbf{D}} & \multicolumn{3}{|c}{\textbf{E}} & \multirow{2}{*}{\textbf{F}}\\
        \cmidrule{4-9}
        &&&  D1 & D2 & \multicolumn{1}{c|}{D3} & E1 & E2 & E3 & \\ 
        \midrule
        a & b & c & d1 & d2 & d3 & e1 & e3 & e3 & f\\
        aa & bb & cc & dd1 & dd2 & dd3 & ee1 & ee3 & ee3 & ff\\
        \bottomrule
    \end{tabular}

\end{document}

Result

3
  • it works but the problem is that you don't have a continuous vertical line, which is quite the opposite of what you usually want. Commented Sep 26, 2019 at 12:04
  • Yeah, it gets closer when you use \hline but then the head looks not so nice.
    – Sango
    Commented Sep 26, 2019 at 12:07
  • Instead of \cmidrule{4-9} you can consider using \cline{4-9} Commented Jul 26, 2021 at 17:40

You must log in to answer this question.

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