1
\documentclass{article}

%\usepackage[margin=1in]{geometry}
\usepackage{multirow,tabularx}
\newcolumntype{Y}{>{\centering}X}

\begin{document}

\begin{table}
    \begin{tabularx}{\textwidth}{
        *{4}{|m{\dimexpr.25\linewidth-2\tabcolsep}} |   
    }
\hline
1 & 2 & 3 & 4 \\ \hline
\multicolumn{3}{|Y|}{1-3 centered} & 4 \\ \hline
\multicolumn{2}{|Y|}{1-2 centered} & \multicolumn{2}{Y|}{3-4 centered} \\ \hline
    \end{tabularx}
\end{table}
\end{document}

result

Why is the first cell from the second row (multicol 3) NOT centering across the 3 columns, but instead only over 2 colums as the next row does?

  • on first sight it seems that behavior of your table is strange ... but in your code you never define width of X column, so it with is select randomly. for better/more correct explanation we should ask author of tabularx package. – Zarko Oct 7 '17 at 19:27
  • try \begin{tabularx}{\textwidth}{|X|X|X|X|} \hline 1 & 2 & 3 & 4 \\ \hline \multicolumn{3}{|>{\hsize=3\hsize}Y|}{\rule{\hsize}{4pt}} & 4 \\ \hline \multicolumn{2}{|>{\hsize=2\hsize}Y|}{\rule{\hsize}{4pt}} & \multicolumn{2}{>{\hsize=2\hsize}Y|}{\rule{\hsize}{4pt}} \\ \hline \end{tabularx}. result is what you expected to obtain (discrepancy between rule length and cells width is consequence not considering \tabcolsep and \arrayrulewidth` in determining multi column cells). – Zarko Oct 7 '17 at 19:35
  • Seems to work, although it feels a bit hacky. Thank you anways. – nardian Oct 7 '17 at 19:42
1

There no need to use tabularx only to define a strange \newcolumntype{Y}{>{\centering}X} only used in multicolumns, when you have already defined your columns in order to occupy all the line width: *{4}{|m{\dimexpr.25\linewidth-2\tabcolsep}} |.

A simple tabular with c multicolumns does the trick.

I have also added a -1.25\arrayrulewidth to avoid overfull hbox. The value 1.25\arrayrulewidth is due to 1\arrayrulewidth plus .25\arrayrulewidth, the latter is 1\arrayrulewidth (the width of the 5th rule) divided by 4 (the number of columns). Thanks to Zarko for having pointed out the need of the .25.

\documentclass{article}
\usepackage{array}

\begin{document}

    \begin{table}
        \begin{tabular}{
                *{4}{|m{\dimexpr.25\linewidth-2\tabcolsep-1.25\arrayrulewidth}} |   
                }
            \hline
            1 & 2 & 3 & 4 \\ 
            \hline
            \multicolumn{3}{|c|}{1-3 centered} & 4 \\ \hline
            \multicolumn{2}{|c|}{1-2 centered} & \multicolumn{2}{c|}{3-4 centered} \\ 
            \hline
        \end{tabular}
    \end{table}
\end{document}

enter image description here

  • accurate width of cells is m{\dimexpr.25\linewidth-2\tabcolsep-1.25\arrayrulewidth} ;-). – Zarko Oct 7 '17 at 20:04
  • @Zarko I'm bowing in front of your precision and editing my answer :) – CarLaTeX Oct 7 '17 at 20:14
  • i have great teacher here on site. one of them are you :-) – Zarko Oct 7 '17 at 20:25
  • @Zarko Oh no! I'm only (almost) a beginner! – CarLaTeX Oct 7 '17 at 20:26
  • really? then you are extraordinary one ;-) – Zarko Oct 7 '17 at 20:37
0

less simple as answer of CarLaTeX, however it is based on use of tabularx (improved version of my comment to your question):

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcc[2]{% shortcut for cells spanned two columns
    \multicolumn{2}{#1>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth}C|}{#2}}
\newcommand\mccc[2]{% shortcut for cells spanned three columns
    \multicolumn{3}{#1>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}C|}{#2}}

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
%\setlength\arrayrulewidth{10pt}

\begin{document}
    \begin{table}
\begin{tabularx}{\textwidth}{|C|C|C|C|}
    \hline
1 & 2 & 3 & 4 \\
    \hline
\mccc{|}{ \rule{\hsize}{4pt} }                  & 4             \\
    \hline
\mccc{|}{ 1-3 centered }                        & 4             \\
    \hline
\mcc{|}{ \rule{\hsize}{4pt} }   & \mcc{}{ \rule{\hsize}{4pt} }  \\
    \hline
\mcc{|}{ 1-2 centered }         & \mcc{}{ 3-4 centered }        \\
    \hline
\end{tabularx}
    \end{table}
\end{document}

enter image description here

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.