Vertically center equation number in aligned equation

I have these aligned equations for which I would like to have a single equation number. I found that this can be simply solved by embedding an aligned environment in an equation, but now I have on formula for which the last line is too long to add an equation number to the right. This causes the equation number, which would actually fit nicely to the right if vertically centred, to be wrapped below the equation.

\documentclass[12pt, oneside]{scrbook}

\usepackage{amsmath}

\DeclareMathOperator{\E}{E}
\newcommand{\with}{\;;}
\newcommand{\dx}[1]{\mathop{\mathrm{d}#1}}
\newcommand{\expect}[2][]{\E_{#1} \left[#2\right]}

\begin{document}
\begin{aligned} \mu_{D_-} & = \expect{f'(S_-) \cdot \sum_{i=1}^{N} W D} \\ & = N \mu_W \mu_D \int_{-\infty}^{\infty} f'(s) p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} \\ \sigma_{D_-}^2 & = \expect{f'(S_-)^2 \cdot \left(\sum_{i=1}^{N} W D\right)^2} - \mu_{D_-}^2 \\ & = N \left(\sigma_W^2 \sigma_D^2 + \mu_W^2 \sigma_D^2 + \sigma_W^2 \mu_D^2 + N \mu_W^2 \mu_D^2\right) \int_{-\infty}^{\infty} f'(s)^2 p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} - \mu_{D_-}^2 \,. \end{aligned}
\end{document}


It just bothers me slightly and I was wondering whether there is a way to force a vertically centred equation number in this case. I found this answer, but all of the things that I have tried to shorten that expression were unsuccessful. I also would like to keep the last expression on one line to stay consistent with the rest of my document.

Is there any way to force the equation number vertically centred to the right, although my last expression is too long? Or any trick to make the equation believe that the aligned part is not as long as it actually is, so that the equation number appears vertically centred on the right (above the equation that would be too long)?

This is a case where visual formatting seems necessary, so it should be dealt with only in the very last stage of document preparation.

You can pretend that the very last part of the formula “is not there”:

\documentclass[12pt, oneside]{scrbook}

\usepackage{amsmath,mathtools}

\DeclareMathOperator{\E}{E}
\newcommand{\with}{\,;}
\newcommand{\dx}{\mathop{}\!\mathrm{d}}
\newcommand{\expect}[2][]{\E\if\relax\detokenize{#1}\relax\else_{#1}\fi \left[#2\right]}

\begin{document}

\begin{aligned} \mu_{D_-} &= \expect{f'(S_-) \cdot \sum_{i=1}^{N} W D} \\ &= N \mu_W \mu_D \int_{-\infty}^{\infty} f'(s) p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} \\ \sigma_{D_-}^2 &= \expect{f'(S_-)^2 \cdot \left(\sum_{i=1}^{N} W D\right)^2} - \mu_{D_-}^2 \\ &= N(\sigma_W^2 \sigma_D^2 + \mu_W^2 \sigma_D^2 + \sigma_W^2 \mu_D^2 + N \mu_W^2 \mu_D^2) \int_{-\infty}^{\infty} f'(s)^2 p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} \mathrlap{{} - \mu_{D_-}^2 \,.} \end{aligned}

\end{document}


I amended your definition of \dx, because it would give wrong spacing on the right. Also \; seems too big for \with (to be honest, I wouldn't add a space before the semicolon).

Your definition of \expect with automatic \left and \right produces too big delimiters in many cases (see the third line, for instance).

Here's an amended version that uses xparse and mathtools features; the syntax for \expect is

\expect{formula}
\expect[\big]{formula}
\expect*{formula}

\expect_{sub}{formula}
\expect_{sub}[\big]{formula}
\expect_{sub}*{formula}


as shown in the added equation. In the optional argument one can use \big, \Big, \bigg or \Bigg; the *-version uses \left and \right.

Note also the addition of \, and \! in a few places.

• Hi, very good. I like much your picture created with mac retina :-) monitor. Good work +1. – Sebastiano Oct 17 '17 at 9:02

In addition to the \mathrlap solution, you have the possibility to replace aligned with split and use the \raisetag command. I took the liberty to adjust manually the size of a couple of delimiters and the placement of an exponent:

\documentclass[12pt, oneside]{scrbook}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}

\usepackage{mathtools}

\DeclareMathOperator{\E}{E}
\newcommand{\with}{\;;}
\newcommand{\dx}[1]{\mathop{\mathrm{d}#1}}
\newcommand{\expect}[2][]{\E_{#1} \left[#2\right]}

\begin{document}

$$\begin{split}\raisetag{15ex} \mu_{D_-} & = \expect{f'(S_-) \cdot \sum_{i=1}^{N} W D} \\ & = N \mu_W \mu_D \int_{-\infty}^{\infty} f'(s) p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} \\ \sigma_{D_-}^2 & = \expect{f'(S_-)^2 \cdot \biggl(\sum_{i=1}^{N} W D\biggr)^{\!\!2}} - \mu_{D_-}^2 \\ & = N \bigl(\sigma_W^2 \sigma_D^2 + \mu_W^2 \sigma_D^2 + \sigma_W^2 \mu_D^2 + N \mu_W^2 \mu_D^2\bigr) \int_{-\infty}^{\infty} f'(s)^2 p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} - \mu_{D_-}^2 \,. \end{split}$$

\end{document}


last line of your equation is to long for text width, so equation number is pushed below equation. split this line into two lines:

\documentclass[12pt, oneside]{scrbook}
\usepackage{mathtools}

\DeclareMathOperator{\E}{E}
\newcommand{\with}{\;;}
\newcommand{\dx}[1]{\mathop{\mathrm{d}#1}}
\newcommand{\expect}[2][]{\E_{#1} \left[#2\right]}

\begin{document}
\begin{aligned} \mu_{D_-} & = \expect{f'(S_-) \cdot \sum_{i=1}^{N} W D} \\ & = N \mu_W \mu_D \int_{-\infty}^{\infty} f'(s) p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} \\ \sigma_{D_-}^2 & = \expect{f'(S_-)^2 \cdot \left(\sum_{i=1}^{N} W D\right)^2} - \mu_{D_-}^2 \\ & = \begin{multlined}[t][0.6\linewidth] % defined in mathtools N \left(\sigma_W^2 \sigma_D^2 + \mu_W^2 \sigma_D^2 + \sigma_W^2 \mu_D^2 + N \mu_W^2 \mu_D^2\right) \cdot\\[1ex] \int_{-\infty}^{\infty} f'(s)^2 p_\mathcal{N}(s \with \mu_{S_-}, \sigma_{S_-}) \dx{s} - \mu_{D_-}^2 \,. \end{multlined} \end{aligned}
\end{document}

• I actually hoped to find a solution where I could leave the last expression on one line and force the equation number to appear to the right. I have edited my question to make this more clear. – Mr Tsjolder Oct 17 '17 at 8:47