Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use eqnarray in R markdown for both html and pdf output?

Tags:

r

r-markdown

I can't work out how to use eqnarray to create equations in R markdown such that I can create both html and pdf output. This R markdown works for output_format='html_document' but not output_format='pdf_document'

My LaTeX equations
$$
\begin{eqnarray}
A &=& x \\
B &=& y
\end{eqnarray}
$$

For pdf output it gives this error:

! Missing \endgroup inserted.
<inserted text> 
                \endgroup 
l.81 \begin{eqnarray}

pandoc: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43

If I remove the $$ then this R markdown works for pdf output but the equation is missing from the html output.

My LaTeX equations
\begin{eqnarray}
A &=& x \\
B &=& y
\end{eqnarray}

Am I missing something here? Is there some way to generate both pdf and html output from the same document?

like image 629
Epimetheus Avatar asked Jul 01 '15 12:07

Epimetheus


People also ask

What is an advantage of exporting as an HTML file or a PDF in R Markdown?

HTML documents can be attached to emails and viewed using any browser, even with no internet access (as long as it is a self-contained HTML document, which R Markdown exports usually are). PDF looks most professional when printed.

What are the three types of sections in an R Markdown document select three options?

The following sections dive into the three components of an R Markdown document in more details: the markdown text, the code chunks, and the YAML header.

How do you make a superscript in R Markdown?

To indicate a superscript, use a single caret character ^ . Note: this can be confusing, because the R Markdown language delimits superscripts with two carets. In LaTeX equations, a single caret indicates the superscript. If the subscript or superscript has just one character, there is no need to delimit with braces.


2 Answers

I ran into the same problem and found that while eqnarray has the problems mentioned above, in the context of R markdown and knitr, the aligned environment works for both PDF and HTML output. Try this:

My LaTeX equations
\begin{aligned}
A &= x \\
B &= y
\end{aligned}

Note that there is only one & symbol here and that it's the alignment operator in the context for the aligned environment. aligned works a little differently to eqnarray in that it will align at more than just one tab stop, so to speak.

like image 108
user3808113 Avatar answered Sep 21 '22 11:09

user3808113


For Word output, this works:

\[
\begin{aligned}
A &= x \\
B &= y
\end{aligned}
\]
like image 40
Richard Layton Avatar answered Sep 21 '22 11:09

Richard Layton