Numbers with two digits exceeds the cell in tabularx
Instead of fixing the column width of the 12 final columns, set the content in regular c
entred (non-fixed-width) column. And then use collcell
to capture the content of the columns and set them in a box that naturally fits to the widest element. This latter max-width-box-fixing is done using eqparbox
's \eqmakebox[<tag>]{<stuff>}
. Elements using the same <tag>
are set in a box of the same, naturally-widest, box.
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx,collcell,eqparbox}
\newcommand{\fixwidth}[1]{\eqmakebox[colhead]{#1}}
\newcolumntype{C}{>{\collectcell\fixwidth}c<{\endcollectcell}}
\newcommand{\graycell}{\cellcolor{gray}}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{ | >{\centering\arraybackslash}X | *{12}{C|} }
\hline
& \multicolumn{12}{c|}{Months} \\ \cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\ \hline
Activity 1 & \graycell & \graycell & \graycell & & & & & & & & & \\ \hline
Activity 2 & & \graycell & \graycell & & & & & & & & & \\ \hline
Activity 3 & & & \graycell & \graycell & \graycell & & & & & & & \\ \hline
Activity 4 & & & & & \graycell & \graycell & & & & & & \\ \hline
Activity 4 & & & & & & & \graycell & \graycell & \graycell & & & \\ \hline
Activity 4 & & & & & & & & & \graycell & \graycell & \graycell & \graycell \\ \hline
\end{tabularx}
\caption{Cronogram}
\end{table}
\end{document}
If you want multiple such tables that have varying fixed-width, one can also achieve this using manual insertion of \eqmakebox
with an appropriate table-specific <tag>
.
If you truly want the columns to be 0.1cm
(or 1mm
), you can use the following definitions in your preamble:
\newcommand{\fixwidth}{\makebox[0pt]}
\newcolumntype{C}{>{\centering\arraybackslash\collectcell\fixwidth}p{1mm}<{\endcollectcell}}
No need to use eqparbox
then.
You seem to be under the impression that if one specifies the column type as m{1mm}
, the column's width will be 1mm
. In fact, that's merely its usable width; its full width is 1mm+2\tabcolsep
; the length parameter \tabcolsep
(default value: 6pt
) governs the amount of whitespace that's inserted to the left and right of each column. Since 72.27 TeX points = 1 inch, 1 TeX point is equal to about 0.3515mm, and the total width of each m{1mm}
column is actually 1mm+12*0.3515mm=5.22mm
.
If the font is Computer Modern and the font size is 11pt
, the width of each and every numeral is 5.475pt
. Given that you've specified a usable column width of only 1mm=2.84pt
, it's not surprising that you get lots of warning messages about overfull boxes and that the two-digit numbers protrude into the following cells.
I take it that (a) you'd like to keep the month columns as narrow as possible and also (b) all 12 month columns should be equally wide. If that's the case, I suggest you take the following approach: Set \tabcolsep
to a very small value, say, 0.5pt
, and set the usable width of each month column to the width of the widest material in the header cells -- here, the number 12
. The width of the number 12
is 2*5.475pt=10.95pt; the total width of the cell will be 10.95pt+2*0.5pt=11.95pt=4.2mm
-- about 1mm less than in your initial setup. Keep using a tabularx
environment to maximize the usable width of the first column.
\documentclass[a4paper,11pt]{article}
% I've simplified and streamlined the preamble
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
% Some new code
\newlength\mylength
\settowidth\mylength{12}
\newcommand\cg{\cellcolor{gray}} % handy shortcut macro
\begin{document}
\begin{table}[htbp]
\begin{tabularx}{\textwidth}{|C|
*{12}{>{\centering\arraybackslash}m{0.1cm}|}}
\hline
& \multicolumn{12}{c|}{Months} \\
\cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\
\hline
Activity 1 & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & & & & & & & \\
\hline
Activity 2 & & \cellcolor{gray} & \cellcolor{gray} & & & & & & & & & \\
\hline
Activity 3 & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & & & & & \\
\hline
Activity 4 & & & & & \cellcolor{gray} & \cellcolor{gray} & & & & & &\\
\hline
Activity 4 & & & & & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & \\
\hline
Activity 4 & & & & & & & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} \\
\hline
\end{tabularx}
\caption{Original form}
\end{table}
\setlength\tabcolsep{0.5pt} %default is 6pt
\begin{table}[htbp]
\begin{tabularx}{\textwidth}{|C|
*{12}{>{\centering\arraybackslash}m{\mylength}|}}
\hline
& \multicolumn{12}{c|}{Months} \\
\cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\
\hline
Activity 1 & \cg & \cg & \cg & & & & & & & & & \\
\hline
Activity 2 & & \cg & \cg & & & & & & & & & \\
\hline
Activity 3 & & & \cg & \cg & \cg & & & & & & & \\
\hline
Activity 4 & & & & & \cg & \cg & & & & & &\\
\hline
Activity 4 & & & & & & & \cg & \cg & \cg & & & \\
\hline
Activity 4 & & & & & & & & & \cg & \cg & \cg & \cg \\
\hline
\end{tabularx}
\caption{Modified form}
\end{table}
\end{document}