我一直遇到的一个问题是尝试使文本和/或数学文本在tabular
或array
环境中垂直居中。
举个例子:
\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}
我想垂直将数学和文本置于两列的中心。我该怎么做?
答案1
看一下该cellspace
包,其中摘要说明了以下内容:
以下是 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}
另一个可能的解决方案是使用垂直规则,如下面的 MWE 所示:
\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}
答案2
您可以定义一个“数学支柱”——一个有高度和深度但没有宽度(因此不可见)的不可见对象——并将其插入到需要的地方。我建议使用“有上限和下限的大积分”作为此类支柱的模板。
\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}
答案3
至少在这种情况下,可以\addstackgap
从stackengine
包中添加对称间隙,该间隙位于对象的上方和下方。如果要添加的间隙不对称(例如,上方间隙大于下方间隙),包\addvbuffer
的宏verbatimbox
允许这样做。
解决方法如下\addstackgap
:
\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}