0

I have a following problem. I prepare a project in R and always get a problem that after knitting formulas are partially outside of pdf document.

Formula issue formatting description

There is a post with similar problem, but it didn't help (R Markdown Knitr to PDF Code Outside Margin)

I have also tried adding "\\", but it also didn't help.

Is there any easy solution about how to organize auto line breaks in formulas in Rmarkdown? I don't want to convert html to pdf since I would lose all internal references then.

I will be very grateful if someone would help!!!

Update:

Here is a sample formula for rmarkdown:

---
title: "Code Sample"

output:

  pdf_document: default
  html_document: default

---
1) $$ (\sum_{i = 1}^{N} \|x_i - \mu - V_q \lambda_i\|^2)'_\mu = \\ 
(\sum_{i = 1}^{N} (x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\ 
= \sum_{i = 1}^{N} ((x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\ 
= \sum_{i = 1}^{N} 2 (x_i - \mu - V_q \lambda_i) \cdot (-1) = \\ 
= \sum_{i = 1}^{N} -2 \cdot (x_i - \mu - V_q \lambda_i) \\ 
\text{since the degree was 2 and the sign of $\mu$ was '-' (minus)} $$

2 Answers2

2

Welcome to SO, @Irina Mironova!

You could try this, using & to align and \\ to break lines:

---
title: "Aligned equations"
author: "bttomio"
date: "4/7/2021"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\begingroup\small
\begin{equation}
  \begin{aligned}
ER_t 
= & \lambda_1 + \sum_{i=0}^k\alpha_{11i}ER_{t-1} + \sum_{j=k+1}^p\alpha_{12j}ER_{t-j} + \sum_{i=0}^k\beta_{11i}CT_{t-1} + \sum_{j=k+1}^p\beta_{12j}CT_{t-j} \\
+ & \sum_{i=0}^k\gamma_{11i}IRD_{t-1} + \sum_{j=k+1}^p\gamma_{12j}IRD_{t-j} + \sum_{i=0}^k\delta_{11i}VIX_{t-1} + \sum_{j=k+1}^p\delta_{12j}VIX_{t-j} \\
+ & \sum_{i=0}^k\phi_{11i}SM_{t-1} + \sum_{j=k+1}^p\phi_{12j}SM_{t-j} + \sum_{i=0}^k\psi_{11i}SMUS_{t-1} + \sum_{j=k+1}^p\psi_{12j}SMUS_{t-j} +\varepsilon_{1t}
  \end{aligned} 
\end{equation}
\endgroup

-output

enter image description here


UPDATE

Following the equation supplied by the OP, here is a working example:

---
title: "Code example"
author: "bttomio"
date: "4/9/2021"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\begingroup
\begin{equation}
  \begin{aligned}
(\sum_{i = 1}^{N} \|x_i - \mu - V_q \lambda_i\|^2)'_\mu = \\
(\sum_{i = 1}^{N} (x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\
\sum_{i = 1}^{N} ((x_i - \mu - V_q \lambda_i)^T (x_i - \mu - V_q \lambda_i))'_\mu = \\
\sum_{i = 1}^{N} 2 (x_i - \mu - V_q \lambda_i) \cdot (-1) = \\
\sum_{i = 1}^{N} -2 \cdot (x_i - \mu - V_q \lambda_i) \\ 
\text{since the degree was 2 and the sign of $\mu$ was '$-$' (minus)}
  \end{aligned} 
\end{equation}
\endgroup

-output

enter image description here

bttomio
  • 2,206
  • 1
  • 6
  • 17
  • 2
    This is the way forward. Personally, I'd align at the pluses, so for the first line I'd do `ER_t = \lambda_1 + &` (but this is just personal preference) – jwalton Apr 08 '21 at 09:33
  • 1
    Hello, thank you:) Yes, it worked for me, but slightly in different way. I used \begin{math} $$ ... $$ \end{math}, otherwise R would through errors. Also, in this way \begingroup can be relaxed – Irina Mironova Apr 08 '21 at 09:59
  • bttomio, sorry, I was too fast. I tried your solution, and I have managed to launch it correctly with innovations I described above (without them I got an error). But than I have done the same thing for my solution (I added in the "update" part), but it didn't work: R says "! Missing $ inserted." Could you, please, show how to solve this issue? – Irina Mironova Apr 08 '21 at 10:07
  • @IrinaMironova, are you looking for a pdf or html output? Your update works fine for me, without any error. Nonetheless, there is no line break in the equation. – bttomio Apr 08 '21 at 19:15
  • I am looking for pdf output. – Irina Mironova Apr 08 '21 at 21:58
  • All right, thanks. Please feel free to edit my answer. Could you please confirm/upvote it? Thank you – bttomio Apr 09 '21 at 09:24
0

What happens when you use specific margins in PDF output?

---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
output: pdf_document
---
Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27