5

I basically have a DI table that I want to construct and I have a slight vertical centering issue in the cells. This is the code:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{relsize}
\usepackage{array}
\usepackage{pgfplots}

\begin{document}

\begin{center}
\renewcommand{\arraystretch}{2.5}
\begin{tabular}{|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{3cm}|}
\hline
$\displaystyle + {x}^{4}$ & $\displaystyle + \sin(3x)$\\
\hline
$\displaystyle - 4{x}^{3}$ & $\displaystyle - \frac{1}{3} \cos(3x)$\\
\hline
$\displaystyle + 12{x}^{2}$ & $\displaystyle - \frac{1}{9} \sin(3x)$\\
\hline
$\displaystyle - 24x$ & $\displaystyle + \frac{1}{27} \cos(3x)$\\
\hline
$\displaystyle + 24$ & $\displaystyle + \frac{1}{81} \sin(3x)$\\
\hline
$\displaystyle - 0$ & $\displaystyle - \frac{1}{243} \cos(3x)$\\
\hline
\end{tabular}
\end{center}

\end{document}

The problem here is that the math in the boxes are too close to the bottom lines and to far from the top lines of the table (aka not centered in the vertical axis of the cells). I found that I can center the math using double $ symbols instead of just a single $ with \displaystyle, but, that makes the cells too large. Any way I can fix this centering issue?

4 Answers 4

5

With {NiceTabular} of nicematrix and its key cell-space-limits.

\documentclass{article}
\usepackage{mathtools}
\usepackage{nicematrix}

\begin{document}

\begin{center}
\begin{NiceTabular}{w{c}{1.5cm}w{c}{3cm}}[cell-space-limits=6pt,hvlines]
$\displaystyle + x^{4}$ & $\displaystyle + \sin(3x)$\\
$\displaystyle - 4x^{3}$ & $\displaystyle - \frac{1}{3} \cos(3x)$\\
$\displaystyle + 12x^{2}$ & $\displaystyle - \frac{1}{9} \sin(3x)$\\
$\displaystyle - 24x$ & $\displaystyle + \frac{1}{27} \cos(3x)$\\
$\displaystyle + 24$ & $\displaystyle + \frac{1}{81} \sin(3x)$\\
$\displaystyle - 0$ & $\displaystyle - \frac{1}{243} \cos(3x)$\\
\end{NiceTabular}
\end{center}

\end{document}

Output of the above code

3

I'd not try to set \arraystretch, nor to use p columns.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

\begin{document}

\[
\begin{array}{
 |
 >{\displaystyle\vphantom{\Bigg|}} w{c}{1.5cm}
 |
 >{\displaystyle} w{c}{3cm}
 |
}
\hline
+ {x}^{4}  & \sin(3x) \\
\hline
- 4{x}^{3} & - \frac{1}{3} \cos(3x) \\
\hline
12{x}^{2}  & - \frac{1}{9} \sin(3x) \\
\hline
- 24x      & \frac{1}{27} \cos(3x) \\
\hline
24         & \frac{1}{81} \sin(3x) \\
\hline
- 0        & - \frac{1}{243} \cos(3x) \\
\hline
\end{array}
\]

\end{document}

enter image description here

Avoid the pdftex option to graphicx; the package is usually able to guess the backend it needs and it definitely is with pdflatex.

3

With tblr oftabularray package:

\documentclass{article}
\usepackage{tabularray}

\begin{document}

\[
\begin{tblr}{hlines, vlines,
             colspec = {*{2}{Q[c,mode=dmath]}},
             rows    = {ht=7ex}
             }
 + {x}^{4}  & \sin(3x)                  \\
- 4{x}^{3}  & - \frac{1}{3} \cos(3x)    \\
12{x}^{2}   & - \frac{1}{9} \sin(3x)    \\
- 24x       & \frac{1}{27}  \cos(3x)    \\
24          & \frac{1}{81}  \sin(3x)    \\
- 0         & - \frac{1}{243} \cos(3x)  \\
\end{tblr}
\]

\end{document}

enter image description here

3

I have a slight vertical centering issue

This issue that arises in your table is actually a self-inflicted wound[!]. It is caused by the fact that you use \hline directives, which provide absolutely no vertical whitespace padding, to draw horizontal rules.

I'd like to suggest that you (a) get rid of all vertical rules and (b) use fewer, but well-spaced horizontal rules, using the user macros of the booktabs package, to create the two-column table. That way, without all those distracting and (typographically) unnecessary straight-line guideposts, your vertical-centering issue is taken care of automatically.

Given that the table's contents are entirely math-y, I'd also use an array environment instead of a tabular environment. Moreover, since your document employs the array package, I'd use its w column type instead of the m column type. Finally, a value of 2.5 for \arraystretch seems wildly excessive, even profligate. I suggest to reducing \arraystretch to 2 (or even less).

enter image description here

\documentclass{article}
\usepackage{array}     % for 'w' column type
\usepackage{amsmath}   % for '\dfrac' macro
\usepackage{booktabs}  % for well-spaced horizontal rules

\begin{document}

\[
\renewcommand{\arraystretch}{2} % '2.5' seems wildly excessive
\begin{array}{@{} wc{1.5cm} wc{3cm} @{}}
\toprule
+ x^{4}     & + \sin(3x)\\
\midrule
- 4x^{3}    & - \dfrac{1}{3} \cos(3x)\\
\addlinespace
+ 12x^{2}   & - \dfrac{1}{9} \sin(3x)\\
\addlinespace
- 24x       & + \dfrac{1}{27} \cos(3x)\\
\addlinespace
+ 24        & + \dfrac{1}{81} \sin(3x)\\
\addlinespace
- 0         & - \dfrac{1}{243}\cos(3x)\\
\addlinespace
\bottomrule
\end{array}
\]

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .