# Avoiding overlapping superscripts and subscripts in matrices

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{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{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?

• \renewcommand\arraystretch{2} just before \begin{pmatrix}. – Gonzalo Medina Feb 24 '14 at 23:15
• While that certainly helps, is there a way to do it only for some of the lines of the matrix? – Abe Schulte Feb 24 '14 at 23:35
• Use the optional argument of  \\  as in \$20pt]. – Gonzalo Medina Feb 24 '14 at 23:36 • Yes, you can pad rows using struts or by increasing the row's vertical spacing with respect to the others using \\[<len>]. This is described in Column and row padding in tables. – Werner Feb 24 '14 at 23:36 • @Werner I'm not sure that question applies for this case. That one deals with very different cases and it may be difficult to find the good way out for matrices. – egreg Feb 25 '14 at 0:18 ## 3 Answers 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}


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

• What does the optional math argument change for the cellspace package? – Abe Schulte Feb 25 '14 at 0:32
• Without it, it works only for tabular and the like (text mode, I guess). – Bernard Feb 25 '14 at 0: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 0: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 0: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 0:48

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

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$$\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{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{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{document}


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

\documentclass{minimal}
\usepackage{tabstackengine}
\begin{document}
$$\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} }$$
$$\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{document}