# multicolumn row in table with extra alignment

I have a table with three rows and four columns. Each entry of the first row is centered text and there is no \hrule underneath. The next row is more centered text. Because there is no \hrule between these first two rows it looks like centered multiline text which is what I want. So the first two rows look effectively like one row.

The last row is multicolumn spanning all four columns. This row has seven math objects. I am trying to get these to align with the centered text above and the vertical lines separating the columns above (four text components and three vertical column separators). How can I achieve this alignment?

Here is my code:

\documentclass{book}

\begin{document}

\begin{tabular}{|cccc|}
\hline
\multicolumn{1}{|c|}{Text text text} &
\multicolumn{1}{c|}{Text text} &
\multicolumn{1}{c|}{Text text text text} &
\multicolumn{1}{c|}{Text Text}  \\
\multicolumn{1}{|c|}{Text} &
\multicolumn{1}{c|}{Texting} &
\multicolumn{1}{c|}{Text Text Texting} &
\multicolumn{1}{c|}{Text}  \\
\hline
\multicolumn{4}{|c|}{784 \qquad $x^2$ \qquad $\pi^2$ \qquad $>$ \qquad 1,543 \qquad $1+0.6$ \qquad
301}\\
\hline
\end{tabular}

\end{document}


In the last row the $x^2$, $>$, and $1+0.6$ should be directly below the vertical column lines.

• please always post a complete document that shows the problem, why is the last row a multicolumn rather than using the column alignment? May 16, 2020 at 22:14
• It is not apparent in my example but the last row shows relations between four entries and the table is to emphasize these relations and to describe the entries themselves. If it is easier to understand you might think of the last row as an ordering like 1<2<3<4 and the first row as the numbers one, two, three, four. The inequality symbols should be under the vertical column lines. Of course the real first row is centered multiline but that may help. May 16, 2020 at 22:30

If the initial lines are paragraphs of text, more natural to set them in p columns.

\documentclass{book}
\usepackage{array}
\begin{document}

\newcommand\cm[1]{\multicolumn{1}{c}{\makebox[0pt]{$#1$}}}

\centering
\setlength\extrarowheight{3pt}
\setlength\tabskip{3pt}
\begin{tabular}{@{}|
>{\centering}p{2.2cm}>{\vline}c
>{\centering}p{2.2cm}>{\vline}c
>{\centering}p{2.2cm}>{\vline}c
>{\centering\arraybackslash}p{2.2cm}|@{}}
\hline
Text text textText
&&
Text text Texting
&&
Text text text text Text Text Texting
&&
Text Text Text
\\
\hline
784 & \cm{x^2} & $\pi^2$ & \cm{>} & 1,543 & \cm{1+0.6} & 301\\
\hline
\end{tabular}

\end{document}


• Your image looks perfect. In my version I am getting a huge amount of spacing on the left and right of the entries causing the table to extend off the page. My margins are 1in set by the geometry package. I have another table directly above this one that does not have this problem. Also, \centering affects my section headings too. May 17, 2020 at 0:43
• By "in my version" I meant when I try your code. May 17, 2020 at 0:55
• Adding \setlength{\tabcolsep}{1.36em} worked for me. Thank you! May 17, 2020 at 1:39

Something like this?

\documentclass{article}
\usepackage{array}
\usepackage{mathtools}
\usepackage{makecell}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}

\begin{document}

\begin{tabular}{|*{4}{Sc|}}
\hline
\makecell{Text text text\\ Text} &
\makecell{Text text \\ Texting} &
\makecell{Text text text text \\ Text Text Texting} &
\makecell{Text Text \\ Text} \\
\hline
\multicolumn{1}{|Sc@{$\mathclap{x^2}$}}{784\enspace \mbox{}} & \multicolumn{1}{Sc@{$\mathclap{>}$}}{$\pi^2$} & \multicolumn{1}{c@{$\mathclap{1+0.6 }$}} {1,543\hskip 1.5em\mbox{}} &
\multicolumn{1}{c|}{\hskip1.5em 301} \\
\hline
\end{tabular}

\end{document}


• Yes. Can the 784, 1,542, and 301 be centered better? Also, is it possible to decrease the spacing in the first two rows? They are supposed to be sentences broken over a line. What is Sc? May 16, 2020 at 22:50
• Could we fix the column widths to get perfect alignment? May 16, 2020 at 23:01
• Decreasing the vertical spacing between the first two rows is easy with makecell (so there is a single row). You also could use , say tabularx and X columns, but this wouldn't produce a perfect centring, because of the \clap command, which is a sort of horizontal \smash, so if you need corrections, this has to be done by trial and error. Please see the updated code. May 16, 2020 at 23:19
• Thank you. This is pretty good. May 17, 2020 at 0:15

With {NiceTabular} of nicematrix, you can put elements where you want in your tabular after the construction of the array by using the PGF/Tikz nodes created by {NiceTabular} under the cells, rows and columns of the array.

\documentclass{book}
\usepackage{nicematrix,tikz}
\begin{document}

\centering
\setlength\extrarowheight{3pt}
\setlength\tabskip{3pt}
\begin{NiceTabular}{*{4}{>{\centering\arraybackslash}p{2.2cm}}}[hvlines]
Text text textText
&
Text text Texting
&
Text text text text Text Text Texting
&
Text Text Text \\
\Block{1-*}{} % in order to have no vertical rule inside that row
784 & $\pi^2$ & 1,543 & 301\\
\CodeAfter
\begin{tikzpicture}
\node [anchor=base] at (row-2-base-|2) { $x^2$ } ;
\node [anchor=base] at (row-2-base-|3) { $>$ } ;
\node [anchor=base] at (row-2-base-|4) { $1+0.6$ } ;
\end{tikzpicture}
\end{NiceTabular}

\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes).