# vertically aligning vectors and matrices

What are your techniques to make what's below look better? Thanks.

\documentclass{article}
\usepackage{amsmath}
\newcommand{\ud}{\mathop{\mathrm{{}d}}\mathopen{}}

\begin{document}
$$\everymath{\displaystyle}\begin{pmatrix} \ud X_1 \\ \ud X_2 \\ \ud X_3 \end{pmatrix}=\begin{pmatrix} \ud x_1 \\ \ud x_2 \\\ud x_3 \end{pmatrix}+\begin{bmatrix} \frac{\partial u_1}{\partial x_1} & \frac{\partial u_1}{\partial x_2} & \frac{\partial u_1}{\partial x_3} \\ \frac{\partial u_2}{\partial x_1} & \frac{\partial u_2}{\partial x_2} & \frac{\partial u_2}{\partial x_3} \\ \frac{\partial u_3}{\partial x_1} & \frac{\partial u_3}{\partial x_2} & \frac{\partial u_3}{\partial x_3} \end{bmatrix}\begin{pmatrix} \ud x_1 \\ \ud x_2 \\ \ud x_3 \end{pmatrix}$$
\end{document}


This is more a mathematical than a TeXnical answer: I would typeset the matrix with \partial_1 u_1 instead of \frac{\partial u_1}{\partial x_1} and so on. Then it looks better, and I also find that it is better readable. You may want to explain that the notation \partial_j is a shorthand for \frac{\partial}{\partial x_j}.

Here's a way improving the spacing of you notation. It's a redefinition of an internal amsmath macro. It allows to add an optional space to each matrix row and it's extending all amsmath matrix environments like bmatrix, pmatrix, vmatrix, Bmatrix etc. (except smallmatrix).

\makeatletter
\let\origenv@matrix\env@matrix
\renewcommand*{\env@matrix}[1][0pt]{%
\expandafter\def\expandafter\@arraycr\expandafter{\@arraycr[#1]}
\origenv@matrix}
\makeatother


Now, with the common LaTeX matrix commands, omit the optional argument to get their standard behavior, or specify the additional space. In your example, writing \begin{bmatrix}[2.5ex] instead results in:

You could do that very similar using an optional argument that's temporarily set for \arraystretch. But the first method keeps the delimiters right, the \arraystretch way may leave undesired space at the top of the matrix because it's vertically stretching all rows including the first one.

I used \expandafter avoiding a recursion to add the space in brackets to the \@arraycr macro using its own definition. \g@addto@macro would do it easier but works globally. That's why some classes or packages define a local \l@addto@macro.

One other option is to use \phantom and \lefteqn as in this answer. For the pmatrix blocks, change it to, for example

\begin{pmatrix}
\lefteqn{\phantom{\frac{\partial u_1}{\partial x_3}}} dx_1 \\
\lefteqn{\phantom{\frac{\partial u_2}{\partial x_3}}} dx_2 \\
\lefteqn{\phantom{\frac{\partial u_3}{\partial x_3}}} dx_3
\end{pmatrix}


Inside the \phantom should go the "tallest" term in the row you want to line up.

• I would use \vphantom{...} in preference to \lefteqn{\phantom{...}}. I would also define a strut: \newcommand*{\mystrut}{\vphantom{\frac{\partial u_1}{\partial x_3}}} to make the latex more legible. Oct 2, 2010 at 21:28
• Thank you Lev, now my toolbox gets one more tool! Though I am undecided about the strut: I can't decide whether it is better to stretch all rows by the same, possibly much too large amount, or let the height of each row be decided by its tallest element. (Say if the example has \int_0^{2^{3^{4^5}}} f(x) dx in the third column of the third row of the bmatrix, which would you prefer?) Oct 3, 2010 at 1:55

Here are two approaches using stacks and TABstacks. Interrow baselineskip is specified with \setstackgap{L}{<length>} and intercolumn matrix gap is specified with \setstacktabbedgap{<length>}.

\documentclass{article}
\usepackage{amsmath,tabstackengine}
\newcommand{\ud}{\mathop{\mathrm{{}d}}\mathopen{}}
\setstacktabbedgap{1.5ex}
\begin{document}
$$\everymath{\displaystyle} \setstackgap{L}{14pt} \parenVectorstack{\ud X_1 \\ \ud X_2 \\ \ud X_3} = \parenVectorstack{\ud x_1 \\ \ud x_2 \\ \ud x_3} + {\setstackgap{L}{30pt} \bracketMatrixstack{ \frac{\partial u_1}{\partial x_1} & \frac{\partial u_1}{\partial x_2} & \frac{\partial u_1}{\partial x_3} \\ \frac{\partial u_2}{\partial x_1} & \frac{\partial u_2}{\partial x_2} & \frac{\partial u_2}{\partial x_3} \\ \frac{\partial u_3}{\partial x_1} & \frac{\partial u_3}{\partial x_2} & \frac{\partial u_3}{\partial x_3} }} \parenVectorstack{\ud x_1 \\ \ud x_2 \\ \ud x_3}$$

$$\everymath{\displaystyle} \setstackgap{L}{30pt} \parenVectorstack{\ud X_1 \\ \ud X_2 \\ \ud X_3} = \parenVectorstack{\ud x_1 \\ \ud x_2 \\ \ud x_3} + \bracketMatrixstack{ \frac{\partial u_1}{\partial x_1} & \frac{\partial u_1}{\partial x_2} & \frac{\partial u_1}{\partial x_3} \\ \frac{\partial u_2}{\partial x_1} & \frac{\partial u_2}{\partial x_2} & \frac{\partial u_2}{\partial x_3} \\ \frac{\partial u_3}{\partial x_1} & \frac{\partial u_3}{\partial x_2} & \frac{\partial u_3}{\partial x_3} } \parenVectorstack{\ud x_1 \\ \ud x_2 \\ \ud x_3}$$
\end{document}


You might try using \\[0.5em] (or your favorite spacing) to spread out all the arrays vertically so that the rows line up horizontally. This will also relieve some of the crowding in the 3x3 array caused by your use of \displaystyle.