14

I was working on some calculus today and tried to typeset a matrix of partial derivatives and ran into some trouble. Here is my code:

\documentclass{minimal}
\usepackage{mathtools}
\begin{document}
\begin{equation}
\begin{pmatrix}
    \frac{\partial f^1}{\partial x_1}   & \frac{\partial f^1}{\partial x_2}\\
    \frac{\partial f^2}{\partial x_1}   & \frac{\partial f^2}{\partial x_2}
\end{pmatrix}
\end{equation}
\end{document}

As can be seen, the output results in the subscripts from the denominators of the top row on the matrix with the superscripts from the numerators of the bottom row on the matrix. Is there an easy way to avoid this?

3 Answers3

11

One way is to use the optional argument of \\ like \\[<length>]:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{equation}
\begin{pmatrix}
    \frac{\partial f^1}{\partial x_1}   & \frac{\partial f^1}{\partial x_2}\\[1ex]
    \frac{\partial f^2}{\partial x_1}   & \frac{\partial f^2}{\partial x_2}
\end{pmatrix}
\end{equation}
\end{document}

Another way is to add one invisible line with 0 width, -2ex depth (say) and 4ex total height in the first row.:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{equation}
\begin{pmatrix}
    \rule[-2ex]{0pt}{4ex}\frac{\partial f^1}{\partial x_1}   & \frac{\partial f^1}{\partial x_2}\\
    \frac{\partial f^2}{\partial x_1}   & \frac{\partial f^2}{\partial x_2}
\end{pmatrix}
\end{equation}
\end{document}

enter image description here

Andrew Swann
  • 95,762
9

The cellspace package defines 2 lengths, \cellspacetoplimit and \cellspacebottomlimit that are the minimal vertical white space between the top of a cell and the bottom of the above cell, and between the bottom of a cell and the top of the below cell. Here is a example:

    \documentclass{minimal}
    \usepackage{mathtools}

    \usepackage[math]{cellspace}
    \cellspacetoplimit 3pt
    \cellspacebottomlimit 3pt
    \setlength{\arraycolsep}{4pt}
    \begin{document}
    \[
    \begin{pmatrix}
        \dfrac{\partial f^1}{\partial x_1}   & \dfrac{\partial f^1}{\partial x_2}\\
        \dfrac{\partial f^2}{\partial x_1}   & \dfrac{\partial f^2}{\partial x_2}
    \end{pmatrix}
    \]

    \cellspacetoplimit 0pt
    \cellspacebottomlimit 0pt
    \[
    \begin{pmatrix}
        \dfrac{\partial f^1}{\partial x_1}   & \dfrac{\partial f^1}{\partial x_2}\\
        \dfrac{\partial f^2}{\partial x_1}   & \dfrac{\partial f^2}{\partial x_2}
    \end{pmatrix}
    \]
    \end{document} 

enter image description here

It works with the amsmath environments (matrix, &c.), but not, unfortunately, with their starred versions defined by mathtools.

Bernard
  • 271,350
  • What does the optional math argument change for the cellspace package? – Abe Schulte Feb 25 '14 at 00:32
  • Without it, it works only for tabular and the like (text mode, I guess). – Bernard Feb 25 '14 at 00:35
  • Ok. Additionally, is there a way to set the length using more flexible length parameters such as 1em or 2ex? I'd like it to be variable based on the size of my text if possible. – Abe Schulte Feb 25 '14 at 00:37
  • I've just tested: the answer is no. But I can ask the author if it would be possible. – Bernard Feb 25 '14 at 00:44
  • I forgot to mention that if one has to ask specifically the math option, the reason is that it tackles an internal command of the matrix environments (the \env@matrix command). So amsmath, empheqand mathtoolsh ave to be loaded before cellspace. – Bernard Feb 25 '14 at 00:48
6

With a TABstack, you have complete control over the horizontal and vertical spacing. Here are two examples.

\documentclass{minimal}
\usepackage{tabstackengine}
\begin{document}
\begin{equation}
\setstackgap{L}{1.6\baselineskip}
\setstacktabbedgap{1ex}
\parenMatrixstack{
    \frac{\partial f^1}{\partial x_1}   & \frac{\partial f^1}{\partial x_2}\\
    \frac{\partial f^2}{\partial x_1}   & \frac{\partial f^2}{\partial x_2}
}
\end{equation}
\begin{equation}
\setstackgap{L}{1.8\baselineskip}
\setstacktabbedgap{1.4ex}
\parenMatrixstack{
    \frac{\partial f^1}{\partial x_1}   & \frac{\partial f^1}{\partial x_2}\\
    \frac{\partial f^2}{\partial x_1}   & \frac{\partial f^2}{\partial x_2}
}
\end{equation}
\end{document}

enter image description here