1

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.

2
  • please always post a complete document that shows the problem, why is the last row a multicolumn rather than using the column alignment? – David Carlisle May 16 '20 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. – E2R0NS May 16 '20 at 22:30
1

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

enter image description here

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

3
  • 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. – E2R0NS May 17 '20 at 0:43
  • By "in my version" I meant when I try your code. – E2R0NS May 17 '20 at 0:55
  • Adding \setlength{\tabcolsep}{1.36em} worked for me. Thank you! – E2R0NS May 17 '20 at 1:39
1

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} 

enter image description here

4
  • 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? – E2R0NS May 16 '20 at 22:50
  • Could we fix the column widths to get perfect alignment? – E2R0NS May 16 '20 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. – Bernard May 16 '20 at 23:19
  • Thank you. This is pretty good. – E2R0NS May 17 '20 at 0:15
1

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

Output of the above code

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.