# Wrong cell width in table while merging colums

I am trying to create a cell with merged columns but it is not producing cell with the correct width. When I use c or l or r as alignment parameters then it works perfectly if text in cell is of less that cell width, but it the case where text is more than few words it overflow right margin. To resolve this I am using p{with} as alignment parameter but for some reason it is producing cell with smaller cell width.

In the example below 0.84*4+0.97 matches with 4.33 but it is not producing expected results.

\begin{table}[H]
\begin{tabular}{p{0.97in}p{0.84in}p{0.84in}p{0.84in}p{0.84in}}
\multicolumn{1}{p{0.97in}}{\cellcolor[HTML]{FFFFFF}} &
\multicolumn{1}{p{0.84in}}{\cellcolor[HTML]{CCCCCC}\Centering {\fontsize{10pt}{12.0pt}\selectfont col-head-1}} &
\multicolumn{1}{p{0.84in}}{\cellcolor[HTML]{CCCCCC}\Centering {\fontsize{10pt}{12.0pt}\selectfont col-head-2}} &
\multicolumn{1}{p{0.84in}}{\cellcolor[HTML]{CCCCCC}\Centering {\fontsize{10pt}{12.0pt}\selectfont col-head-3}} &
\multicolumn{1}{p{0.84in}}{\cellcolor[HTML]{CCCCCC}\Centering {\fontsize{10pt}{12.0pt}\selectfont col-head-4}} \\

\multicolumn{1}{p{0.97in}}{\cellcolor[HTML]{CCCCCC}\Centering {\fontsize{10pt}{12.0pt}\selectfont row-1}} &
\multicolumn{1}{p{0.84in}}{\Centering YES} &
\multicolumn{1}{p{0.84in}}{\Centering NO} &
\multicolumn{1}{p{0.84in}}{\Centering YES} &
\multicolumn{1}{p{0.84in}}{\Centering YES} \\

\multicolumn{5}{p{4.33in}}{\Centering \cellcolor[HTML]{CCCCCC}{\textbf{ Some random long text, Some random long text, Some random long text,}}} \\
\end{tabular}
\end{table}


The sum of the usable widths of the five individual columns is indeed 0.84in*4+0.97in=4.33in.

However, since there is whitespace padding (in the amount of \tabcolsep) to the left and right of each cell, the usable width of the combination of the five cells is 4.33in+8\tabcolsep.

To make use of this fact, change

\multicolumn{5}{p{4.33in}}{...}


to

\multicolumn{5}{p{\dimexpr4.33in+8\tabcolsep\relax}}{...}


The value of \tabcolsep is generally set by the document class. The "standard" LaTeX document classes -- article, report, and book -- use 6pt as the value.

The main issue has been solved in @Mico's answer, I'd like to address other problems. The table has a lot of noise and unnecessary repeated commands. In LaTeX, you can define your own shortcut macros to do anything used frequently, here is a list of changes you can try:

• define a new macro for colored cells, \CC{..} for example
• remove \centering and define columntype C instead
• remove \fontsize{10pt}{12.0pt}\selectfont and use \small or \footnotesize
• remove first \cellcolor[HTML]{FFFFFF}, default is white
• remove unnecessary \multicolumns
• better to use [!htbp] instead of [H] and let LaTeX decide the recommended place for your table
• you could also try tabularx for automatic hsize instead of p columntype

Here is a cleaned up version of your table:

\documentclass[12pt,a4paper]{article}
%\usepackage{colortbl,xcolor} % OR
\usepackage[table]{xcolor}    % see Mico's comment below
\begin{document}

\newcommand{\CC}[1]{\cellcolor[HTML]{CCCCCC}{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{table}
\begin{tabular}{C{0.97in}C{0.84in}C{0.84in}C{0.84in}C{0.84in}}

• +1. Instead of \usepackage{colortbl,xcolor}, though, do write \usepackage[table]{xcolor}. This maximizes the interoperability between the colortbl and xcolor packages. – Mico Dec 7 '19 at 14:34