4

I would like to add vertical lines for certain columns starting from \midrule to \bottomrule as depicted in the figure provided below.

enter image description here

I am also providing a MWE. I tried to use *{3}{c}| *{2}{r r r}}, however it does not generate the exact output that I want (the undesired output is shown below).

enter image description here

\documentclass[11pt]{article}
\usepackage{rotating}
\usepackage{booktabs, makecell} 
\usepackage{multicol}
\usepackage{multirow}
\usepackage{mathtools}
\usepackage[flushleft]{threeparttable}
\usepackage{newtxtext}

\usepackage{booktabs}
\usepackage{makecell}

\begin{document}

\begin{sidewaystable}

  \begin{center}
\setlength\tabcolsep{0pt}
\renewcommand{\cellset}{\bfseries\linespread{1}\selectfont}
\renewcommand\cellalign{bc}
  \centering
  \caption{My Table}{
 \small
    \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} 
                        *{3}{c}
                        *{2}{r r r}}
                        
     \toprule
    &  &   & \multicolumn{3}{c}{First}  & \multicolumn{3}{c}{ Second}   \\
    \cmidrule(l){4-6} \cmidrule(l){7-9}
    {A} & {B}  & {C} 
    & {\makecell{D}} & {\makecell{E}} & {\makecell{F}}  & {\makecell{X}} & {\makecell{Y}} & {\makecell{Z}}  \\
    \midrule
    
 1 & 2 & 3 &4 & 5& 6 &7 & 8& 9  \\
 11 & 12 & 13 &14 & 15& 16 &17 & 18& 19  \\
  21 & 22 & 23 &24 & 25& 26 &27 & 28& 29  \\
    \bottomrule
    \end{tabular*}
}
   \end{center}
\end{sidewaystable}

\end{document}

11
  • 2
    Don't use left-trimming, and your tableau will be just fine without the vertical lines.
    – Mico
    Mar 16, 2021 at 23:04
  • Thanks for the suggestion, however, I am required to add those lines to make a point in my paper. This is just a minimal example. In essence, adding the vertical lines to my own table is important.
    – ball_jan
    Mar 16, 2021 at 23:06
  • Horizontal lines from the bokksabt package are incompatible with vertical lines.
    – leandriis
    Mar 16, 2021 at 23:09
  • 1
    @leandriis Then, it does not make sense to try anything on my current example, right? I should probably consider a different approach?
    – ball_jan
    Mar 17, 2021 at 0:08
  • 1
    @ball_jan: Thanks for the clarification. What about the two example outputs in my previous comment?
    – leandriis
    Mar 17, 2021 at 16:57

2 Answers 2

2

Edit:

That vertical rules are not interrupted by rules defined in booktabs package you should replace \toprule, \bottomrule and \midrule for example with Xhline{<width>} .

In MWE below are used S columns for better number align. With use of the tabularx table are prescribed columns width:

\documentclass[11pt]{article}
\usepackage{newtxtext}
\usepackage{rotating}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[flushleft]{threeparttable}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}
\NewExpandableDocumentCommand\mcx{O{1}m}{\multicolumn{#1}{C}{#2}}

%\usepackage{multicol}
%\usepackage{mathtools}
\usepackage{siunitx}    % new

\begin{document}

\begin{sidewaystable}[hp]
    \setcellgapes{5pt}
    \makegapedcells
    \sisetup{table-format=2, 
             table-column-width=4em}
    \setlength\tabcolsep{0pt}
    \centering
\caption{My Table}
    \begin{tabularx}{\linewidth}{ *{3}{S}| *{3}{S}| *{3}{S} }
    \Xhline{1.2pt}
\mcc[3]{}           & \mcc[3]{First}& \mcc[3]{Second}       \\
    \cmidrule(lr){4-6}  \cmidrule(l){7-9}
\mcx{A} & \mcx{B}   & \mcx{C}   & \mcx{D}   & \mcx{E}   & \mcx{F}   & \mcx{X}   & \mcx {Y}  & \mcx{Z}   \\
    \Xhline{0.8pt}
 1      &  2        &  3        &  4        &  5        &  6        &  7        &  8        &  9        \\
11      & 12        & 13        & 14        & 15        & 16        & 17        & 18        & 19        \\
21      & 22        & 23        & 24        & 25        & 26        & 27        & 28        & 29        \\
    \Xhline{1.2pt}
    \end{tabular*}
\end{sidewaystable}

\end{document}

enter image description here

4
  • Thanks for the help, but I illustrated what I want in the first figure in my post. For instance, the horizontal line in your example starts from the top of the table. I want it to go from the middle to the bottom. In addition, I would like to place a similar line between F and X.
    – ball_jan
    Mar 17, 2021 at 16:53
  • @ball_jan, now is clear. You like to reproduce first image, not the second. I will correct this ASAP.
    – Zarko
    Mar 17, 2021 at 17:01
  • Thank you! I also edited my post accordingly since I see your point about why it could confuse others.
    – ball_jan
    Mar 17, 2021 at 17:14
  • @ball_jan, see edited answer.
    – Zarko
    Mar 17, 2021 at 17:39
2

It is possible to reproduce your MWE using the nicematrix package. It creates TikZ nodes so it is possible to draw the vertical lines you need, with two tikz commands.

The package offers the command \Block that is able to create multicolumn / multirow cells.

\Block{1-3}{First} creates a 1 row x 3 columns cell with a centered First inside.

Aware that this is super simplified MWE I added another, more complete example, using \Block.

\Block{}{<content>} creates a 1 row x 1 column cell that allows to use \\ inside.

Assuming the second lines of headings could have longer text than A, B, C, ... and some of them might need to be split into two or more lines, all those headings were included in a \Block command, that also centered them.

Due to the tikz nodes two compilations might be needed the first time.

ar

br

This is the code. As you can see very compact and easy to maintain.

\documentclass[11pt]{article}
\usepackage{rotating}

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{sidewaystable}

\centering
  \caption{My Table I}
  \medskip
\small

\begin{NiceTabular}{!{\extracolsep{4pt}}*{3}{c} *{2}{r r r}}[%
    cell-space-limits=5pt, %expand cells vertically
    columns-width = 1.8cm, %adjust as needed
    ]   
    \hline
    &   &   &\Block{1-3}{First} &   &   & \Block{1-3}{Second}   &   &   \\
    \cline{4-6} \cline{7-9}
    A   &B  &C  &
    \bfseries D &
    \bfseries E &
    \bfseries F &
    \bfseries X &
    \bfseries Y &
    \bfseries Z \\
    \hline
1   &2  &3  &4  &5  &6  &7  &8  &9  \\
11  &12 &13 &14 &15 &16 &17 &18 &19 \\
21  &22 &23 &24 &25 &26 &27 &28 &29 \\
\hline
\CodeAfter
{%
    \tikz \draw (row-3-|col-4) -- (row-6-|col-4);
    \tikz \draw (row-3-|col-7) -- (row-6-|col-7);
}
\end{NiceTabular}
\end{sidewaystable}


\begin{sidewaystable}
    \centering
    \caption{My Table II}
    \medskip
    \small
\begin{NiceTabular}{!{\extracolsep{4pt}}*{3}{c} *{2}{r r r}}[%
    cell-space-limits=5pt, %expand cells vertically
    columns-width = 1.8cm, %adjust as needed
    ]   
    \hline
    &   &   &\Block{1-3}{First} &   &   & \Block{1-3}{Second}   &   &   \\
    \cline{4-6} \cline{7-9}
    \Block{}{A }    &
    \Block{}{B \\ line} &
    \Block{}{C \\ line} &
    \Block[c]{}<\bfseries>{D \\ line}   &
    \Block[c]{}<\bfseries>{E }  &
    \Block[c]{}<\bfseries>{F \\ line}   &
    \Block[c]{}<\bfseries>{X \\ line}   &
    \Block[c]{}<\bfseries>{Y \\ line}   &
    \Block{}<\bfseries>{Z}                 \\
    \hline
    1   &2  &3  &4  &5  &6  &7  &8  &9  \\
    11  &12 &13 &14 &15 &16 &17 &18 &19 \\
    21  &22 &23 &24 &25 &26 &27 &28 &29 \\
    \hline
    \CodeAfter
    {%
        \tikz \draw (row-3-|col-4) -- (row-6-|col-4);
        \tikz \draw (row-3-|col-7) -- (row-6-|col-7);
    }
\end{NiceTabular}

\end{sidewaystable}

\end{document}

You must log in to answer this question.

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