# Aligned vector, matrix, vector, multiplication

I already have something that looks good to me, but the latex implementation is hideous. Any ideas for how this could be done in a cleaner way?

$\frac{dz}{dt} = \raisebox{1.02em} { \boldsymbol{[}\begin{array}{cc} 1 & -1 \\ \end{array}\boldsymbol{]} } \left[\begin{array}{cc} \frac{\partial \underline{y}}{\partial \underline{x}} & \frac{\partial \overline{y}}{\partial \underline{x}} \\ [0.5em] \frac{\partial \underline{y}}{\partial \overline{x}} & \frac{\partial \overline{y}}{\partial \overline{x}} \\ \end{array}\right] \left[\begin{array}{c} 1 \\[0.5em] -1 \\ \end{array}\right].$


• Is the example what you think looks good, or what you think looks hideous? I would personally prefer the first vector to be aligned centrally, but I get why one might prefer pushing it up. And the \partial symbols are not aligned horizontally which I don't love, I guess they're being pushed up by the \underline{}? – Charles Angus Apr 29 at 22:44
• Welcome to TeX.SE. – Mico Apr 29 at 22:44
• I think that the code looks hideous. I'm happy with way the rendered image looks. I wanted to align the tops of all the braces and have equally sized braces because that's how I think most people would write it. This misalignment of the \partials is a nightmare. I didn't notice. Any fixes for that? – Jesse Apr 29 at 23:45
• I think the bigger brackets may be an improvement as well. – Jesse Apr 29 at 23:57
• Don't worry about the “d”. Contrary to what @Schrödinger'scat thinks, mathematicians are happy to use an italic “d” for the differential and have mathematical reasons to justify the choice. – egreg Apr 30 at 8:19

Getting alignments with so inhomogeneous objects requires a bit of manual adjusting.

I raised the first row vector “by eye”; it would be possible to compute the amount of raising exactly, but I did it just to show that it's really awful and unhelpful for the reader. Why the top and not the bottom? There's no reason for choosing either, so the right place is at the center.

More importantly, I added some shorthands that help both in typing and in reading code.

Some “phantoms” make the objects align nicely.

\documentclass{article}
\usepackage{amsmath,bm}

\newenvironment{rowvector}
{\bm{[}\begin{matrix}}
{\end{matrix}\bm{]}}

\newcommand{\pder}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\ol}{\overline}
\newcommand{\ul}{\underline}

\begin{document}

$\frac{dz}{dt} = \raisebox{3ex}{\begin{rowvector} 1 & -1 \end{rowvector}} \begin{bmatrix} \pder{\ul{y}}{\ul{x}} & \pder{\ol{y}\vphantom{\ul{y}}}{\ul{x}} \\[0.5em] \pder{\ul{y}}{\ol{x}} & \pder{\ol{y}\vphantom{\ul{y}}}{\ol{x}} \end{bmatrix} \begin{bmatrix} 1 \vphantom{\pder{\ul{y}}{\ul{x}}} \\[0.5em] -1 \vphantom{\pder{\ul{y}}{\ul{x}}} \end{bmatrix}$

$\frac{dz}{dt} = \begin{rowvector} 1 & -1 \end{rowvector} \begin{bmatrix} \pder{\ul{y}}{\ul{x}} & \pder{\ol{y}\vphantom{\ul{y}}}{\ul{x}} \\[0.5em] \pder{\ul{y}}{\ol{x}} & \pder{\ol{y}\vphantom{\ul{y}}}{\ol{x}} \end{bmatrix} \begin{bmatrix} 1 \vphantom{\pder{\ul{y}}{\ul{x}}} \\[0.5em] -1 \vphantom{\pder{\ul{y}}{\ul{x}}} \end{bmatrix}$

\end{document}


• Although a number of the answers gave excellent solutions, I accepted this because it was the best for my question. The code is clean and the result is in the same style as my solution, but with multiple improvements (e.g. the bracket alignment and spacing looks better imho). I have no idea what did it, but even the partials look aligned. – Jesse Apr 30 at 15:22
• @Jesse Look in the code for \vphantom that inserts an invisible object with the same vertical size of what's specified in the argument. – egreg Apr 30 at 15:30

\end{document}


I tried a number of things, but finally settled on this. I \smashed the matrix elements and increased \arraystretch until the spacing look good. Then I put the row vector into \raisebox and adjusted the alignment again by hand.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\def\arraystretch{1.6}% increase until matrix looks good
\frac{dz}{dt} = \raisebox{2.2ex}% adjust until ones are lined up {\displaystyle \begin{bmatrix} 1 & -1 \end{bmatrix}} \begin{bmatrix} \smash{\frac{\partial\underline{y}}{\partial\underline{x}}} & \smash{\frac{\partial\overline{y}}{\partial\underline{x}}}\\ \smash{\frac{\partial\underline{y}}{\partial\overline{x}}} & \smash{\frac{\partial\overline{y}}{\partial\overline{x}}} \end{bmatrix} \begin{bmatrix*}[r] 1 \\% \llap{\rule{1.5in}{0.5pt}}\\% to chaeck alignment -1 \end{bmatrix*}\,.
\end{document}


This is perhaps a little goofy, but I think it's in a way cleaner. Slightly different result.

I use \vphantom to create space to line things up programmatically without using explicit numbers, and define a command which holds the taller of your partial fractions so you only need to change the spacing in one place.

I use \bigl[ instead of \boldsymbol{[} (the \boldsymbol does not actually appear to do anything).

I line up the row vector with the center of the upper row of the other elements, rather than push it right to the top.

\newcommand{\partialfractall}{\frac{\partial \underline{y}}{\partial \underline{x}}}

$\frac{dz}{dt} = \begin{array}{cc} \bigl[\begin{array}{cc} 1 & -1 \end{array}\bigr] \\ \vphantom{\partialfractall{}} \end{array} \left[\begin{array}{cc} \frac{\partial \underline{y}}{\partial \underline{x}} & \frac{\partial \overline{y}}{\partial \underline{x}} \\ [0.5em] \frac{\partial \underline{y}}{\partial \overline{x}} & \frac{\partial \overline{y}}{\partial \overline{x}} \\ \end{array}\right] \left[% \vphantom{\begin{array}{cc} \frac{\partial \underline{y}}{\partial \underline{x}} & \frac{\partial \overline{y}}{\partial \underline{x}} \\ [0.5em] \frac{\partial \underline{y}}{\partial \overline{x}} & \frac{\partial \overline{y}}{\partial \overline{x}} \\ \end{array}}% \begin{array}{c} \vphantom{\partialfractall{}} 1 \\ \vphantom{\partialfractall{}} -1 \\ \end{array}\right].$
`