# How to do a table with fixed columns and cells spanning multiple columns?

I would like to do a table like this:

I have found how to fix the column widths in: How to create fixed width table columns with text raggedright/centered/raggedleft?, How to create a table with fixed column widths and Center a fixed width text within a multicolumn .

However, when I try to merge multiple cells together with multicolumn, the column width fails:

\documentclass[10pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}

\begin{document}
\begin{table}[!ht]
\centering
\begin{tabularx}{\linewidth}{l|X|X|X|X}
\toprule
Activity & \multicolumn{2}{|c|}{1st year} & \multicolumn{2}{c}{2nd year} \\
& 1st semester & 2nd semester & 1st semester & 2nd semester  \\
\midrule
I. &  Perform some experiment, do some calculations, write a report and present the results in a conference.
& & \\
II. &  \multicolumn{3}{|c|}{Perform some experiment, do some calculations, write a report and present the results in a conference.}
& \\
III. &  & \multicolumn{3}{|c|}{Perform some experiment, do some calculations, write a report and present the results in a conference.} \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}


How can I manage to do a table with fixed columns and cells spanning multiple columns?

• Could you please post the code used? – Bernard May 30 at 12:16
• @Bernard I updated the post with the code I tried. – Puco4 May 30 at 12:33
• Entirely unrelated to the issue, but please keep in mind that horizontal lines from the booktabs package are intended to be used without vertical lines. This is thereason for the small gaps in the vertical lines around the intersections. – leandriis May 30 at 12:37

With the help of \newcolumntype{L}{>{\raggedright\arraybackslash}X} in defined a new column type L. This column type is based on the X type, but its contents are left aligned instead of justified. (See also: How to create fixed width table columns with text raggedright/centered/raggedleft?)

In order to get the correct width for the \multicolumn cells, I used >{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}L. Since the \multicolumn cell spans 3 L (or X) columns, we can start with >{\hsize=3\hsize}L (See also: Table layout with tabularx (column widths: 50%|25%|25%)). To this, we then add 4\tabcolsep (\tabcolsep is the small amount of horizontal white space that is added to either side of the text inside of a table cell) as well as 2\arrayrulewidth(\arrayrulewidthis the width of the vertical lines in the table.)

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}
\begin{tabularx}{\linewidth}{|l|L|L|L|L|}
\hline
Activity & \multicolumn{2}{c|}{1st year} & \multicolumn{2}{c|}{2nd year} \\ \cline{2-5}
& 1st semester & 2nd semester & 1st semester & 2nd semester  \\
\hline
I. &  Perform some experiment, do some calculations, write a report and present the results in a conference.
& & &\\ \hline
II. &  \multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}L|}{Perform some experiment, do some calculations, write a report and present the results in a conference.}
& \\ \hline
III. &  & \multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}L|}{Perform some experiment, do some calculations, write a report and present the results in a conference.} \\
\hline
\end{tabularx}
\end{table}
\end{document}


Here is an entirely different approach based on the pgfgantt package:

\documentclass{article}
\usepackage{pgfgantt}

\begin{document}

\noindent
\begin{ganttchart}[
y unit title=0.5cm,
y unit chart=1.25cm,
x unit =1.75cm,
vgrid,hgrid,
title height=1,
title label font=\footnotesize,
bar height=0.5,
group right shift=0,
group top shift=0.7,
bar label node/.style={text width=4.75cm,
align=right,
anchor=east,
font=\footnotesize\raggedleft}
]{1}{4}

\gantttitle{1st year}{2}
\gantttitle{2nd year}{2}         \\
\gantttitle{1st semester}{1}
\gantttitle{2st semester}{1}
\gantttitle{1st semester}{1}
\gantttitle{2st semester}{1}\\
\ganttbar{Perform some experiment, do some calculations, write a report and present the results in a conference.}{1}{1}\\
\ganttbar{Perform some experiment, do some calculations, write a report and present the results in a conference.}{1}{3}\\
\ganttbar{Perform some experiment, do some calculations, write a report and present the results in a conference.}{2}{4}
\end{ganttchart}

\end{document}

• I am not able to understand \newcolumntype{L}{>{\raggedright\arraybackslash}X} and \multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep}L|}. Could you explain it? – Puco4 May 30 at 12:44
• @Puco4: I added some short comments on both code pieces. I hope this helps. If you have some further questions, feel free to ask. I also added an entirely different approach based on the pgfgantt package. – leandriis May 30 at 13:04