4

I am trying to add a list of elements into a longtable cell.
I have tried several solutions and the only one that has proved really applicable is the one I present in the following MWE.
The main problem is that I want to colour the table (with alternating rows) with the colour I have defined, called lightblue.
Here's the example:

\documentclass[a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{longtable}
\usepackage{makecell}

\definecolor{lightblue}{rgb}{0.93,0.95,1.0}

\begin{document}

\subsection{Tracciamento} \label{subsec:tracciamento}
\rowcolors{1}{lightblue}{white}
    \begin{longtable}[c]{|c|c|}
        \hline
            \textbf{Fonte} &
            \textbf{Requisiti} \\
        \hline
            Capitolato & \makecell[t]{RF02\\RFD3\\RFD4} \\
        \hline
            Interno & \makecell[t]{RQO1\\RQO2\\RQD8} \\
        \hline
            UC 1 &
            RF01 \\
        \hline
            UC 2 &
            RF01.1 \\
        \hline
        \caption{Tracciamento fonte - requisiti} \label{tab:fonte_requisiti}
    \end{longtable}
\end{document}

and this is the output after the compilation:
enter image description here

Now, the problem lies in the fact that the colour does not seem to work properly.
I thank anyone who can help me solve this problem!

4 Answers 4

6

You can avoid the problem by using longtblr from the tabularray package:

\documentclass[a4paper]{article}

\usepackage[table]{xcolor}
\definecolor{lightblue}{rgb}{0.93,0.95,1.0}
\usepackage{tabularray}

\begin{document}

\subsection{Tracciamento} 
\label{subsec:tracciamento}

    \begin{longtblr}[
      caption={Tracciamento fonte - requisiti}, 
      label={tab:fonte_requisiti}
    ]{
      colspec={cc},
      row{odd}={lightblue},
      row{1}={font=\bfseries},
      vlines,
      hlines
    }
      Fonte & Requisiti \\
      Capitolato & {RF02\\RFD3\\RFD4} \\
      Interno & {RQO1\\RQO2\\RQD8} \\
      UC 1 & RF01 \\
      UC 2 & RF01.1 \\
    \end{longtblr}
\end{document}

enter image description here

2
4

Here is a solution with {NiceTabular} of nicematrix. I have replaced in your code {longtable} by {NiceTabular} and I have put your instruction \rowcolors{1}{lightblue}{white} in the so-called \CodeBefore of the environment {NiceTabular}.

\documentclass[a4paper]{article}
\usepackage{xcolor}
\usepackage{makecell}
\usepackage{nicematrix}

\definecolor{lightblue}{rgb}{0.93,0.95,1.0}

\begin{document}

\subsection{Tracciamento} \label{subsec:tracciamento}

    \begin{NiceTabular}[c]{|c|c|}
    \CodeBefore
        \rowcolors{1}{lightblue}{}
    \Body
        \hline
            \textbf{Fonte} &
            \textbf{Requisiti} \\
        \hline
            Capitolato & \makecell[t]{RF02\\RFD3\\RFD4} \\
        \hline
            Interno & \makecell[t]{RQO1\\RQO2\\RQD8} \\
        \hline
            UC 1 &
            RF01 \\
        \hline
            UC 2 &
            RF01.1 \\
        \hline
    \end{NiceTabular}
\end{document}

As usual with nicematrix, you need several compilations.

Output of the above code

2

Inner tabular can sort it out. You could also create a macro for convenience:

\documentclass[a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{longtable}
\usepackage{makecell}

\definecolor{lightblue}{rgb}{0.93,0.95,1.0}

\NewDocumentCommand\TB{O{c}O{c}m}{%
  \renewcommand\arraystretch{1}%
  \begin{tabular}[#1]{@{}#2@{}}#3\end{tabular}}


\begin{document}
\renewcommand*{\arraystretch}{1.25}

\subsection{Tracciamento} \label{subsec:tracciamento}
\rowcolors{1}{lightblue}{white}
\begin{longtable}[c]{|c|c|}
  \hline
  \textbf{Fonte} & \textbf{Requisiti} \\
  \hline
  Capitolato     & \TB[t]{RF02\\RFD3\\RFD4} \\
  \hline
  Interno        & \TB[t]{RQO1\\RQO2\\RQD8} \\
  \hline
  UC 1           & RF01 \\
  \hline UC 2    & RF01.1 \\
  \hline
  \caption{Tracciamento fonte - requisiti} \label{tab:fonte_requisiti}
\end{longtable}
\end{document}

enter image description here

1

One more solution with use of tabularray package. From other it differ In the following:

  • added is new package tblr-extras for writing captions with style defined by caption package
  • added is option for repeating table column headers on each new package

Just curious: why you need long table? From showed document example is not seems that it is necessary. It can be standard table inserted in float table. Anyway, MWE below show its use, wheb it should be broken between pages.

\documentclass{article}

\usepackage{xcolor}
\definecolor{lightblue}{rgb}{0.93,0.95,1.0}
\usepackage{tabularray}
\usepackage{tblr-extras}    %%%% new, should be last version from 2024.06.30 
\UseTblrLibrary{caption}    %%%% new   
\usepackage{caption}
\captionsetup[table]{skip=0pt,
                     font = {small, sf},
                     labelfont = bf,
                     %justification=centerlast
                    }
\usepackage{lipsum}


\begin{document}

\subsection{Tracciamento} 
\label{subsec:tracciamento}
\lipsum[1-3]

\bigskip
Long table is broken between two pages \dots
    \begin{longtblr}[
      caption={Tracciamento fonte - requisiti}, 
      label={tab:fonte_requisiti}
    ]{colspec={cc},
      row{odd}={lightblue},
      row{1}={font=\bfseries},
      hlines, vlines,
      rowhead = 1
      }
Fonte       & Requisiti             \\
Capitolato  & {RF02\\RFD3\\RFD4}    \\
Interno     & {RQO1\\RQO2\\RQD8}    \\
UC 1        & RF01                  \\
UC 2        & RF01.1                \\
    \end{longtblr}
\end{document}

enter image description here

You must log in to answer this question.

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