# The correct layout for mathematical derivations

I am writing my 2nd TeX document. Therefore I am still at the beginning. I have a chapter inside my Appendix, which is dealing with small derivations. However, I am not quite sure if my layout/form is correct.

Based on one comment of this post I avoid the use of \Leftrightarrow

Here is a short MWE of one derivation.

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{aligned-overset}

\newcommand{\Lagr}{\mathcal{L}}
\newcommand{\matr}[1]{\mathbf{#1}}
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\yct}{\ubar{\y}^\mathbf{T}} %Zentriert
\newcommand{\betahat}{\hat{\beta}} %betahat

\begin{document}
\begin{flalign*}
\Lagr(\beta_{0},\beta) &=\min\limits_{\beta_{0}, \beta} \left\{ \frac{1}{2} \sum_{i=1}^N\left(y_{i}-\beta_{0} - \sum_{j=1}^p\beta_{j}\tilde{x}_{ij}\right)^2  +\lambda\sum_{j=1}^p\left|\beta_{j}\right| \right\} &\\
&= \min\limits_{\beta_{0}, \beta} \left\{ \frac{1}{2} \sum_{i=1}^N\left(y_{i}-\beta_{0} - \sum_{j=1}^p\beta_{j}(x_{ij}-\bar{x}_{j})\right)^2  +\lambda\sum_{j=1}^p\left|\beta_{j}\right| \right\}  &
\end{flalign*}
\vspace*{-1cm}
\begin{flalign*}
&\frac{\partial \Lagr}{\partial \beta_{0}} \overset{!}{=}0 \\
&-\sum_{i=1}^N\left(y_{i}-\betahat_{0} - \sum_{j=1}^p\betahat_{j}(x_{ij}-\bar{x}_{j})\right) = 0 \\
& -\sum_{i=1}^{N}y_i + N \betahat_{0}+\sum_{i=1}^N\left(\sum_{j=1}^p\betahat_{j}x_{ij}\right) - N\sum_{j=1}^{p}\betahat_{j}\bar{x}_{j} =0 \\
& -\frac{1}{N}\sum_{i=1}^{N}y_i +\betahat_{0}+  \sum_{j=1}^p\left(\betahat_{j}\frac{1}{N}\sum_{i=1}^N x_{ij} \right) - \sum_{j=1}^p\betahat_{j}\bar{x}_{j}=0 \\
& -\bar{y}+\betahat_{0}+\sum_{j=1}^p\betahat_{j}\bar{x}_{j}-\sum_{j=1}^p\betahat_{j}\bar{x}_{j}=0\\
& \betahat_{0} =\bar{y}\\
\intertext{\textnormal{Aus der Annahme, dass $\y$ zentriert ist $\frac{1}{N}\sum_{i=1}^{N}\tilde{y}_{i}=0$, folgt}}
&\betahat_{0}=0  &
\end{flalign*}
\end{document}


The code can certainly be written more elegantly, but I am primarily interested in the optical layout of the derivation. I would be very grateful for help and tips.

• One idea is to write equations instead of equalities. I.e. write "A=B=C" and then solve for "C=0" and not "A=0" iff "B=0" iff "C=0". Naturally that only works if things are actually equal. But in your example the first few steps are equalities and then you divide by "N". – moewe Aug 21 '18 at 10:38
• I am not sure what do you mean, may you want ellaborate a bit – rook1996 Aug 21 '18 at 10:41
• Something like gist.github.com/moewew/eb79abfe72a6462132d2aa0f2eca1ab5 I prefer to use alignment on equal signs, so the equalities scan easier. I find strings of equivalences tedious because they always have two sides I have to worry about. So I prefer equations with one LHS and then the RHSes nicely aligned. Of course in that case that means that I reformulated the derivation a bit, that's why I posted a comment and not an answer. – moewe Aug 21 '18 at 11:19

I can suggest some improvements.

1. There's no need to stack two flalign* environments.
2. There's no need for \intertext.
3. \left( and \right) should be \biggl( and \biggr).
4. Also \left\{ and \right\} might benefit from the above; see the first and second line to see the difference; with \left and \right you get oversized braces.
5. There's no need for \limits after \min.
6. ^2 after a big parenthesis should be ^{\!2}.
7. &- should be &{-} in order for the minus sign to be “unary” and so left aligned.
\documentclass[a4paper,12pt]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{aligned-overset}

\newcommand{\Lagr}{\mathcal{L}}
\newcommand{\matr}[1]{\mathbf{#1}}
\newcommand{\X}{\matr{X}} %Matrix von X
\newcommand{\y}{\matr{y}} %y als voller Vektor
\newcommand{\yct}{\ubar{\y}^\mathbf{T}} %Zentriert
\newcommand{\betahat}{\hat{\beta}} %betahat

\begin{document}
\begin{flalign*}
&\begin{aligned}
\Lagr(\beta_{0},\beta) &=\min_{\beta_{0}, \beta} \left\{ \frac{1}{2} \sum_{i=1}^N\biggl(y_{i}-\beta_{0} - \sum_{j=1}^p\beta_{j}\tilde{x}_{ij}\biggr)^{\!2}  +\lambda\sum_{j=1}^p\left|\beta_{j}\right| \right\} &&\\
&=\min_{\beta_{0}, \beta} \biggl\{ \frac{1}{2} \sum_{i=1}^N\biggl(y_{i}-\beta_{0} - \sum_{j=1}^p\beta_{j}(x_{ij}-\bar{x}_{j})\biggr)^{\!2}  +\lambda\sum_{j=1}^p\left|\beta_{j}\right| \biggr\}
\end{aligned} && \\
&\frac{\partial \Lagr}{\partial \beta_{0}} \overset{!}{=}0 \\
&{-}\sum_{i=1}^N\biggl(y_{i}-\betahat_{0} - \sum_{j=1}^p\betahat_{j}(x_{ij}-\bar{x}_{j})\biggr) = 0 \\
&{-}\sum_{i=1}^{N}y_i + N \betahat_{0}+\sum_{i=1}^N\biggl(\sum_{j=1}^p\betahat_{j}x_{ij}\biggr) - N\sum_{j=1}^{p}\betahat_{j}\bar{x}_{j} =0 \\
&{-}\frac{1}{N}\sum_{i=1}^{N}y_i +\betahat_{0}+  \sum_{j=1}^p\biggl(\betahat_{j}\frac{1}{N}\sum_{i=1}^N x_{ij} \biggr) - \sum_{j=1}^p\betahat_{j}\bar{x}_{j}=0 \\
&{-}\bar{y}+\betahat_{0}+\sum_{j=1}^p\betahat_{j}\bar{x}_{j}-\sum_{j=1}^p\betahat_{j}\bar{x}_{j}=0\\
&\betahat_{0} =\bar{y}
\end{flalign*}
Aus der Annahme, dass $\y$ zentriert ist $\frac{1}{N}\sum_{i=1}^{N}\tilde{y}_{i}=0$, folgt
\begin{flalign*}
&\betahat_{0}=0  &&
\end{flalign*}
\end{document}


• That helps a lot ! – rook1996 Aug 21 '18 at 10:52
• How do you determine whether to use \big, \bigg and similar commands or delimiters like \left and \right? – YiFan Aug 21 '18 at 10:54
• Also, what is the reason for using braces around ^2 and when should these be used? – YiFan Aug 21 '18 at 10:55
• Using \Bigl( … \Bigr) is still more pleasing to the eye, in my opinion. – Bernard Aug 21 '18 at 10:58
• @user496634 The “right” size is known from experience. ^{2} should be preferred anyway, although ^2 works as well. But when the exponent contains more than a single token, the braces are mandatory. – egreg Aug 21 '18 at 11:08