3

I have a huge problem with formatting my tabular latex table with multicolumns and multirows. Specifically with vertical line separation and centering the last section. I'm pretty new in tables so feel free to give me any piece of advice which can help with it.

Here is my MWE:

\documentclass[final,5p,twocolumn]{elsarticle}

\usepackage{float, enumitem, amsmath} 
\usepackage{booktabs, makecell, multicol, multirow, threeparttable, tabularx, subcaption, booktabs, colortbl, color}
\usepackage{adjustbox}
\usepackage{todonotes}
\usepackage{caption}
\captionsetup{belowskip=-5pt}
\captionsetup[table]{justification=raggedright,singlelinecheck=off}
\newcommand*\rot{\rotatebox{90}}

\begin{document}

\clearpage
\onecolumn
\begin{table} \centering
\caption{Simple caption.}
\label{tab:setTag}
\begin{adjustbox}{width=0.9\textwidth,center=\textwidth}
\begin{threeparttable}
\begin{tabular}{@{}l|lcc@{}}
\toprule
& \multicolumn{1}{l}{\bfseries Results} & \bfseries Method 1 & \bfseries Method 2 \\
\midrule
& &\multicolumn{2}{c}{First section}\\ \cmidrule{3-4} 
\multirow{14}{*}{\rotatebox[origin=c]{90}{~Description 1}}& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}

& &\multicolumn{2}{c}{Second section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}

& &\multicolumn{2}{c}{Third section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 

\rowcolor{gray!10}
& &\multicolumn{2}{c}{First section}\\ \cmidrule{3-4} 
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
\rowcolor{gray!10}
& &\multicolumn{2}{c}{Second section}\\ \cmidrule{3-4} 
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
\rowcolor{gray!10}
& &\multicolumn{2}{c}{Third section}\\ \cmidrule{3-4} 
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
\multirow{-8}{*}{\rotatebox[origin=c]{90}{~Description 2}}& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}

& &\multicolumn{2}{c}{First section}\\ \cmidrule{3-4} 
\multirow{1}{*}{\rotatebox[origin=c]{90}{~Description 3}}& Very long name & Something pretty long too & Something else even longer than before \\
\cmidrule{3-4}
\end{tabular}
\end{threeparttable}
\end{adjustbox}
\end{table}

\end{document}

enter image description here

2 Answers 2

1

Like this?

enter image description here

  • Rules defined in booktabs package has small gape above and below rule, consequently the are not intended for table with vertical lines.
  • It is not clear, why your table you use the threeparttable when you not use any its features.
  • I would not use scalebox, better is use table environment for example as is tabularx where you can prescribe table width. With use its X column type the text in cells in such column is automatic break into more lines (well, this you can obtain with p{<width>} columns too).
  • Instead of switch from \twocolumn document to \onecolumn (and than back again) is better to use table* float environment. Using it, table will appear on the top of the next page from where is inserted in the text.
\documentclass[final,5p,twocolumn]{elsarticle}

\usepackage{float, enumitem, amsmath}
\usepackage{rotating}     % new
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{} % new
\usepackage[table]{xcolor}% changed, instead of color, colortbl
%\usepackage{adjustbox} % not used in this MWE
%\usepackage{todonotes} % not used in this MWE

\usepackage{caption}
\captionsetup{belowskip=-5pt}
\captionsetup[table]{justification=raggedright,singlelinecheck=off}
%\usepackage{subcaption} % not used in this MWE

\begin{document}
    \begin{table*} 
    \centering
    \settowidth\rotheadsize{\theadfont Description 3 }
    \renewcommand\arraystretch{1.1}
\caption{Simple caption.}
\label{tab:setTag}
%\begin{adjustbox}{width=0.9\textwidth,center=\textwidth}
%\begin{threeparttable}
\begin{tabularx}{\linewidth}{l | l *{2}{>{\centering\arraybackslash}X}}
    \Xhline{1pt}
    & \thead[l]{Results} 
        & \thead{Method 1} & \thead{Method 2}           \\
    \Xhline{0.7pt}
    &   & \multicolumn{2}{c}{First section}             \\ 
    \Xcline{3-4}{0.6pt}
\multirow{16}{*}{\rothead{Description 1}}
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Second section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Third section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   & \multicolumn{2}{c}{First section}             \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Second section}             \\ 
    \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Third section}          \\ 
    \Xcline{3-4}{0.6pt}
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
    \rowcolor{gray!10}
\multirow{-8}{*}{\rothead{Description 2}}
    & Very long name & Something pretty long too & Something else even longer than before \\
%    \Xcline{3-4}{0.7pt}
%    &   &\multicolumn{2}{c}{First section}      \\ 
%    \Xcline{3-4}{0.7pt}
%\multirow{1}{*}{\rotatebox[origin=c]{90}{~Description 3}}
%    & Very long name & Something pretty long too & Something else even longer than before \\
%    \Xcline{3-4}{0.7pt}
    \Xhline{1pt}
\end{tabularx}
%\end{threeparttable}
%\end{adjustbox}
    \end{table*}
\end{document}

Addendum: A possible solution or third part of table, where instead of "Description 3" in the first column is used abbreviation "D. 3". Association to their meaning is given with "Description 1 (D. 1)" and "Description 2 (D. 2)" in the first two parts of table.

\documentclass[final,5p,twocolumn]{elsarticle}

\usepackage{float, enumitem, amsmath}
\usepackage{rotating}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{}
\usepackage[table]{xcolor}

\usepackage{caption}
\captionsetup{belowskip=-5pt}
\captionsetup[table]{justification=raggedright,singlelinecheck=off}
\usepackage{subcaption}

\begin{document}
    \begin{table*} 
    \centering
    \settowidth\rotheadsize{\theadfont Description 3 (D. 3)}
    \renewcommand\arraystretch{1.1}
\caption{Simple caption.}
\label{tab:setTag}
\begin{tabularx}{\linewidth}{l | l *{2}{>{\centering\arraybackslash}X}}
    \Xhline{1pt}
    & \thead[l]{Results} 
        & \thead{Method 1} & \thead{Method 2}           \\
    \Xhline{0.7pt}
    &   & \multicolumn{2}{c}{First section}             \\ 
    \Xcline{3-4}{0.6pt}
\multirow{16}{*}{\rothead{Description 1 (D.\,1)}}
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Second section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Third section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   & \multicolumn{2}{c}{First section}             \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.8pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Second section}             \\ 
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.8pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Third section}          \\ 
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
\multirow{-8}{*}{\rothead{Description 2 (D.\,2)}}
    & Very long name & Something pretty long too & Something else even longer than before \\
    \Xcline{3-4}{0.6pt}
\settowidth\rotheadsize{\theadfont(D.\,3)} % <--- changed (reduced) \rothead size
\multirow{2}{*}{\rothead{(D.\,3)}}
    &   &\multicolumn{2}{c}{First section}      \\ 
    & Very long name & Something pretty long too & Something else even longer than before \\[1.5ex]
    \Xhline{1pt}
\end{tabularx}
    \end{table*}
\end{document}

enter image description here

11
  • thank you so much Zarko! almost there. I can handle with Xline under specific section (Fist/Second/First) but do you have any idea how to solve the issue with Description 3 section where is only one subsection (describe in the bottom of the photo)?
    – Dominika
    Jun 2, 2020 at 13:41
  • @Dominika, this was not clear to me. I thought that you didn’t write all the rows of your table, so that part of table I omitted. You certainly can't insert "Description 3" in the height of one table row because that word is just too long. You can add blank rows, or use an abbreviation instead, such as "D 3.
    – Zarko
    Jun 2, 2020 at 13:50
  • to answer some of your thoughts: I believe that I used threeparttable features in my thesis when it comes to pretty long caption but in the MWE no (I probably forgot to delete it so my mistake, so sorry). Scalebox was the first idea and stuck with me but I'm happy to try your suggestion with tabularx. Previously I have some trouble with tabularx environment when I have a lot of headers - not text in the cell - and I can't handle with fit these headers in A4 wide.
    – Dominika
    Jun 2, 2020 at 13:50
  • And the last thought - I don't use 2 columns environment but I have very long .cls file with specific option to my document and I want put here whole self-enclosed MWE example so I used the first type of document which I found on overleafs templates.
    – Dominika
    Jun 2, 2020 at 13:51
  • Yes, the blank rows are the good starting point - I will try.
    – Dominika
    Jun 2, 2020 at 13:54
1

The environment {NiceTabular} of nicematrix (≥ 4.0) is compatible with booktabs.

You can do all the blocks (\multicolumn and multirow) with \Block).

You can color your rows with only one instruction [code-before = \rowcolor{gray!10}{18-25}]

You can rule whatever rule you want at the end using Tikz and the nodes created by nicematrix under the array.

\documentclass{article}

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

\begin{document}

\begin{NiceTabular}{wc{5mm}LCC}[code-before = \rowcolor{gray!10}{18-25}]
\toprule
& \bfseries Results & \bfseries Method 1 & \bfseries Method 2 \\
\midrule
& &\Block{1-2}{First section}\\ \cmidrule{3-4} 
\Block{14-1}{\rotate Description 1}
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}
%
& &\Block{1-2}{Second section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}
%
& &\Block{1-2}{Third section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
%
\Block{8-1}{\rotate Description 2}& &\Block{1-2}{First section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
%
& &\Block{1-2}{Second section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
%
& &\Block{1-2}{Third section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \bottomrule
\CodeAfter
\tikz \draw (row-1-|col-2) -- (row-26-|col-2) ;
\end{NiceTabular}

\end{document}

Result of above code

9
  • 2
    It seems that NiceTabular will become serious competitor in table design options :-)! +1!
    – Zarko
    Jun 2, 2020 at 17:19
  • @Zarko: I try to improve the package... :-) Jun 2, 2020 at 17:30
  • @F.Pantigny when I try put this MWE to overleafs for quick view the difference between these two solutions I had a lot of errors message with 'environment NiceTabular undefined' even if I added '\usepackage{nicematrix,tikz}' to my preamble. any advice?
    – Dominika
    Jun 2, 2020 at 17:42
  • @Dominika: As I wrote, you need a very recent version of nicematrix (v. 4.0 of 2020/05/08) and that version needs a very recent version of expl3 (LaTeX3 programming layer which is now included in the LaTeX format itself). That means that you need a up-to-date version of LaTeX (e.g. TeXLive 2020). I think that Overleaf is still at TeXLive 2019. So you can't use it with Overleaf. Maybe in one year... Jun 2, 2020 at 17:47
  • @F.Pantigny thank you for clarification. I will try maybe in Texmaker. Can I do with nicetab align with mathematical signs? Like, let's suppose I have one column with '+/-' sign which should be aligned to the left of columns and all the values in each cell should be aligned to '+/-' sign. Can I do it easier than siunitx which completely destroy my table...
    – Dominika
    Jun 2, 2020 at 17:51

You must log in to answer this question.

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