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!

3 Answers 3

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}.

\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}{white}
    \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

1

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

You must log in to answer this question.

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