# Problem in table with fixed width columns and multicolumn

I want to do a diagram following the idea of How to create fixed width table columns with text raggedright/centered/raggedleft?. Using the code

\documentclass{article}

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{tabular}{| C{2cm} | C{2cm} | C{2cm} | C{2cm} | C{2cm} | C{2cm} |}
\hline
$f(x)$   & \multicolumn{5}{c|}{+}\\
\hline
$f'(x)$  & \multicolumn{2}{c|}{+} & \multicolumn{2}{c|}{-} & +\\
\hline
$f''(x)$ & - & \multicolumn{2}{c|}{-} & \multicolumn{2}{c|}{+}\\
\hline
\end{tabular}

\end{document}


the widths are ignored:

But adding a multicolumn-less row:

\documentclass{article}

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{table}[h]
\begin{tabular}{| C{2cm} | C{2cm} | C{2cm} | C{2cm} | C{2cm} | C{2cm} |}
\hline
$f(x)$   & \multicolumn{5}{c|}{+}\\
\hline
$f'(x)$  & \multicolumn{2}{c|}{+} & \multicolumn{2}{c|}{-} & +\\
\hline
$f''(x)$ & + & \multicolumn{2}{c|}{-} & \multicolumn{2}{c|}{+}\\
\hline
U & V & W & X & Y & Z\\
\hline
\end{tabular}
\end{table}

\end{document}


the widths are OK:

What I'm missing?

• your C column specifcations are not used at all after the first column as you are removing them all, specifiying alternative column specifications with multicolumn Mar 28, 2021 at 10:25
• @DavidCarlisle, yes, but... using "C" instead "c" gives a lot of "Illegal unit of measure" errors. Mar 28, 2021 at 10:29
• Obviously you can't use C on its own as you have defined it to take an argument, but C{\dimexpr4cm+2\tabcolsep+\arrayrulewidth} would make a column the width of a two column span Mar 28, 2021 at 10:31

I suggest you use (a) an array environment rather than a tabular environment (since the table's contents are all in math mode) and (b) use the w column type to specify the column widths as no line-breaking within columns would seem necessary or desirable. Since the tedious-looking

\multicolumn{2}{w{c}{\dimexpr2\mylen+2\arraycolsep+\arrayrulewidth\relax}|}


expression occurs no fewer four times, I further suggest replacing it with a shortcut macro called, say, \mycol.

\documentclass{article}
\usepackage{array}  % for 'w' column type
\newlength\mylen
\setlength\mylen{1cm} % <-- set the desired usable column width (2cm seems excessive)
% handy shortcut macro:
\newcommand\mycol[1]{%
\multicolumn{2}{w{c}{\dimexpr2\mylen+2\arraycolsep+\arrayrulewidth\relax}|}{#1}}
\begin{document}
$% start an unnumbered display-math group \begin{array}{| *{6}{w{c}{\mylen}|} } \hline f(x) & \multicolumn{5}{c|}{+} \\ \hline f'(x) & \mycol{+} & \mycol{-} & + \\ \hline f''(x) & - & \mycol{-} & \mycol{+} \\ \hline \end{array}$
\end{document}


You can easily construct that array with {NiceMatrix} of nicematrix. That environment is similar to {nicematrix} (of amsmath) but with additionnal features:

• You can specify the width of the columns with the key columns-width.
• You can merge cells (both vertically and horizontally) with the command \Block.
• The key hvlines draws all the rules (except in the blocks created by \Block).

Here is the code.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
$\begin{NiceMatrix}[columns-width=1cm,hvlines] f(x) & \Block{1-5}{+} \\ f'(x) & \Block{1-2}{+} && \Block{1-2}{+} && + \\ f''(x) & - & \Block{1-2}{-} && \Block{1-2}{+} \\ \end{NiceMatrix}$
\end{document}