Home > front end >  How to line break in RMarkdown Displayed Equation Chunk
How to line break in RMarkdown Displayed Equation Chunk

Time:12-01

I am new to RMarkdown and I have the following I am knitting into PDF

$$

log( \text{employed}) = \beta_0   \beta_1*log( adult \ wage )   \\  
\beta_2*jobs \ created  \beta_3*jobs \ destroyed   \beta_4*mw   \\  
\beta_5*teen \ pop   \beta_6*teen \ wage* \mu_i   \delta_{it}   \alpha_{it}   \kappa_{ir} 

$$

While the display that appears under this chunk projects the line breaks correctly, the output file has the equation run off the margins

CodePudding user response:

Maybe this solution?

 header-includes:
 - \usepackage{amsmath}

\begin{equation}
\begin{split}
log( \text{employed}) = \beta_0   \beta_1*log( adult \ wage )   \\  
\beta_2*jobs \ created  \beta_3*jobs \ destroyed   \beta_4*mw   \\  
\beta_5*teen \ pop   \beta_6*teen \ wage* \mu_i   \delta_{it}   \alpha_{it}   \kappa_{ir} 
\end{split}
\end{equation}

enter image description here

or this:

\begin{gather}
log( \text{employed}) = \beta_0   \beta_1*log( adult \ wage )   \nonumber \\  
\beta_2*jobs \ created  \beta_3*jobs \ destroyed   \beta_4*mw    \\  
\beta_5*teen \ pop   \beta_6*teen \ wage* \mu_i   \delta_{it}   \alpha_{it}   \kappa_{ir} \nonumber
\end{gather}

enter image description here

Second looks cooler ;)

CodePudding user response:

I would use an align* environment in this case, that allows alligned or multiline equations. The *suppresses the equation numbers. In addition I made a backslash in front of the \log, added \_ and used \cdotinstead of a multiplication * and finally placed the at the begining under the =.

\begin{align*}
\log(\text{employed}) & = \beta_0   \beta_1 \cdot \log( adult\_wage )\\  
                       &   \beta_2 \cdot jobs\_created   \beta_3 \cdot jobs\_destroyed   \beta_4 \cdot mw\\  
                       &   \beta_5 \cdot teen\_pop   \beta_6 \cdot teen\_wage \cdot \mu_i   \delta_{it}   \alpha_{it}   \kappa_{ir} 
\end{align*}

enter image description here

You may also consider to typeset multiletter variables upright as text or to remove redundant \cdot multiplication operators if you want.

CodePudding user response:

I think this is more of a LaTeX question than an RMarkdown one.

I'm actually surprised it's rendering your display like you want at all -- LaTeX doesn't usually like having line breaks inside the displaymath environments. I'm also surprised that your document is successfully knitting for that same reason, but I guess you're knitting to HTML which is apparently a bit more forgiving than knitting to PDF.

At any rate, here is a possible fix:

\begin{align*}
log( \text{employed}) & = \beta_0   \beta_1*log( adult \ wage )   \\
& \qquad \beta_2*jobs \ created  \beta_3*jobs \ destroyed   \beta_4*mw    \\
& \qquad \beta_5*teen \ pop   \beta_6*teen \ wage* \mu_i   \delta_{it}   \alpha_{it}   \kappa_{ir} 
\end{align*}

Changes:

  1. Replace the $$ displaymath environment with align*, which will give you better control over the left/right alignment. (The * character instructs TeX not to number this equation for you.)
  2. Use the & character to control how things should be aligned from left to right. The \qquad will add a bit of padding to offset the continuing lines to the right, which is the typical thing to do for expressions that extend over multiple lines.

Now, some further suggestions that I didn't implement above:

  1. Consider replacing your original log with \log for nice typsetting of that function.
  2. Consider adding a \text{...} wrapper around all your variable names (e.g. adult wage, jobs created, etc.) to make them look nice as well. You'll want to remove the \ spacing if you do this. Parentheses around these names may help readability when the name includes a space, or you could join the names with an underscore character (\_ in text mode).
  3. (This one is extremely nitpicky:) Notice that the spacing on the characters at the ends of your lines aren't quite right -- this is because LaTeX doesn't recognize that there's anything being added, because there's nothing on the right side of the operator. We can fix this by adding a bit of space before with \:, i.e.
... *\log(\text{adult\_wage}) \:   \\

EDIT: After reflecting on tpetzold's answer, I realized I should change the alignment keys: the second and third lines should start to the right of the equals sign, and should be offset to the right a bit for visual distinction.

  • Related