# Tabular cells with exact same width and multicolumn

I tried to design a table with some very specific preferences:

• The table should span across the whole width

• Each cell can have a different alignment

• Each cell has exactly the same width

• Some cells are multicolumn cells, 2 combined cells should have exactly the same width as 2 single cells

Here is a small sample of what I've tried:

\documentclass[]{article}

\usepackage{tabularx}

\begin{document}
\noindent
\begin{table}[h!]
\begin{tabularx}{\textwidth}{|X|X|X|X|} \hline
\multicolumn{3}{|l|}{3 cells left}  & \multicolumn{1}{|r|}{1 cell right} \\ \hline
\multicolumn{1}{|r|}{1 cell right}  & \multicolumn{3}{|l|}{3 cells left} \\ \hline
\multicolumn{1}{|r|}{1 cell right}  & \multicolumn{1}{|r|}{1 cell right} & \multicolumn{2}{|r|}{2 cells right} \\ \hline
\multicolumn{1}{|r|}{1 cell right}  & \multicolumn{1}{|r|}{1 cell right} & \multicolumn{1}{|r|}{1 cell right}   & \multicolumn{1}{|r|}{1 cell right} \\ \hline
\end{tabularx}
\end{table}

\end{document}


The first column in this example is significantly smaller than the others. I really hope that somebody can help me.

Your problems stem from the fact that your table, defined with 4 X columns really has none. It suffices to re-define X column type so as to ensure right alignment, and you'll have both a table that looks like what you want, and a simpler code. I also suppressed redundant vertical lines in \multicolumns:

\documentclass[a4paper]{article}

\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{>{\raggedleft\arraybackslash}m{#1}}

\begin{document}

\begin{table}[!h]
\begin{tabularx}{\linewidth}{|X|X|X|X|}
\hline
\multicolumn{3}{|l|}{3 cells left} & 1 cell right \\
\hline
1 cell right & \multicolumn{3}{l|}{3 cells left} \\
\hline
1 cell right & 1 cell right & \multicolumn{2}{r|}{2 cells right} \\
\hline
1 cell right & 1 cell right & 1 cell right & 1 cell right \\
\hline
\end{tabularx}
\end{table}

\end{document}


• The \noindent instruction just ahead of \begin{table} isn't needed.
– Mico
Aug 23 '14 at 13:46
• Yes, it was in the O.P.'s code and I didn't notice it. Line removed Aug 23 '14 at 14:28

Here is a try using \hsize

\documentclass[]{article}

\usepackage{tabularx}
\begin{document}
\noindent
\begin{table}[h!]
\begin{tabularx}{\textwidth}{|X|X|X|X|} \hline
\multicolumn{3}{|>{\hsize=3\hsize}X|}{3 cells left}  &
\multicolumn{1}{>{\raggedleft}X|}{1 cell
right} \\ \hline
\multicolumn{1}{|>{\raggedleft}X|}{1 cell right}  &
\multicolumn{3}{>{\hsize=3\hsize}X|}{3 cells left} \\ \hline
\multicolumn{1}{|>{\raggedleft}X|}{1 cell right}  &
\multicolumn{1}{>{\raggedleft}X|}{1 cell right} &
%               \multicolumn{1}{X}{} &
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\raggedleft}X|}{2 cells
right} \\ \hline
\multicolumn{1}{|>{\raggedleft}X|}{1 cell right}  &
\multicolumn{1}{>{\raggedleft}X|}{1 cell right} &
\multicolumn{1}{>{\raggedleft}X|}{1 cell right}   &
\multicolumn{1}{>{\raggedleft}X|}{1 cell right} \\ \hline
\end{tabularx}
\end{table}

\end{document}


• The 2 cells right column in your solution is actually a one cell column, I tried making it a 2 cell column with the \hzise solution but with \hsize=2\hsize the text is not aligned correct. Aug 23 '14 at 12:13
• @Hemeroc When you use \raggedleft you have to add 2\tabcolsep+\arrayrulewidth in 2 cells right column like >{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\raggedleft}X|. Remember that redefining tabularxcolumn as in the other answer makes all X columns in other tables \raggedleft. I have updated the answer.
– user11232
Aug 23 '14 at 13:16