2

I'm trying to make a Gantt chart using the NiceTabular environment, but when trying to combine multicolumn and cellcolor I am having issues. Here's a MWE

\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{multicol}
\usepackage{nicematrix}

\begin{document}

\noindent
\begin{NiceTabular}{|c|l|*{10}c|}[color-inside]
  \cline{1-12}
     \multicolumn{2}{c}{} & \multicolumn{2}{c|}{Year 1} &   \multicolumn{2}{c|}{Year 2} & \multicolumn{2}{c|}{Year 3}& \multicolumn{2}{c|}{Year 4}& \multicolumn{2}{c|}{Year 5}\\
    \cline{1-12}
  \multirow{3}{*}{Topic 1} &Project 1.1 & \multicolumn{4}{c|}{\cellcolor{green!60} }& \multicolumn{6}{c|}{}\\
  \cline{2-12}
  & Project 1.2 & \multicolumn{3}{c|}{}& \multicolumn{3}{c|}{\cellcolor{red!60} }& \multicolumn{4}{c|}{}\\
  \cline{2-12}
  & Project 1.3 & \multicolumn{6}{c|}{}& \multicolumn{4}{c|}{\cellcolor{orange!60}}\\
  \cline{1-12}
\end{NiceTabular}

\end{document}

This is the result

There are two problems:

  1. Of the three cells that are supposed to be colored, only one appears
  2. The one that appears should start at the middle of Year 2, but appears at the beginning

How can these two things be fixed?

2 Answers 2

5

The environment {NiceTabular} of nicematrix tries to be compatible with the standard technics provided by LaTeX and the standard packages array, tabularx, multirow, booktabs. However, there are some incompatibilities. For instance, you can't use \cellcolor in the argument of a \multicolumn. However, it's possible to use the command \Block provided by nicematrix.

In your case, here is what I would write with nicematrix.

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\noindent
\begin{NiceTabular}{cl*{10}{w{c}{6mm}}}[hvlines]
 \Block{1-2}{} && \Block{1-2}{Year 1} && \Block{1-2}{Year 2} && \Block{1-2}{Year 3} && \Block{1-2}{Year 4}
&& \Block{1-2}{Year 5} & \\
\Block{3-1}{Topic 1} & Project 1.1  & \Block[fill=green!60]{1-4}{} &&&& \Block{1-6}{} \\
& Project 1.2 & \Block{1-3}{} &&& \Block[fill=red!60]{1-3}{} &&& \Block{1-4}{}{} &&& \\
& Project 1.3 & \Block{1-6}{} &&&&&& \Block[fill=orange!60]{1-4}{} \\
\end{NiceTabular}

\end{document}

Output of the above code


With nicematrix you can also create that table with only one column for each year (Year 1, Year 2, etc.).

Indeed, it's possible to draw the red rectangle by using directly TikZ and the PGF/TikZ nodes created by nicematrix. The node 4.5 corresponds to the median position of the fourth column of the tabular.

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{nicematrix,tikz}

\begin{document}

\noindent
\begin{NiceTabular}{cl*{5}{w{c}{12mm}}}[hvlines]
 \Block{1-2}{} && Year 1 & Year 2 & Year 3 & Year 4 & Year 5  \\
\Block{3-1}{Topic 1} & Project 1.1  & \Block[fill=green!60]{1-2}{} && \Block{1-3}{} \\
& Project 1.2 & \Block{1-5}{} \\
& Project 1.3 & \Block{1-3}{} &&& \Block[fill=orange!60]{1-2}{} \\
\CodeAfter
  \tikz \draw [fill=red!60] (3-|4.5) rectangle (4-|6) ; 
\end{NiceTabular}

\end{document}

Output of the second code

1

As possible alternative with use of the tblr of tabularray package:

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{tabularray}
\NewTableCommand\SCC[2]{\SetCell[c=#1]{bg=#2}}

\begin{document}

\noindent%
\begin{tblr}{hlines, vlines,
             colspec = {cl *{10}{X[c]}},
             cell{2}{1} = {r=3}{},
             cell{1}{odd} = {c=2}{}
             }
        &               & Year 1    
                            &   & Year 2    
                                    &   & Year 3    
                                            &   &  Year 4   
                                                    &   & Year 5    
                                                            &   \\
Topic 1 & Project 1.1   & \SCC{4}{green!60} 
                            &   &   &   & \SetCell[c=6]{}   
                                            &   &   &   &   &   \\
        & Project 1.2   & \SetCell[c=3]{}   
                            &   &   & \SetCell[c=3]{red!60} 
                                        &   &   & \SetCell[c=4]{}   
                                                    &   &   &   \\
        & Project 1.3   & \SetCell[c=6]{}
                            &   &   &   &   &   & \SetCell[c=4]{orange!60}
                                                    &   &   &   \\
\end{tblr}

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