2

Following some of the answers like color only a cell of a table and Color merged and regular cells in a table individually and some others I tried to do the following:

One cell must have different boxes of different colors having text in them. In the code, it is represented as:

\cellcolor{red!25}AB.C \cellcolor{gray!25}AB.C \cellcolor{orange!25}AB.C

However, it only picks the last one (i.e., orange).

Another view of what I am looking for: image

MWE is as follows: (Please note that it's a long table spanning 3 pages so I followed tabularx)

\documentclass[review]{elsarticle}
\usepackage{ltablex}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{makecell}
\begin{document}
\begin{tabularx}
{\linewidth}{@{}|l|X|X|X|X|X|@{}}
\caption{Table.}
\label{tab:tab} \\
\toprule
\textbf{Col1} & \textbf{Col2}         & \textbf{Col3} & \textbf{Col4} & \textbf{Col5}& \textbf{Col6} \\  
\midrule
\endfirsthead
\toprule
\textbf{Col1} & \textbf{Col2}         & \textbf{Col3} & \textbf{Col4} & \textbf{Col5}& \textbf{Col6} \\ 
\midrule
\endhead
\midrule
\multicolumn{2}{r}{\footnotesize( To be continued)}
\endfoot
\bottomrule
\endlastfoot
\multirow{4}{*}{\thead{DforD(D\&E)}}    & \multirow{3}{*}{A: sometext} & D\&E:A1 & C17, C18, C19       & N/A & sometext                     \\ \cline{3-6} 
 &    & D\&E:A2 & sometext & N/A & sometext\\ \cline{3-6} 
 &    & D\&E:A3 & C16 & N/A & long text \\ \cline{2-6} 
 % & &    &  & &  \\ 
 % D&E:B
  & \multirow{3}{*}{B: sometext} & D\&E:B1 & N/A & N/A & sometext \\ \cline{3-6}
 &    & D\&E:B2-1 & \cellcolor{blue!25}C20, C21 & \cellcolor{red!25}AB.C \cellcolor{gray!25}AB.C \cellcolor{orange!25}AB.C   & long text \\ \cline{3-6} 
 &    & D\&E:B2 & C1  & N/A & long text  \\ \cline{3-6} 
  &    & D\&E:B2-3 & C6, C7  & N/A & long text\\ \cline{3-6}  
&  &    &  & &  \\ 
  &    & D\&E:B2-3 & N/A  &N/A  & long text \\ \cline{2-6} 
 \hline
\end{tabularx}
% \end{landscape}

\end{document}
2
  • You can't put several commands \cellcolor in the same cell. You should split your cell in three cells. Commented May 4 at 12:56
  • Could you please give me some hints? Commented May 4 at 14:07

2 Answers 2

1

With tabularray:

\documentclass[review]{elsarticle}
\usepackage[table]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\DefTblrTemplate{contfoot-text}{default}{\footnotesize( To be continued)}

\begin{document}
\begin{longtblr}[
  caption={Table.},
  label={tab:tab},
  ]{
  colspec={l*5X},
  vlines, hlines,
  column{1}={font=\bfseries},
  row{1}={font=\bfseries},
  rowhead={1},
  }
  Col1 & Col2 & Col3 & Col4 & Col5 & Col6 \\  
  \SetCell[r=10]{m} DforD(D\&E) & \SetCell[r=3]{m} A: sometext & D\&E:A1 & C17, C18, C19       & N/A & sometext \\ 
  & & D\&E:A2 & sometext & N/A & sometext\\
  & & D\&E:A3 & C16 & N/A & long text \\ 
  & \SetCell[r=7]{m} B: sometext & D\&E:B1 & N/A & N/A & sometext \\ 
  & & \SetCell[r=3]{m} D\&E:B2-1 & \SetCell[r=3]{m,blue!25}C20, C21 & \SetCell{red!25}AB.C & \SetCell[r=3]{m}long text \\
  & & & & \SetCell{gray!25}AB.C & \\
  & & & & \SetCell{orange!25}AB.C & \\
  & & D\&E:B2 & C1  & N/A & long text  \\  
  & & D\&E:B2-3 & C6, C7  & N/A & long text\\  
  & & D\&E:B2-3 & N/A  &N/A  & long text \\ 
\end{longtblr}
\end{document}

enter image description here

6
  • While this answer solved the cell coloring problem, it created many issues for the table itself (as I mentioned it is a long table). Can I just stick to tabularx or even tabular and still be able to achieve the cell coloring? Commented May 5 at 9:47
  • 1
    @user3582228 Of course you can, with the same technic (many rows instead of one row). But which issues do you get?
    – CarLaTeX
    Commented May 5 at 10:38
  • Even though I solved the problem with tabularx, however, it has now issues with cellcolor overlapping the table boundary. Commented May 11 at 16:09
  • @user3582228 Please ask another question with a minimal example that shows your problem
    – CarLaTeX
    Commented May 12 at 6:17
  • Maybe this: tex.stackexchange.com/questions/366873/… could be of some help
    – CarLaTeX
    Commented May 12 at 6:30
0

With {NiceTabular} of nicematrix.

\documentclass[review]{elsarticle}
\usepackage{nicematrix}

\begin{document}

\begin{table}
\begin{NiceTabular}
  [caption=Table., label = tab:tab, hvlines, color-inside]
  {>{\bfseries}l*5{X}}
\RowStyle{\bfseries}
  Col1 & Col2 & Col3 & Col4 & Col5 & Col6 \\  
  \Block{*-1}{DforD(D\&E)} & \Block{3-1}{A: sometext} & D\&E:A1 & C17, C18, C19       & N/A & sometext \\ 
  & & D\&E:A2 & sometext & N/A & sometext\\
  & & D\&E:A3 & C16 & N/A & long text \\ 
  & \Block{*-1}{B: sometext} & D\&E:B1 & N/A & N/A & sometext \\ 
  & & \Block{3-1}{D\&E:B2-1} & \Block[fill=blue!25]{3-1}{C20, C21} & \cellcolor{red!25} AB.C & \Block{3-1}{long text} \\
  & & & & \cellcolor{gray!25} AB.C & \\
  & & & & \cellcolor{orange!25} AB.C & \\
  & & D\&E:B2 & C1  & N/A & long text  \\  
  & & D\&E:B2-3 & C6, C7  & N/A & long text\\  
  & & D\&E:B2-3 & N/A  &N/A  & long text \\ 
\end{NiceTabular}
\end{table}

\end{document}

Output of the above code

You must log in to answer this question.

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