I'm trying to explain my steps in an equation. The problem is that the text is longer than the line in the \flalign*
environment, is there any way to make the text span over multiple lines while maintaining alignment?
I want it to look something like this:
\documentclass[a4paper,oneside,article,leqno]{memoir}
\pagestyle{title}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[danish]{babel}\renewcommand{\danishhyphenmins}{22}
\renewcommand{\danishhyphenmins}{22}
\usepackage{sistyle, amsmath}
\usepackage{mathtools,amssymb}
\usepackage[margin=1.0in]{geometry}
\begin{document}
\begin{flalign*}
GKA(a,b)&=\frac{1}{n}\sum\limits_{i=1}^{n}(y_i-(ax_i+b))^2 &&\text{Anvend hjælpesætning b.}\\
&=\frac{1}{n}\sum\limits_{i=1}^{n}(\underbrace{(y_i-\bar{y})}_\text{s}-\underbrace{a(x_i-\bar{x})}_\text{t}+\underbrace{(\bar{y}-(a\bar{x}+b)}_\text{u})^2&&\text{Anvend omskrivning 2.}\\
&=\frac{1}{n}\sum\limits_{i=1}^{n}
\begin{pmatrix*}[l]
\vphantom{\frac{1}{n}\sum\limits_{i=1}^{n}}\underbrace{(y_i-\bar{y})^2}_\text{$s^2$}+\underbrace{a^2(x_i-\bar{x})^2}_\text{$t^2$}+\underbrace{(\bar{y}-(a\bar{x}+b))^2}_\text{$u^2$}\\
\vphantom{\frac{1}{n}\sum\limits_{i=1}^{n}}-\underbrace{2a(y_i-\bar{y})(x_i-\bar{x})}_\text{2st}+\underbrace{2(y_i-\bar{y})(\bar{y}-(a\bar{x}+b))}_\text{2su}\\
\vphantom{\frac{1}{n}\sum\limits_{i=1}^{n}}-\underbrace{2a(x_i-\bar{x})(\bar{y}-(a\bar{x}+b))}_\text{2tu}
\end{pmatrix*}&&\text{Gang med $\frac{1}{n}\sum\limits_{i=1}^{n}$ og brug hjælpesætning a.}\\
&=\frac{1}{n}\sum\limits_{i=1}^{n}(y_i-\bar{y})^2+\frac{1}{n}a^2\sum\limits_{i=1}^{n}(x_i-\bar{x})^2+\frac{1}{n}\sum\limits_{i=1}^{n}(\bar{y}-(a\bar{x}+b))^2&&\text{Brug at $V_1=$ gennemsnittet af $x_i$-erne}\\
&\quad-2a\cdot\frac{1}{n}\sum\limits_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})+2(\bar{y}-(a\bar{x}+b))\cdot\frac{1}{n}\sum\limits_{i=1}^{n}(y_i-\bar{y})\\
&\quad-2a(\bar{y}-(a\bar{x}+b))\cdot\frac{1}{n}\sum\limits_{i=1}^{n}(x_i-\bar{x})\\
&=V_2+a^2V_1+\frac{1}{n}\cdot n(\bar{y}-a\bar{x}-b)^2-2Ca+0-0\\
&=V_1\cdot a^2-2Ca+V_2+(\bar{y}-a\bar{x}-b)^2\\
&=f(a)+g(a,b)
\end{flalign*}
\end{document}
Best Answer
In addition to placing the explanatory text that is supposed to be automatically wrapped in a
\parbox
of a suitably chosen width (in the code below, I've chosen 4.5cm) that sets its material in ragged-right mode (in order to avoid big interword gaps), you may also want to eliminate all unneeded\text
directives as well as get rid of all but one of the\limits
modifiers; in display-math mode,\sum
and\sum\limits
produce the exact same output. Separately, sinceGKA
presumably doesn't represent the product of variables namedG
,K
, andA
, you should write that term as either\mathit{GKA}
or\mathrm{GKA}
.