2

I am trying to add multirows to this Coloring rows with longtable.

How can I add multirows in this example?

This is my attempt:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\title{A random title}

% Add longtable and xcolor packages and define two colors
\usepackage{longtable}
\usepackage[dvipsnames,table]{xcolor}
\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}

% Add the multirow package
\usepackage{multirow, makecell}

\begin{document}
\maketitle


% Table
\rowcolors{1}{light-cyan}{light-gray}
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
  \hiderowcolors
  \caption{Fine caption}\\
  \showrowcolors
  \hline
  \rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
  \hline
  \rowcolor{yellow}Head 1 & Head 2 & Head 3\\\hline\endhead
  \hline\endfoot
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  % attempt to insert multirows
  \multirow{2}{*}[0em]{Saturday} & \multirow{2}{*}[0em]{$17$} & \\
  & & \\
  % attempt to add a horizontal line
  \cline{2-3}
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\

\end{longtable}
        
\end{document}

This is how it currently looks:

enter image description here

1
  • note: I tried to add a horizontal line using \cline{2-3}, just for a better readability of the table
    – Ommo
    Feb 14, 2023 at 15:07

2 Answers 2

3

Merge cells from the "bottom" with a negative number of cells. It seems that colours applied to rows overlap the text. If you move \multirow to the bottom, you let the text be typeset after the colours are applied.

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\title{A random title}

% Add longtable and xcolor packages and define two colors
\usepackage{longtable}
\usepackage[dvipsnames,table]{xcolor}
\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}

% Add the multirow package
\usepackage{multirow, makecell}


\begin{document}
\maketitle

% Table
\rowcolors{1}{light-cyan}{light-gray}
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
  \hiderowcolors
  \caption{Fine caption}\\
  \showrowcolors
  \hline
  \rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
  \hline
  \rowcolor{yellow}Head 1 & Head 2 & Head 3\\\hline\endhead
  \hline\endfoot
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  & & \\
  \multirow{-2}{*}[0em]{Saturday} & \multirow{-2}{*}[0em]{$17$} & \\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
\end{longtable}
\end{document}

enter image description here


Additional variants of the code to keep the same background colour of "merged" cells--only snippets are provided whereas the rest of the code is the same.

A variant with resetting the counter rownum (based on answers in this question)

\begin{longtable}{|*{3}{p{.3\textwidth}|}}
  \hiderowcolors
  \caption{Fine caption}\\
  \hline
  \rowcolor{magenta}%
  Head 1 & Head 2 & Head 3\\\hline\endfirsthead
  \hline
  \rowcolor{yellow}%
  Head 1 & Head 2 & Head 3\\\hline\endhead
  \hline\endfoot
  \showrowcolors
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  \global\rownum=1\relax% applies to the row after this one
  & & \\
  \multirow{-2}{*}[0em]{Saturday} & \multirow{-2}{*}[0em]{$17$} & \\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
\end{longtable}

enter image description here

A variant with an inner tabular:

% 
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
  \hiderowcolors
  \caption{Fine caption}\\
  \showrowcolors
  \hline
  \rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
  \rowcolor{yellow} Head 1 & Head 2 & Head 3\\\hline\endhead
  \hline\endfoot
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Saturday  & $17$      & \begin{tabular}{@{}l@{}}
                            A\\
                            B\\
                            C
                          \end{tabular} \\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
\end{longtable}

enter image description here

A variant with a "stretched" row via a strut:

\begin{longtable}{|*{3}{p{.3\textwidth}|}}
  \hiderowcolors
  \caption{Fine caption}\\
  \showrowcolors
  \hline
  \rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
  \rowcolor{yellow} Head 1 & Head 2 & Head 3\\\hline\endhead
  \hline\endfoot
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  \rule[\dimexpr-\baselineskip+0.5ex]{0pt}{2\baselineskip}%
  Saturday  & $17$      & \\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
\end{longtable}

enter image description here

5
  • Thanks a lot @Celdor! ....just a question...something I did not understand....... is it possible to have the same color for the two cells shared by the Saturday and by 17 texts ? Or maybe I misunderstood your answer... :-)
    – Ommo
    Feb 14, 2023 at 16:35
  • 1
    It's possible, although I wonder if merged rows are supposed to have the same colour, why do you apply \multirow?
    – Celdor
    Feb 14, 2023 at 16:44
  • Ah ok, Yes yes, I would need the same color for merged rows.... that would my final goal.. :-) I thought that by applying \multirow I could get merged rows (often, for just a few columns) for both text and colors..
    – Ommo
    Feb 14, 2023 at 16:52
  • 1
    I updated my answer and added some additional variants of your code. Depending of your final goal, \multirow might not be the best tool. Hope it helps.
    – Celdor
    Feb 14, 2023 at 17:08
  • Amazing, thanks a lot @Celdor!!! Do you think it is possible to add also horizontal lines, for example, as \cline{2-3}?
    – Ommo
    Feb 14, 2023 at 19:58
4
  • As I see your table, you not need multi row cells.
  • Using \longtblr of tabularray package enable to write concise table code:
\documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{longtblr}[
caption = {Fine caption},
  label = {tab:long}
                ]{vlines = {gray},
                 colspec = { *{3}{X[l]} },
                 row{odd} = {bg=light-cyan},
                 row{even}= {bg=light-gray},
                 row{1} = {bg=purple!80, c},
                 %
                 rowhead=1
                 }
    \toprule[gray]
Head 1      & Head 2    & Head 3    \\ 
    \midrule[gray]
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
\SetRow{ht=2\baselineskip}  
Saturday    & 17        &           \\
  Content 1 & Content 2 & Content 3\\
Mondday     & 19        &   {A\\ B\\ C} \\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
  Content 1 & Content 2 & Content 3\\
    \bottomrule
\end{longtblr}
\end{document}

enter image description here

3
  • Fantastic answer @Zarko!!! Can I accept both your and @Celdor's answers ?? .......A little question.... @Zarko, would it be possible to add horizontal lines among rows as well, for example, \cline{2-3}?
    – Ommo
    Feb 14, 2023 at 20:00
  • 1
    @limone, of course. Just try and you will see what will happen. But why you will add those lines? BTW, you should accept only one answer ... which solve your problem on the best (simple, concise, ...) way ;-)
    – Zarko
    Feb 14, 2023 at 21:21
  • thanks a lot for your nice comment! About the lines, it is just for aesthetic reasons / personal taste, and not for any technical reason or particular needs to fulfill.... btw, I am a bit embarrassed because both of your solutions are simple, concise and clear! I am very grateful to both, even if I can "accept" only one answer, many many thanks!
    – Ommo
    Feb 15, 2023 at 11:51

You must log in to answer this question.

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