# Matrix within equation

I am having some difficulty getting this to display properly.

 $${\bf H} = -{\bf \mu} \cdot {\bf B} = - \gamma B_o {\bf S}_z = -\frac{\gamma B_o\hbar}{2} \begin{bmatrix} 1&0\\0&-1 \end{bmatrix}.$$


Which appears as,

I would like the matrix height to be consistant with the rest of the equation.

• Welcome to TeX.SX! Judging from the picture you seem to be using some setspace command or \linespread for increasing the leading. Can you confirm? By the way, avoid {\bf H} that should be \mathbf{H}. Oct 8, 2013 at 17:29
• Yes I am using setspace throughout my document. Thank you for the \mathbf{} tip! Oct 8, 2013 at 17:31

\documentclass{article}
\usepackage{amsmath}
\usepackage[nodisplayskipstretch]{setspace}
\doublespacing

\begin{document}
$$\mathbf{H} = -\mathbf\mu \cdot \mathbf{B} = -\gamma B_o \mathbf{S}_z = -\frac{\gamma B_o\hbar}{2} \begin{bmatrix} 1&0\\0&-1 \end{bmatrix}.$$

\end{document}

• You may want to use \boldsymbol{\mu} instead of \mathbf{\mu}.
– Mico
Oct 8, 2013 at 17:55
• You get Command \normalsize invalid in math mode at every display, which could be annoying. Oct 8, 2013 at 18:04

I'd modify the \env@matrix command, on which all the matrix constructions are built;

\documentclass{article}

\usepackage{amsmath,bm}
\usepackage{setspace}

\usepackage{lipsum} % just for this example

\makeatletter
\def\env@matrix{\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\renewcommand{\arraystretch}{1.2}%
\array{*\c@MaxMatrixCols c}}
\makeatother

\doublespacing

\begin{document}
\lipsum*[2]
$$\mathbf{H} = -\bm{\mu} \cdot \mathbf{B} = - \gamma B_o \mathbf{S}_z = -\frac{\gamma B_o\hbar}{2} \begin{bmatrix} 1&0\\0&-1 \end{bmatrix}.$$
\lipsum[3]
\end{document}


Try also without \renewcommand{\arraystretch}{1.2}, that I added in order to space out a bit the rows, which seems better with \doublespacing.

Actually, it's difficult to improve something using \doublespacing, which destroys any attempt at good typography.

In what does this differ from Mico's and Herbert's methods?

Mico's workaround works only within equation. Herbert's instead applies single spacing to all display environments, so also to align, for instance, reducing the line spacing between aligned equations. This might be desirable or not, it depends on you.

Note that {\bf H} should not be used, preferring \mathbf{H}. Also, you'll have noticed that {\bf \mu} doesn't embolden the mu; use \bm{\mu} and load the bm package as in my code.

You could use the etoolbox package and use its \AtBeginEnvironment command to reset spacing to \singlespacing at the start of an equation environment.

\documentclass{article}
\usepackage{amsmath}
\usepackage{setspace} % per the OP's information provided in a comment
\doublespacing
\usepackage{etoolbox}
\AtBeginEnvironment{equation}{\singlespacing}
\begin{document}
$$\mathbf{H} = -\boldsymbol{\mu} \cdot \mathbf{B} = - \gamma B_o \mathbf{S}_z = -\frac{\gamma B_o\hbar}{2} \begin{bmatrix} 1&0\\0&-1 \end{bmatrix}.$$
\end{document}


Remark: This method is quite simple if only the equation environment has to be fixed; it becomes tedious quickly if lots of different display-math environments occur in your document.

Well I thought there would be a simple (inelegant) answer and there is! Simply switch to singlespaceing and add some \vspace to make everything hunkydory!

Text here. . .Magnetic resonance imaging blah blah Hamiltonian. . .

\vspace{-0.5cm}
\singlespacing
$${\bf H} = -{\bf \mu} \cdot {\bf B} = - \gamma B_o {\bf S}_z = -\frac{\gamma B_o\hbar}{2} \begin{bmatrix} 1&0\\0&-1 \end{bmatrix}. \vspace{0.5cm}$$
\doublespacing


Thanks egreg for your comments! Note: egreg warns that this may cause the equation to move to the top of the next page, which is a big no no. Thus, this quick fix may not be the solution for everyone.

• You probably are leaving a blank line before the equation, which is wrong. Oct 10, 2013 at 14:08
• Leaving a blank line before an equation means that the equation can end up at the top of the next page, which is a typographic sin worse than leaving a widow line. Oct 10, 2013 at 14:39
• @egreg and Shinobii: it would be good to clean up and to remove the comments after the issue in the comments is solved. If there's a texnical value in the comments, it could be moved into the answer. Oct 10, 2013 at 15:51