7

I have the following code:

\documentclass[fleqn, 12pt]{article} 
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\setlength{\parskip}{\baselineskip}%
\setlength{\parindent}{0pt}%
\begin{document}
\raggedright

From the model, 
\begin{align}
&E( Y_i - \bar{ Y } ) = E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \nonumber \\
&( \text{Since the first normal equation gives} \ \ Y_i  = \beta_0 + \beta_1 X_i \ \ \text{and} \ \ \bar{Y} = \beta_0 + \beta_1 \bar{X}) \nonumber \\
& \ \ \ \ \ \ \ \ \ \ \ \ \ \ = \beta_1( X_i - \bar{X} ) \nonumber
\end{align}

\end{document}

Notice that the lines

&E( Y_i - \bar{ Y } ) = E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \nonumber \\ 

and

& \ \ \ \ \ \ \ \ \ \ \ \ \ \ = \beta_1( X_i - \bar{X} ) \nonumber

are separated by the line

&( \text{Since the first normal equation gives} \ \ Y_i  = \beta_0 + \beta_1 X_i \ \ \text{and} \ \ \bar{Y} = \beta_0 + \beta_1 \bar{X}) \nonumber \\

Despite this, I want the line

& \ \ \ \ \ \ \ \ \ \ \ \ \ \ = \beta_1( X_i - \bar{X} ) \nonumber

to line up at the = sign with the first line

&E( Y_i - \bar{ Y } ) = E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \nonumber \\ 

This is why I put the \ \ \ \ \ \ \ \ \ \ \ \ \ \

Is it possible to somehow do this using &, whilst maintaining the line in the middle as it is? Or, perhaps, some other way to automatically have them align?

EDIT: The reason I had to use & to put the middle line as its own line is because, if I put it on the same line as the first equation, then it would go off the page margin.

Thanks.

  • 1
    Why not simply put since the first normal equation gives $Y_i = \beta_0 + \beta_1 X_i$ and $\bar{Y} = \beta_0 + \beta_1 \bar{X}$. after the display? There's little added value (if any) from putting the comment in the middle. – egreg Jun 3 '18 at 14:07
  • @egreg You mean after the entire multi-line equation? It’s mostly for pedagogical reasons. It clearly associates the comment with the line of the equation with which it is referring to. – The Pointer Jun 3 '18 at 14:11
  • And it hinders legibility and understandability. – egreg Jun 3 '18 at 14:15
5

Like this?

\documentclass[fleqn, 12pt]{article} 
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\setlength{\parskip}{\baselineskip}%
\setlength{\parindent}{0pt}%
\begin{document}
\raggedright

From the model, 
\begin{align}
E( Y_i - \bar{ Y } ) &= E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \nonumber \\
\rlap{(Since the first normal equation gives $Y_i  = \beta_0 + \beta_1 X_i$ and $\bar{Y} = \beta_0 + \beta_1 \bar{X}$)} \phantom{E( Y_i - \bar{ Y } )} \nonumber \\
& = \beta_1( X_i - \bar{X} ) \nonumber
\end{align}
\end{document}

enter image description here

  • Yes, that's it! Thank you for the assistance. – The Pointer Jun 3 '18 at 14:04
  • 1
    @Mico Oh, my apologies. You’re right, I should have waited. I just had an answer in mind and Stefan happened to exemplify it precisely. Again, my apologies. I will avoid this in the future. – The Pointer Jun 3 '18 at 14:16
7

I would put the explanatory text in a \parbox immediately below the material to the right of the first = symbol. That way, it's immediately clear that the explanatory text pertains to the material on the preceding line.

enter image description here

\documentclass[fleqn, 12pt]{article}
\usepackage{amsmath,amsfonts,amssymb}
\DeclareMathOperator{\E}{E} % <-- new (expectation operator)
\setlength{\parskip}{\baselineskip}
\setlength{\parindent}{0pt}

\begin{document}
\raggedright

From the model,
\begin{align*}
\E( Y_i - \bar{Y} ) 
&= \E[(\beta_0+\beta_1 X_i) - (\beta_0-\beta_1\bar{X})]  \\
&\qquad\parbox[t]{0.5\textwidth}{(since the first normal equation gives 
$Y_i = \beta_0 + \beta_1 X_i$ and $\bar{Y} = \beta_0 + \beta_1\bar{X}$)}\\
&= \beta_1(X_i-\bar{X}) \,.
\end{align*}

\end{document}
  • 1
    Ahh, that looks even better than what I was trying to do! I think I will try this. Thank you for the answer. – The Pointer Jun 3 '18 at 14:12
7

I would simply set the comment after the display (using “because” instead of “since”). It's more legible and in style with standard mathematical practice; to the contrary, the comment in the middle will raise doubts what it refers to.

Anyway, this is how you can do it; if the comment doesn't fit on one line it will wrap, as shown in the third example.

\documentclass[fleqn, 12pt]{article} 
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{graphicx}
\usepackage{parskip} % \parskip=\baselineskip is HUGE

\makeatletter
\newcommand{\devioustrick}[1]{%
  \ifmeasuring@\else
    \kern-\ifcase\expandafter1\maxcolumn@widths\fi
    \parbox{\dimexpr\linewidth-\mathindent\relax}{#1}%
  \fi
}
\makeatother

\begin{document}

From the model, 
\begin{align*}
E( Y_i - \bar{ Y } ) 
&= E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \\
&= \beta_1( X_i - \bar{X} )
\end{align*}
because the first normal equation gives 
$Y_i  = \beta_0 + \beta_1 X_i$ and 
$\bar{Y} = \beta_0 + \beta_1 \bar{X}$.

From the model, 
\begin{align*}
E( Y_i - \bar{ Y } ) 
={}& E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \\
&\devioustrick{
  (since the first normal equation gives
  $Y_i  = \beta_0 + \beta_1 X_i$ and
  $\bar{Y} = \beta_0 + \beta_1 \bar{X}$)
}\\
={}& \beta_1( X_i - \bar{X} )
\end{align*}

From the model, 
\begin{align*}
E( Y_i - \bar{ Y } ) 
={}& E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ] \\
&\devioustrick{
  (since the first normal equation gives
  $Y_i  = \beta_0 + \beta_1 X_i$ and
  $\bar{Y} = \beta_0 + \beta_1 \bar{X}$
  and since the definitive answer on life, the universe
  and everything is~$42$)
}\\
={}& \beta_1( X_i - \bar{X} )
\end{align*}

\end{document}

enter image description here

6

Use \intertext to place text between aligned equations. There is rarely any need to manually align material using 'hard' spaces. The amsmath package provides environments that meet all of the most common requirements.

\documentclass[fleqn, 12pt]{article}
\usepackage{amsmath}
\begin{document}
From the model,
\begin{align*}
E( Y_i - \bar{ Y } ) &= E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ]  \\
\intertext{Since the first normal equation gives $Y_i  = \beta_0 + \beta_1 X_i$ and $\bar{Y} = \beta_0 + \beta_1 \bar{X}$}
& = \beta_1( X_i - \bar{X} )
\end{align*}

\end{document}

equations

  • Thanks for the answer. The problem with this is that it removes the indentation of the middle line, which I'd prefer to keep. – The Pointer Jun 3 '18 at 13:59
  • 1
    @ThePointer You're removing all indentations; why should that line be indented? – egreg Jun 3 '18 at 14:01
  • @egreg I want it to be viewed as comment specifically with regards to that part of the equation, rather than something more major, if that makes sense. – The Pointer Jun 3 '18 at 14:03
0

Another solution is to just use \tag. This seems to be appropriate semantically, seeing as the text pertains to the previous equation-line.

From the model,
\begin{align*}
E( Y_i - \bar{ Y } ) &= E[ ( \beta_0 + \beta_1 X_i ) - ( \beta_0 - \beta_1 \bar{ X } ) ]
\tag{since the first normal equation gives $Y_i  = \beta_0 + \beta_1 X_i$ and $\bar{Y} = \beta_0 + \beta_1 \bar{X}$}\\
& = \beta_1( X_i - \bar{X} )
\end{align*}

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.