4

I am new here. I have been using Latex for some time, but I am still facing some problem with tables. I want to create a table exactly as shown in the figure. enter image description here

Please help me out here. Thank you.

My code is below with some details left out:

\documentclass[12p, a4paper]{article}
\usepackage{array}
\usepackage{pdflscape} % Add this package

\usepackage{pgfplots}
\usepackage{siunitx}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{lscape}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}


\begin{table}[h]
    \centering
    
      \begin{tabular}{|c|c|c|c|c|c|c|c|}
      \hline
      Work Package (WP)\\
Deliverable (Dx), \\Milestones (Mx); \\x is 1...n &Tasks& \multicolumn{6}{|c|}{Each cell represents a period of 6 months}\\
\hline
& &Q1&Q2&Q3&Q4&Q5&Q6\\
\hline
WP-1:Project\\
management\\
D1: Theoretical Model \\
(analytical and numerical solutions)& & abc& & & & & \\
\hline
 \multirow{3}{*}{WP-2:
D2: Optimal material  for seismic applications}& & abc& & & & & \\
\cline{2-8}
        &A&2&95&96& & & &\\\cline{2-8}
        &B&100&100&100&&B&100&100&100\\\hline



      \end{tabular}
\end{table}
13
  • 6
    Have a look at the tabularray package. It makes it easy to merge and colour cells. Commented Jun 4 at 10:05
  • 1
    Welcome to TeX Stack Exchange. Please provide some more details, including what you have tried. Please also share the class you're using (eg, article): packages are available, but can have different behaviour/compatibility in different classes. Ideally, you should provide a minimal working example of your set-up
    – Sam OT
    Commented Jun 4 at 10:16
  • 2
    Thank you. // Please do this test: copy your posted code into a new file; compile; add what's missing until it compiles. Then please update the code you posted. Thank you
    – MS-SPO
    Commented Jun 4 at 11:34
  • 3
    Currently we do not know, which packages you are already using. So how can we know, how to make the table without adding any new package? And note, if you've not yet added a color package, the question cannot be answered without adding a new package. The restriction to packages already in use is therefore rarely useful and surely not, if we know nothing about the used class and packages. So you should at least complete your code to a minimal working example and a list of packages that are allowed to be used.
    – cabohah
    Commented Jun 4 at 11:49
  • 2
    Sorry, but I still don't know, which packages would be old enough in your opinion. Would, e.g., nicematrix be old enough or makecell? Would tikzmark be old enough? “Usual packages” is not really specific but very opinion based. For me tabularray is a usual package for colorful tables with merged cells.
    – cabohah
    Commented Jun 4 at 12:53

2 Answers 2

4

Here is a start. I think the rest is just more variations on the same themes.

I left out the packages that are not necessary for this solution.

\documentclass{article}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage{array}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage[english]{babel}
\newcommand\BC{\multicolumn{1}{c}{}} %cBlank Cell
\newcommand\CC{\multicolumn{1}{|c|}} % Centered Cell
\begin{document}

\begin{table}[h]
  \centering
    
      \begin{tabularx}{\textwidth}{|X|X|c|c|c|c|c|c|}
        \cline{1-2}
        \CC{\textbf{Work Package (WP)}} & \multicolumn{1}{c|}{\textbf{Tasks}} \\ \cline{3-8}
        \CC{\emph{Deliverable (Dx), Milestones}} &  & \multicolumn{6}{p{0.27\textwidth}|}{\multirow{2}{=}{\textbf{Each cell represents a period of 6 months}}}\\
        \CC{\emph{(Mx); x is 1...n}} & &\BC &\BC &\BC &\BC &\BC &\\ \cline{3-8}
         & & \textbf{Q1} & \textbf{Q2} & \textbf{Q3} & \textbf{Q4} & \textbf{Q5} & \textbf{Q6} \\
        \hline
        \textbf{WP-1:Project} & & & & & & & \\
        \textbf{management} & & & & & & & \\
        \textbf{D1:} \emph{Theoretical Model} & & & & & & & \\
        \emph{(analytical and numerical solutions}) & & abc& & & & & \\
        \hline
        \multirow{3}{=}{\textbf{WP-2:}\\
        \textbf{D2:} \emph{Optimal material for seismic applications}} & & abc& & & & & \\
        \cline{2-8}
                     & A & 2 & 95 & 96 & & & \\ \cline{2-8}
                     & B & 100 & 100 & 100 & B & 100 & 100 \\ 
        \hline
        \multirow{3}{=}{\textbf{WP-3:}\\
        \textbf{D3:} Optimal structural design for seismic applications} & & & & & & & \\ 
                             & & & & & & & \\ \cline{2-8}
                             & & & & & & & \\ \cline{2-8}
        \multirow{2}{=}{\textbf{M1:} Material, Shape, Size and Arrangement in Metastructure} & & & & & & & \\ \cline{2-8}
                      & & & & & & & \\ 
        \hline
      \end{tabularx}
    \end{table}

\end{document}

enter image description here

1
  • Thank you Pieter so much. Your solution is what I was trying to do initially. I have used some of the things from Mane32's answer and I got exactly what I wanted. Commented Jun 6 at 4:52
6

As an example of tabularray and functional usage:

The following MWE uses x (or any other string) in the code of the table to color it (similar to what I do to draft a project programation with pen and paper).

I also create 3 commands to make easier the typesetting of your WP, Dx and Mx (instead of flooding your source code with \textbf and \textit).

And the star of the code is the functional function \qColor. This macro scans row by row, cell by cell (column by column) the contents of each cell and if its not empty (it has an "x" in the example) it sets the cell bg to mycolor and clears the cell contents.

Suggestions are welcome as usual

\documentclass{report}
\usepackage[margin=2.5cm]{geometry}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{functional}
%% Commands for Typesetting stuff in table
\NewExpandableDocumentCommand{\WP}{m m}{\textbf{WP-#1: #2}}
\NewExpandableDocumentCommand{\Dx}{O{1} m}
{\textbf{D\textsubscript{#1}:} \textit{#2}}
\NewExpandableDocumentCommand{\Mx}{O{1} m}
{\textbf{M\textsubscript{#1}:} \textit{#2}}
%% Functional for coloring cells
\IgnoreSpacesOn
\definecolor{mycolor}{HTML}{91c1eb}
\newcommand{\mystr}{x}
\prgNewFunction \qColor {} {
    \intStepOneInline {3} {\arabic{rowcount}} {
        \intStepOneInline {3} {\arabic{colcount}} {
            \strSet \lTmpaStr {\cellGetText {##1} {####1} }
            \strVarIfEmptyF \lTmpaStr {
                \cellSetStyle{##1}{####1}{bg=mycolor}
                \cellSetText{##1}{####1}{}
            }
        }   
    }
}
\IgnoreSpacesOff
\begin{document}
    \centering
    \begin{tblr}[
    expand = \expanded,
    ]{
    hspan=minimal,
    vspan=even,
    colspec = {X[h]X[h]*{6}{c}},
    width = \linewidth,
    hlines = {0.6pt},
    vlines = {0.6pt},
    cell{1}{1,2} = {r=2}{c},
    cell{1}{3} = {c=6}{l},
    row{1} = {font=\bfseries},
    process = \qColor,
    }
     {Work package (WP)\\
     \mdseries\itshape
     Deliverables (D\textsubscript{x}),\\
     Milestones (M\textsubscript{x}),\\
     x is 1,...,n}
     & Task & Each cell represents a period of 6 months \\
     &      & Q1 & Q2 & Q3 & Q4 & Q5 & Q6 \\
     {
     \WP{1}{Project Management}\\
     \Dx[1]{Theoretical Model (analitical and numerical solutions)}
     }
     &   & x & x & x & x & x & x \\
     {\SetCell[r=2]{}
        \WP{2}{}\\
        \Dx[2]{Optimal material for seismic applications}
     }
     &   & x & x &   &   &   &   \\
     &   & x & x &   &   &   &   \\
     {\SetCell[r=4]{}
        \WP{3}{}\\
        \Dx[3]{Optimal structural design for seismic applications}\\
        \Mx[1]{Material, shape, size, and arrangment in metaestructure}
     }            
     &   & x & x &   &   &   &   \\
     &   &   & x & x &   &   &   \\
     &   &   &   & x &   &   &   \\
     &   &   &   & x &   &   &   \\
     {\SetCell[r=2]{}
        \WP{4}{Material and mechanical characterization}\\
        \Dx[4]{Structure-Property relationships for selected material}
     }            
     & Microscopy analysis          &   &   & x & x &   &   \\
     & Mechanical testing           &   &   & x & x &   &   \\
     {\SetCell[r=2]{}
        \WP{5}{}\\
        \Dx[5]{Physical scaled-down models of metaestructure}
     }            
     & Fabrication of varios parts  &   &   & x & x &   &   \\
     & Unit cells assembly          &   &   & x & x &   &   \\
    \end{tblr}
\end{document}

enter image description here

EDIT: The above code works by checking if the cell is empty. If it is not empty, the qColor function colors the cell and then empties it, ensuring that only empty cells are possible (either originally empty or colored and then emptied).

However, the original poster (OP) has changed the requirements and now wants colored cells that can either be empty or contain text. The following code now checks if the cell is empty, and if it is not, colors it. Additionally, it checks if the content of the cell is a "marker" text (e.g., "x"), and only if that condition is true, the cell is emptied. I have also added some improvements to the documentation of my approach.

Please note that this answer is merely a demonstration of the tabularray and functional packages. The OP could have achieved the same results using \SetCell{bg=mycolor}. My goal was to explore whether a more "lazy/automated" solution was possible.

\documentclass{report}
\usepackage[margin=2.5cm]{geometry}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{functional}
%% Commands for Typesetting stuff in table
\NewExpandableDocumentCommand{\WP}{m m}{\textbf{WP-#1: #2}}
\NewExpandableDocumentCommand{\Dx}{O{1} m}
{\textbf{D\textsubscript{#1}:} \textit{#2}}
\NewExpandableDocumentCommand{\Mx}{O{1} m}
{\textbf{M\textsubscript{#1}:} \textit{#2}}
%% Functional for coloring cells
\IgnoreSpacesOn
% Color definition:
\definecolor{mycolor}{HTML}{91c1eb}
\prgNewFunction \qColor {} {
    % Set "Marker" (Change x for whatever character or word you want)
    \tlNew \markerTl
    \tlSet \markerTl {x}
    % For loop from 3 (first row of the colored grid) to the table last row (last row of the colored grid)
    \intStepOneInline {3} {\arabic{rowcount}} {
        % For loop from 3 (first column of the colored grid) to the table last column (last column of the colored grid)
        \intStepOneInline {3} {\arabic{colcount}} {
            % A scratch token list variable set to the content of the current scanned cell
            \tlSet \lTmpaTl {\cellGetText {##1} {####1} }
            % Test if the cell is empty
            \tlVarIfEmptyF \lTmpaTl {
                % if NOT empty (hence the F in tlVarIfEmptyF) change the cell background color to mycolor
                \cellSetStyle{##1}{####1}{bg=mycolor}
                % Check if the contents of the cell are the marker
                \tlVarIfEqT \lTmpaTl \markerTl {
                    % If True (Hence the T), empty the cell
                    \cellSetText{##1}{####1}{}
                }
            }
        }
    }
}
\IgnoreSpacesOff
\begin{document}
    \centering
    \begin{tblr}[
    expand = \expanded,
    ]{
    hspan=minimal,
    vspan=even,
    colspec = {X[h]X[h]*{6}{c}},
    width = \linewidth,
    hlines = {0.6pt},
    vlines = {0.6pt},
    cell{1}{1,2} = {r=2}{c},
    cell{1}{3} = {c=6}{l},
    row{1} = {font=\bfseries},
    process = \qColor,
    }
     {Work package (WP)\\
     \mdseries\itshape
     Deliverables (D\textsubscript{x}),\\
     Milestones (M\textsubscript{x}),\\
     x is 1,...,n}
     & Task & Each cell represents a period of 6 months \\
     &      & Q1 & Q2 & Q3 & Q4 & Q5 & Q6 \\
     {
     \WP{1}{Project Management}\\
     \Dx[1]{Theoretical Model (analitical and numerical solutions)}
     }
     &   & x & x & x & x & x & x \\
     {\SetCell[r=2]{}
        \WP{2}{}\\
        \Dx[2]{Optimal material for seismic applications}
     }
     &   & x & x &   &   &   &   \\
     &   & x & x &   &   &   &   \\
     {\SetCell[r=4]{}
        \WP{3}{}\\
        \Dx[3]{Optimal structural design for seismic applications}\\
        \Mx[1]{Material, shape, size, and arrangment in metaestructure}
     }
     &   & x & x &   &   &   &   \\
     &   &   & x & x &   &   &   \\
     &   &   &   & x &   &   &   \\
     &   &   &   & x &   &   &   \\
     {\SetCell[r=2]{}
        \WP{4}{Material and mechanical characterization}\\
        \Dx[4]{Structure-Property relationships for selected material}
     }
     & Microscopy analysis          &   &   & x & x &   &   \\
     & Mechanical testing           &   &   & x & x &   &   \\
     {\SetCell[r=2]{}
        \WP{5}{}\\
        \Dx[5]{Physical scaled-down models of metaestructure}
     }
     & Fabrication of varios parts  &   &   & x & x &   &   \\
     & Unit cells assembly          &   & abc & test &   & X & x  \\
    \end{tblr}
\end{document}

New Output is:

enter image description here

7
  • Thank you so much. It runs fine. However, when I am trying to put some words on the colored portions, they are not appearing. Commented Jun 5 at 6:23
  • @GauravMaurya well, that is kinda the whole point of my answer. I designed it under the assumption that you don't want to put text in the part corresponding to the Q1, Q2, etc.
    – Mane32
    Commented Jun 5 at 6:30
  • If you want to keep the text you are going to put, you can comment or delete the line with Cell SetText in the inside of the prgNewFunction definition
    – Mane32
    Commented Jun 5 at 6:32
  • Thank you for your response. I did that, but what is happening now is where I am writing, it is getting coloured, but other spaces are still white. What I want is that in some places, there will be empty cells with colours, and then there will be non-empty cells filled with the same colour. Commented Jun 5 at 6:54
  • Well, my answer was a showcase of how you can automate your table colouring using tabularray and functional. I will update my answer tomorrow to try to implement your additional req
    – Mane32
    Commented Jun 5 at 7:37

You must log in to answer this question.

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