7

One problem I've always had is trying to make text and/or math text vertically centered in either the tabular or array environments.

Take this for example:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\firstpartial}[1]{\dfrac{\partial}{\partial {#1}}}
\begin{document}
\[
\begin{array}{|l|l|}
\hline
\firstpartial{x}\left[f(x,y)\right] = 2xy & \text{Differentiate with respect to }x\text{, treating }y \text{ as a constant.}\\[4ex]
\hline 
\end{array}
\]
\end{document}

enter image description here

I would like to vertically center both the math and the text in both columns. How can I do this?

Clarinetist
  • 1,550

3 Answers3

8

Take a look at the cellspace package which states the following in the abstract:

enter image description here

Here is a MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage[math]{cellspace}
\cellspacetoplimit5pt
\cellspacebottomlimit5pt
\newcommand{\firstpartial}[1]{\dfrac{\partial}{\partial {#1}}}
\begin{document}
\begin{tabular}{|Sl|l|} % Note the use of the prefix S to the column which needs be modified.
\hline
$\firstpartial{x}\left[f(x,y)\right] = 2xy$ & Differentiate with respect to $x$, treating $y$ as a constant.\\
\hline 
\end{tabular}

\end{document}

enter image description here

Another possible solution is to use a vertical rule as shown in the MWE below:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\addmathvspace}{\vrule width 0pt height 4ex depth 2.5ex} % Adjust height and depth here.
\newcommand{\firstpartial}[1]{\dfrac{\partial}{\partial {#1}}}
\begin{document}
\begin{tabular}{|l|l|}
\hline
\addmathvspace$\firstpartial{x}\left[f(x,y)\right] = 2xy$ & Differentiate with respect to $x$, treating $y$ as a constant.\\
\hline 
\end{tabular}

\end{document}
azetina
  • 28,884
4

You could define a "math strut" -- an invisible object with height and depth but no width (and thus invisible) -- and insert it where needed. I suggest using a "large integral with upper and lower limits" as the template for such a strut.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\newcommand{\firstpartial}[1]{\dfrac{\partial}{\partial {#1}}}
%% define a "big" strut using \vphantom:
\newcommand\bstrut{\vphantom{\displaystyle\int\limits_0^1}}
\begin{document}
\[
\begin{array}{|l|l|}
\hline
\bstrut\firstpartial{x}\left[f(x,y)\right] = 2xy & 
   \text{Differentiate with respect to $x$, treating $y$ as a constant.}\\
\hline 
\end{array}
\]
\end{document}
Mico
  • 506,678
2

At least in this case, \addstackgap from the stackengine package can add a symmetric gap above and below an object. If the gap to be added is asymmetric (more above than below, for example), the \addvbuffer macro of the verbatimbox package allows for that.

Here is the \addstackgap fix:

\documentclass{article}
\usepackage{amsmath}
\usepackage{stackengine}
\stackMath
\newcommand{\firstpartial}[1]{\dfrac{\partial}{\partial {#1}}}
\begin{document}
\[
\begin{array}{|l|l|}
\hline
\addstackgap[3pt]{\firstpartial{x}\left[f(x,y)\right] = 2xy} & \text{Differentiate with respect to }x\text{, treating }y \text{ as a constant.}\\
\hline 
\end{array}
\]
\end{document}

enter image description here