5
votes

The following code works fine on linux:

---
title: "LaTeX test"
author: "Ignacio"
output: html_document
---


## Latex

$$ 
\begin{aligned}

y_j \sim N(\theta_j , \sigma_j^2) \\

\sigma_j = \nu/\sqrt{n_j}   \\

\theta = \mu +\tau \times \eta \\

\eta \sim N(0,1) \\

\mu + \tau\times\eta = \theta \sim N(\mu , \tau^2)

\end{aligned}
$$

On Windows, rstudio renders the equations correctly:

enter image description here

But, when I Knit the file and open the HTML with chrome or ie the latex is not rendered correctly:

enter image description here

Is there a way to fix this?

1
I'm confused by the two screenshots, because they don't seem to match their descriptions (when you said it worked on Windows, the screenshot showed that it didn't work). In general, I'd recommend that you avoid unnecessary empty lines in equations, and it is not necessary to wrap the equation environment in $$.Yihui Xie
The first screenshot shows the html that I get when I click knit using Rstudio on a windows machine. The second screenshot shows the preview that I get on Rstudio before pressing knit. I tried removing $$ and `\` but that did not make a difference. Thanks for the help @YihuiXieIgnacio
Yihui has given you the right answer, the problem lies with empty lines. With empty lines I can reproduce your error, without it worksCedric
The description of your first image says "On Windows, rstudio renders the equations correctly", but the first image shows a broken equation. That is why I was confused. Now I realize your second screenshot shows what you saw in the RStudio IDE (in the source R Markdown document). Anyway, I'm glad to know that removing the extra empty lines worked.Yihui Xie
You are right, I just made a change to make fix the question.Ignacio

1 Answers

10
votes

In case someone else is encountering this problem, @YihuiXie was right. Removing the empty lines solves the problem:

---
title: "LaTeX test"
author: "Ignacio"
output: html_document
---


## Latex

$$
\begin{aligned}
y_j \sim N(\theta_j , \sigma_j^2) \\
\sigma_j = \nu/\sqrt{n_j}   \\
\theta = \mu +\tau \times \eta \\
\eta \sim N(0,1) \\
\mu + \tau\times\eta = \theta \sim N(\mu , \tau^2)
\end{aligned}
$$