1

I'm trying to typeset a table with a fixed width were the cells contents are centered both vertically and horizontally, even if one cell contains a forced line-break.

I've managed to get that done, but now I want to add text with two lines that spans two columns using the \multicolumn command and have it still centered horizontally.

This is where I'm struggeling:

\documentclass{article}

\usepackage{array, tabularx}
\usepackage[textwidth=6in]{geometry}

\renewcommand{\tabcolsep}{0pt}
\setlength{\extrarowheight}{10pt}

\begin{document}
    %
    \centering%
        %
        \begin{tabularx}{\textwidth}{%
                >{\centering\arraybackslash}m{0.5\textwidth}|
                >{\centering\arraybackslash}m{0.25\textwidth}
                >{\centering\arraybackslash}m{0.25\textwidth}
            }%
            test & test & test\break test\\
            test & \multicolumn{2}{X}{test\break test}
        \end{tabularx}
        %
    %
\end{document}

As you can see, the first column of the second row is not centered vertically and the second column is not centered horizontally, which is because I used the X parameter for \multicolumn, but I don't know what else I should use instead.

How can this problem be solved?

1
  • Probably you are looking for a combination of \renewcommand\tabularxcolumn[1]{m{#1}} and \multicolumn{2}{>{\centering\arraybackslash}X}{test\break test}? – leandriis Jan 18 '20 at 19:19
1

To ensure a consistent cell padding, I deleted the \extrarowheight specification and added the cellspace package, in addition to a redefinition of the X column type and the use of >{\hsize=xx\hsize} to obtain X column widths in the ratio 2:1.

Unrelated: needless to load array if you load tabularx.

\documentclass{article}
\usepackage[textwidth=6in]{geometry}
\usepackage{cellspace, tabularx}
\setlength{\cellspacetoplimit}{8pt}
\setlength{\cellspacebottomlimit}{8pt}
\addparagraphcolumntypes{X}

\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}
\renewcommand{\tabcolsep}{0pt}

\begin{document}

    \centering%

        \begin{tabularx}{\textwidth}{%
               | >{\hsize=1.5\hsize}S{X}|
                >{\hsize=0.75\hsize}S{X}
               | >{\hsize=0.75\hsize}S{X}|
            }%
        \hline
            test & test & test\break test \\
        \hline
            test & \multicolumn{2}{>{\hsize=1.5\hsize}S{X}|}{test\break test} \\
        \hline
        \end{tabularx}

\end{document}

enter image description here

2
  • I thank you for your answer. I'm probably going to stick with this approach. But I have one question: Where does the "S" in ">{\hsize=1.5\hsize}S" come frome? What kind of command is this? – Sir Teddy the First Jan 18 '20 at 22:00
  • Sorry, I forgot to explain it: it is a prefix from cell space. It has nothing to do with >{\hsize=1.5\hsize} and the like, which are here to ensure the relative widths of the columns. The point is that we write, as a column type S{X} to indicate the cells in corresponding column require vertical padding.You can look at the details in the package documentation. One caveat: if yo also load siunits, which defines an S column type, you have to use another prefix. The default in this case is C. – Bernard Jan 18 '20 at 22:08
1

Try the following:

\documentclass{article}
\usepackage[textwidth=6in]{geometry}
\usepackage{array, tabularx}
\renewcommand{\tabcolsep}{0pt}
%%--------------------------------------------------------------%
%%  patch of Ulrike Fisher (on my request)                      %
%%   (http://tex.stackexchange.com/questions/319768/            %
%%--------------------------------------------------------------%
\makeatletter
\renewcommand\mcell@classz{\@classx
\@tempcnta \count@
\prepnext@tok
\@addtopreamble{%\mcell@mstyle
\ifcase\@chnum
\hfil
\mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
\hskip1sp
\mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
\hfil\hskip1sp
\mcell@agape{\d@llarbegin \insert@column\d@llarend}\or
\mcell@agape{$\vcenter
\@startpbox{\@nextchar}\insert@column\@endpbox$}\or
\mcell@agape{\vtop
\@startpbox{\@nextchar}\insert@column\@endpbox}\or
\mcell@agape{\vbox
\@startpbox{\@nextchar}\insert@column\@endpbox}%
\fi
\global\let\mcell@left\relax\global\let\mcell@right\relax
}\prepnext@tok}
\makeatother

\begin{document}
    \begin{table}
    \renewcommand\tabularxcolumn[1]{m{#1}}
    \setcellgapes{10pt}
    \makegapedcells
    \centering
\begin{tabularx}{\textwidth}{|%
                >{\centering\arraybackslash}X|
                >{\centering\arraybackslash\hsize=0.5\hsize}X|
                >{\centering\arraybackslash\hsize=0.5\hsize}X|
            }%
    \hline
test & test & test\break test\\
    \hline
test & \multicolumn{2}{>{\centering\arraybackslash}X|}{test\break test}\\
    \hline
\end{tabularx}
    \end{table}
\end{document}

enter image description here

I add vertical and horizontal rules that you can easy see, that cells' contents are centered as you like to have.

Edit: Now is added more vertical space with use of makecell package AND PATCH of it incompatibility with m column types.

2
  • I also thank you for your answer, although I'm probably going to stick with the other one. – Sir Teddy the First Jan 18 '20 at 22:01
  • @SirTeddytheFirst, ah, you like to have more vertical space. I will done this asap. – Zarko Jan 18 '20 at 22:08

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.