3

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?

Instead of just this: enter image description here

I want it to look something like this:

enter image description here This is my code:

\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}
  • 1
    Welcome to TeX.SX! You could use a \parbox inside of \text – egreg Feb 27 '16 at 20:26
3

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, since GKA presumably doesn't represent the product of variables named G, K, and A, you should write that term as either \mathit{GKA} or \mathrm{GKA}.

enter image description here

\documentclass[a4paper,oneside,article,leqno]{memoir}
\pagestyle{title}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage[danish]{babel}
\renewcommand{\danishhyphenmins}{22}

\usepackage{sistyle, mathtools, amssymb}
\usepackage[margin=1.0in]{geometry}

\newcommand\textbox[2]{\parbox{#1}{\raggedright #2}}
\newcommand\tallstrut{\vphantom{\sum\limits_{i=1}^{n}}} % tall typographic strut

\begin{document}
\begin{flalign*}
\mathit{GKA}(a,b)
&=\frac{1}{n}\sum_{i=1}^{n}(y_i-(ax_i+b))^2 
&&\text{Anvend hjælpesætning b.}\\
&=\frac{1}{n}\sum_{i=1}^{n} \bigl(\,
    {\underbrace{(y_i-\bar{y})}_{s}} 
   -{\underbrace{a(x_i-\bar{x})}_{t}} 
   +{\underbrace{(\bar{y}-(a\bar{x}+b)}_{u}}\,\bigr)^2
&&\text{Anvend omskrivning 2.}\\
&=\frac{1}{n}\sum_{i=1}^{n} 
\begin{pmatrix*}[l]
{\underbrace{(y_i-\bar{y})^2}_{s^2}}+
{\underbrace{a^2(x_i-\bar{x})^2}_{t^2}}+
{\underbrace{(\bar{y}-(a\bar{x}+b))^2}_{u^2}}\\
\tallstrut-{\underbrace{2a(y_i-\bar{y})(x_i-\bar{x})}_{2st}}
+{\underbrace{2(y_i-\bar{y})(\bar{y}-(a\bar{x}+b))}_{2su}}\\
\tallstrut-{\underbrace{2a(x_i-\bar{x})(\bar{y}-(a\bar{x}+b))}_{2tu}}
\end{pmatrix*}
&&\textbox{4.5cm}{Gang med $\frac{1}{n}\sum_{i=1}^{n}$ 
                  og brug hjælpesætning a.}\\ 
&=\frac{1}{n}\sum_{i=1}^{n}(y_i-\bar{y})^2+
  \frac{1}{n}a^2\sum_{i=1}^{n}(x_i-\bar{x})^2+
  \frac{1}{n}\sum_{i=1}^{n}(\bar{y}-(a\bar{x}+b))^2
&&\textbox{4.5cm}{Brug at $V_1=$ gennemsnittet af $x_i$-erne}\\
&\quad-2a\frac{1}{n}\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})
 +2(\bar{y}-(a\bar{x}+b))\frac{1}{n}\sum_{i=1}^{n}(y_i-\bar{y})\\
&\quad-2a(\bar{y}-(a\bar{x}+b))\frac{1}{n}\sum_{i=1}^{n}(x_i-\bar{x})\\
&=V_2+a^2V_1+\frac{1}{n} n(\bar{y}-a\bar{x}-b)^2-2Ca+0-0\\
&=V_1 a^2-2Ca+V_2+(\bar{y}-a\bar{x}-b)^2\\
&=f(a)+g(a,b)
\end{flalign*}
\end{document}
  • I'd also recommend {\underbrace{<math>}_{...}} with an additional pair of braces to make the whole thing an ordinary atom instead of a \mathop. – egreg Feb 27 '16 at 21:39
  • @egreg - Many thanks for this suggestion; I've implemented it and uploaded a new screenshot. – Mico Feb 27 '16 at 22:02
1

Here is a way to do it, with \parboxes, and text in footnote size. Il also reduced the size of the matrix, with the medsize environment from nccmath.

Note that if you load mathtools, you don't have to load amsmath, since this package does it for you.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
%\usepackage{sistyle, amsmath}
\usepackage{mathtools,amssymb, nccmath}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[danish]{babel}
\renewcommand{\danishhyphenmins}{22}
\renewcommand{\danishhyphenmins}{22}

\begin{document}
\begin{flalign*}
  GKA(a,b)&=\frac{1}{n}\sum\limits_{i=1}^{n}(y_i-(ax_i+b))^2 & & & &\text{\footnotesize 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{\footnotesize Anvend omskrivning 2.} \\
  &=\frac{1}{n}\sum\limits_{i=1}^{n}
  \mathrlap{\begin{medsize}\begin{pmatrix*}[l]
    \vphantom{\frac{1}{n}\sum\limits_{i=1}^{n}}\underbrace{(y_i-\bar{y})^2}_{s^2}{} + a^2\underbrace{(x_i-\bar{x})^2}_{t^2} +\underbrace{(\bar{y}-(a\bar{x}+b))^2}_{u^2} \\
    \vphantom{\frac{1}{n}\sum\limits_{i=1}^{n}}-\underbrace{2a(y_i-\bar{y})(x_i-\bar{x})}_{2st} + \underbrace{2(y_i-\bar{y})(\bar{y})-(a\bar{x}+b)}_{2su} \\
    \vphantom{\frac{1}{n}\sum\limits_{i=1}^{n}}-\underbrace{2a(x_i-\bar{x})(\bar{y})-(a\bar{x}+b))}_{2tu}
    \end{pmatrix*}\end{medsize}} & & & & \parbox[t]{4cm}{\footnotesize Gang med $\frac{1}{n}\sum\limits_{i=1}^{n}$ og brug hjælpesætning a.}\\
  & =\!\mathrlap{\begin{aligned}[t]\frac{1}{n} & \sum_{i=1}^{n}(y_i-\bar{y})^2
    + \frac{1}{n}a^2\sum_{i=1}^{n}(x_i-\bar{x})^2 + \frac{1}{n}\sum_{i=1}^{n}(\bar{y}-(a\bar{x}+b))^2 & \\
    &-2a\cdot\smash{\frac{1}{n}\sum_{i=1}^{n}}(x_i-\bar{x})(y_i-\bar{y})\\
    & + 2(\bar{y}-(a\bar{x}+b)) \cdot\frac{1}{n}\sum_{i=1}^{n} (y_i-\bar{y}) -2a(\bar{y}-(a\bar{x}+b))\cdot\frac{1}{n}\sum_{i=1}^{n}(x_i-\bar{x})
    \end{aligned}}& & & & \parbox[t]{4cm}{\footnotesize Brug at $V_1=$ gennemsnittet af $x_i$-erne. \\ Some text. Some text. Some text. Some text}\\[-0.5ex]
  &=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} 

enter image description here

  • Is it a good idea to have \sum\limits in text mode? Careful with Danish hyphenation patterns -- apparently, "te-xt" is OK... :-) – Mico Feb 27 '16 at 22:06
  • It was in the O.P.'s post. Furthermore, the nolimits versiontakes more horizontal space, in small \parbox. So I thought it would be wiser to leave it as is. However I agree this is debatable. – Bernard Feb 27 '16 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.