3

When trying to create a table with color-filled cells, it appears that the last cell of each segment has an extra width than the others.

I created the table using this website, the code generated is:

\begin{table}[] \begin{tabular}{|c|l|l|llll|llll|llll|llll|} \hline \textbf{Fase} & \multicolumn{1}{c|}{\textbf{Etapa}} & \multicolumn{1}{c|}{\textbf{Actividades}} & \multicolumn{4}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Semestre\\ 2025-1\end{tabular}}} & \multicolumn{4}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Semestre \\ 2025-2\end{tabular}}} & \multicolumn{4}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Semestre\\ 2026-1\end{tabular}}} & \multicolumn{4}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Semestre\\ 2026-2\end{tabular}}} \\ \hline & & Actividad 1 & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & & & & & & & & & & \\ & & Actividad 2 & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & & & & & & & & & \\ \multirow{-3}{*}{1} & \multirow{-3}{*}{\begin{tabular}[c]{@{}l@{}}Primera etapa del\\ proyecto\end{tabular}} & Actividad 3 & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & & & & & & & & \\ \hline & & Actividad 1 & & & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & & & & & & \\ & & Actividad 2 & & & & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & & & & & \\ \multirow{-3}{*}{2} & \multirow{-3}{*}{\begin{tabular}[c]{@{}l@{}}Segunda etapa del\\ proyecto\end{tabular}} & Actividad 3 & & & & & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & & & & \\ \hline & & Actividad 1 & & & & & & & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & & \\ & & Actividad 2 & & & & & & & & & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & \\ \multirow{-3}{*}{3} & \multirow{-3}{*}{\begin{tabular}[c]{@{}l@{}}Tercera etapa del\\ proyecto\end{tabular}} & Actividad 3 & & & & & & & & & & & & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} \\ \hline \end{tabular} \end{table} 

and the result is

table

As you can see, the cells on the right (for each term) are much wider than the previous three cells.

1
  • 1
    Off-topic: \begin{table}[] is a bad idea; replace with either \begin{table} or \begin{table}[htbp]. Commented Jul 23, 2024 at 23:42

3 Answers 3

3

That is because the cellin the first row (e.g. Semestre\\ 2025-1) is wider than 4 of the black cells. So the last one of the four swallows up the extra space. You can make them a bit wider for example with

\newcommand\black{\cellcolor[HTML]{000000}\hspace*{1mm}} 

and then replace all the \cellcolor[HTML]{000000} in the table by \black. By the way, instead of \cellcolor[HTML]{000000} you can use \cellcolor{black}, which looks a bit nicer.

enter image description here

3

An alternative way for codding your table:

  • without use of tabular generator (which usually produce a lot of clutter code)
  • since you not provide an MWE (Minimal Working Example), a complete small document, by which we will see your document pages layout (your table is quite wide ...)
  • for table use tabularray package
  • for last 16 columns use equal width of columns, defined by X columns
  • for cells coloring define new command which enable to write shorter code
\documentclass{article} \usepackage{geometry} \usepackage{xcolor} \usepackage{tabularray} \NewTableCommand\scb{\SetCell{bg=black}} \begin{document} \begin{table} \begin{tblr}{colspec = {|c|l|l|*{4}{*{4}{X[c]}|}}, cell{1}{4,8,12,16} = {c=4}{}, cell{2,5,8}{1,2} = {r=3}{}, row{1} = {m, font=\small\bfseries} } \hline Fase & Etapa & Actividades & {Semestre\\ 2025-1} & & & & {Semestre \\ 2025-2} & & & & {Semestre\\ 2026-1} & & & & {Semestre\\ 2026-2} & & & \\ \hline 1 & {Primera etapa\\ del proyecto} & Actividad 1 & \scb & \scb & & & & & & & & & & & & & & \\ & & Actividad 2 & & \scb & \scb & & & & & & & & & & & & & \\ & & Actividad 3 & & & \scb & \scb & & & & & & & & & & & \\ \hline 2 & {Segunda etapa\\ del proyecto} & Actividad 1 & & & & & \scb & \scb & & & & & & & & & \\ & & Actividad 2 & & & & & & \scb & \scb & & & & & & & & & \\ & & Actividad 3 & & & & & & & \scb & \scb & & & & & & & \\ \hline 3 & {Tercera etapa\\ del proyecto} & Actividad 1 & & & & & & & & & \scb & \scb & \scb & & & & & \\ & & Actividad 2 & & & & & & & & & & & \scb & \scb & & & & \\ & & Actividad 3 & & & & & & & & & & & & \scb & \scb & \scb & \scb & \scb \\ \hline \end{tblr} \end{table} \end{document} 

enter image description here

1

Here is an alternative way to construct that table with {NiceTabular} of nicematrix (≥ 6.29).

\documentclass{article} \usepackage{nicematrix,tikz} \usepackage{geometry} \begin{document} \begin{NiceTabular}{cll*{4}{X[c]}}[vlines] \Hline \RowStyle{\bfseries} \Block[m]{}{Fase} & \Block[m]{}{Etapa} & \Block[m]{}{Actividades} & Semestre 2025-1 & Semestre 2025-2 & Semestre 2025-3 & Semestre 2025-4 \\ \Hline \Block{3-1}{1} & \Block{3-1}{Primera etapa \\ del proyecto} & Actividad 1 \\ & & Actividad 2 \\ & & Actividad 3 \\ \Hline \Block{3-1}{2} & \Block{3-1}{Segunda etapa \\ del proyecto} & Actividad 1 \\ & & Actividad 2 \\ & & Actividad 3 \\ \Hline \Block{3-1}{3} & \Block{3-1}{Tercera etapa \\ del proyecto} & Actividad 1 \\ & & Actividad 2 \\ & & Actividad 3 \\ \Hline \CodeAfter \begin{tikzpicture} % Fase 1 \fill (2-|4) rectangle (3-|4.5) (3-|4.25) rectangle (4-|4.75) (4-|4.5) rectangle (5-|5) ; % Fase 2 \fill (5-|5) rectangle (6-|5.5) (6-|5.25) rectangle (7-|5.75) (7-|5.5) rectangle (8-|6) ; % Fases 3 y 4 \fill (8-|6) rectangle (9-|6.75) (9-|6.5) rectangle (10-|7) (10-|6.75) rectangle (11-|8) ; \end{tikzpicture} \end{NiceTabular} \end{document} 

Output of the main code

I have created a tabular with 10 rows (one for each actividad) and 7 columns (there is only one column for each semestre).

Here is the same tabular with the names of the cells (obtained by using \ShowCellNames in the \CodeAfter).

Tabular with the names of the cells

Then, I have used Tikz to fill some parts of the cells by using the PGF/TikZ nodes created by nicematrix.

For example, in order to draw the left-half of the cell 2-4), I have used the TikZ instruction:

\fill (2-|4) rectangle (3-|4.5)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.