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

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!

  • 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 Sep 26 '19 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}
  • 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. – Alexandre Quenon Oct 4 '19 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 Oct 4 '19 at 15:47
1

I have edited this answer because, since version 5.5 (2020-10-20), one must no longer write \omit\CodeAfter here (now, \CodeAfter works in all circonstancies).


I 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, makecell}
\renewcommand\theadfont{\bfseries\normalsize}
\renewcommand\theadgape{}

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
\begin{NiceTabular}{*{10}{c}}
\toprule
\Block{2-1}{\thead{A}} 
    & \Block{2-1}{\thead{B}} 
        & \Block{2-1}{\thead{C}} 
            & \multicolumn{3}{c}{\thead{D}} 
                & \multicolumn{3}{c}{\thead{E}} 
                    & \Block{2-1}{\thead{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 (row-1-|col-7) -- (row-3-|col-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

  • 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. – Alexandre Quenon Sep 26 '19 at 12:04
  • Yeah, it gets closer when you use \hline but then the head looks not so nice. – Sango Sep 26 '19 at 12:07

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.