3

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[a4paper, top=25mm, left=25mm, right=25mm, bottom=20mm, headsep=5mm, footskip=10mm, headheight=25mm]{geometry}

\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.

3

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[top=25mm, hmargin=25mm, bottom=20mm, headsep=5mm, footskip=10mm, headheight=25mm]{geometry}%

\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} 

enter image description here

  • 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 – Bernard Aug 23 '14 at 14:28
3

Here is a try using \hsize

\documentclass[]{article}

\usepackage[a4paper, top=25mm, left=25mm, right=25mm, bottom=20mm, headsep=5mm, footskip=10mm, headheight=25mm]{geometry}

\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}

enter image description here

  • 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. – Hemeroc Aug 23 '14 at 12:13
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.