How does multicolumn allocate column widths?

I would like to make a table that looks like the first two rows of the left MWE below, but instead I get unequally spaced columns as in the right one. What is the reason for this behaviour, and how can it be avoided?

\documentclass{article}
\begin{document}

\begin{tabular}{|c|c|c|c|}
\hline
1&\multicolumn{2}{|c|}{23}&4\\
\hline
\multicolumn{2}{|c|}{12}&\multicolumn{2}{c|}{34}\\
\hline
&&&\\
\hline
\end{tabular}

\begin{tabular}{|c|c|c|c|}
\hline
1&\multicolumn{2}{|c|}{23}&4\\
\hline
\multicolumn{2}{|c|}{12}&\multicolumn{2}{c|}{34}\\
\hline
\end{tabular}
\end{document}


If the spanning entry is wider than the columns that it spans, then the last spanned column (only) is extended. this usually look spoor and it is best to ensure that the natural widths of the normal cells is wider than teh natural width of the spanning cell.

Your example is further distorted as the second column is not used at all so teX optimises it away from its calculations. If your real table had a row with all four entries then you get a better result.

\documentclass{article}
\begin{document}

\begin{tabular}{|c|c|c|c|}
\hline
1&\multicolumn{2}{|c|}{23}&4\\
\hline
\multicolumn{2}{|c|}{12}&\multicolumn{2}{c|}{34}\\
\hline
&&&\\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|c|c|c|c|}
\hline
1&\multicolumn{2}{|c|}{23}&4\\
\hline
\multicolumn{2}{|c|}{12}&\multicolumn{2}{c|}{34}\\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|c|c|c|}
\hline
1&\multicolumn{1}{|c|}{23}&4\\
\hline
\multicolumn{1}{|c|}{12}&\multicolumn{2}{c|}{34}\\
\hline
\end{tabular}

\bigskip

\begin{tabular}{|c|c|c|c|}
\hline
1&\multicolumn{2}{c|}{23}&4\\
\hline
\multicolumn{2}{|c|}{12}&\multicolumn{2}{c|}{34}\\
\hline
\omit&&&
\end{tabular}

\end{document}


• I think I understand, but in my first example the row with all four entries has these entries empty anyway (ie, the second column is not used either) and the result is different. Why? – quim Feb 21 '17 at 14:41
• Also, what solution would you suggest? Put some phantom space to enlarge the "12" cell? – quim Feb 21 '17 at 14:41
• @quim updated answer with some code – David Carlisle Feb 21 '17 at 14:43
• @quim no those entries are not empty (they are white but there are tex boxes with \hspace (and vertical rules) If you use \multicolumn over a column border in every row that border is actually deleted from the internal data before tex allocates widths. – David Carlisle Feb 21 '17 at 14:44
• @quim if you post a question on this site and use it in an example, you will be reprimanded in comments for using a Tex primitive in a latex document. Don't tell them I told you:-) – David Carlisle Feb 21 '17 at 14:47