# Using display style fraction in a matrix environment

I'd like use the \dfrac command in a bmatrix environment as follows:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$\begin{bmatrix} \dfrac{\partial f}{\partial x} \\ \dfrac{\partial f}{\partial y} \end{bmatrix}$

\end{document}


Unfortunately, one gets pretty ugly spaced elements that do not entirely fit between matrix brackets

Without specifying magic line skip values, what is the proper way to fix this problem?

-

You can stick some \struts in there (the height+depth of a paren):

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$\begin{bmatrix} \dfrac{\strut\partial f}{\strut\partial x} \\ \dfrac{\strut\partial f}{\strut\partial y} \end{bmatrix}$
\end{document}


-
Definitely most easy, without manual adjustment of spacing parameters - nice! Note that this solution also adds a bit vertical space around the fraction lines. (with f this is OK, but maybe not with h instead.) –  Hendrik Vogt Jun 26 '12 at 18:30
@HendrikVogt: Yes, I tried with \mathstrut for h, but it looks wrong. Also, for the reader, I think my claim of a \strut being the height and depth of a paren is wrong (I think in LaTeX it is .7\baselineskip for height and .3\baselineskip for depth, and in plain they're fixed). IIRC, for the \mathstrut the claim would be correct. –  morbusg Jun 28 '12 at 18:00

You can use the optional argument of \\ to add vertical space and some zero-width rules to increase the length of the delimiters:

\documentclass{article}
\usepackage{mathtools}

\begin{document}

$\begin{bmatrix} \dfrac{\partial f}{\partial x}\rule{0pt}{1.7em} \\[1em] \dfrac{\partial f}{\partial y}\rule[-1.2em]{0pt}{0em} \end{bmatrix}$

\end{document}


Building on percusses's idea, you could define a new environment increasing \arraystretch and adding some extra space for the last row. The first (optional) argument in my example controls the redefinition of \arraystretch; the second (mandatory) argument controls the spacing after the last row:

\documentclass[12pt]{article}
\usepackage{mathtools}

\newcommand\myfact{}
\newenvironment{Mybmatrix}[2][1.8]
{\$-\myfact em]\mbox{}\end{bmatrix}} \begin{document} \[ \begin{Mybmatrix}{2} \dfrac{\partial f}{\partial x} \\ \dfrac{\partial f}{\partial y} \end{Mybmatrix}$

\end{document}


I don't know, however, how "robust" this definition will be.

-
As already mentioned, I'd like to avoid this. Also, the problem with elements not fitting entirely into the matrix which can be seen in the question persists. –  caustic May 30 '11 at 16:55
@caustic: I've updated my answer with a possible solution for the elements not fitting. I don't know if there is a more automated solution. –  Gonzalo Medina May 30 '11 at 17:07
btw: this has nothing to do with mathtools, \dfrac stem from amsmath (if it isn't in the LaTeX core). mathtools automatically load amsmath –  daleif May 30 '11 at 17:15
@daleif: I've updated the question. –  caustic May 30 '11 at 17:26
@caustic: I've updated my answer with a new approach, using @percusses's idea. –  Gonzalo Medina May 30 '11 at 17:30

I use the following frequently which might be what you wish. But it still lacks the extra space at the bottom.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

${ \renewcommand{\arraystretch}{2} \begin{bmatrix} \dfrac{\partial f}{\partial x} \\ \dfrac{\partial f}{\partial y} \end{bmatrix} }$

\end{document}


-

This answer consists of three parts:

• a solution with manual adjustment of the vertical spacing,
• a solution with a modified \arraystretch that automatically centers the brackets around the matrix properly (unlike the original \arraystretch!),
• an explanation of the ugly default spacing.

You have two issues here:

1. The spacing is ugly – the matrix entries actually overlap slightly.

2. The matrix entries do not entirely fit between the brackets.

Issue #2 is in fact by design since you have a default \delimitershortfall of 5pt, which means that at most 5pt of the entries are not "covered" by the brackets. Thus, an easy solution (with manually adjusted vertical space, as in the first part of Gonzalo's answer) would be

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\setlength{\delimitershortfall}{0pt} \begin{bmatrix} \dfrac{\partial f}{\partial x} \\[2ex] \dfrac{\partial f}{\partial y} \end{bmatrix}$
\end{document}


(The redefinition of \delimitershortfall is local to the displayed formula.) It is interesting to note that adding much less vertical space, say 0.5ex, has no effect whatsoever; I will explain this near the bottom of the answer.

## Using \arraystretch

\end{document}


## Why is the default spacing so ugly?

The reason for the slight overlap is that \partial sticks out of its bounding box a little; in the image I show only the top and the bottom:

But why are the two fractions just put on top of each other by default? The reason lies in the implementation of bmatrix, which is based on the LaTeX array. (The plain TeX \matrix would put a vertical space of 1pt between the fractions!)

Here's how array works: Each row gets a minimum height and a minimum depth, defined by the height and depth of \strutbox. If the actual height and depth of your entries are larger, then the \strutbox has no effect. (This is the case for the displaystyle fractions.) Then the rows are just put on top of each other, without additional vertical space. This implementation results in good spacing only if the actual material in the rows has "usual" small height and depth.

Now I can explain why \\[0.5ex] has no effect in the first part of my answer: simply put, the 0.5ex are just added to the depth of the \strutbox (3.6pt), and together this is still less than the depth of \dfrac{\partial f}{\partial x} (6.86pt). Thus, you'll only get an effect if you add more that 3.26pt vertical space!

Moreover, I can explain why \arraystretch adds too much space at the top of the matrix: LaTeX increases both the height and the depth of the \strutbox by the factor \arraystretch. Now the point is that the height (8.4pt) is much larger than the depth (3.6pt), so with an \arraystretch of 2, 8.4pt are added at the top of the matrix, and only 3.6pt at the bottom. For a text array this should be OK, but in a matrix things tend to be centered vertically with respect to the mathematical axis (like fractions and integrals), so when they get large, they have equal increase in height and depth.

What I don't know is why the above implementation of array was chosen in LaTeX. I guess that for text arrays it makes a lot of sense, but it appears that it's not optimal for matrices.

-