How do I write this complicated formula in R Markdown? - r

I have been trying for days to figure out how to write this formula in R markdown...
...but I didn't find anything on the web.
Can you help me please?
Thanks.

You can use something like that
\documentclass{article}
\begin{document}
\[
x_1 = \left(\begin{array}{c}x_{11}\\\vdots\\x_{1p}\end{array}\right),
\cdots,
x_n = \left(\begin{array}{c}x_{n1}\\\vdots\\x_{np}\end{array}\right)
\]
\end{document}
The array is used to stack elements and the \left( \right) pair provides the enclosing parenthesis.
Does not require any package and can probably be pasted as is in R.

Related

Newtonian notation for derivates on latex

I am using Notion for my studies: so I have to use Latex to represent the equations or others math stuffs. I noticed that I can use the Newtonian notation on Latex with the command \dot or \ddot (first derivative and second derivative) but I cannot represent the third with \dddot or with something else. Is there a way to represent that?
As in this other answer, three dots (\dddot) and four dots (\ddddot) in math mode require the package amsmath.
So you have either
\documentclass{article}
\begin{document}
\[\dot{x}\]
\[\ddot{x}\]
\end{document}
or
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[\dot{x}\]
\[\ddot{x}\]
\[\dddot{x}\]
\[\ddddot{x}\]
\end{document}

Is there a way to use latex expression of chemarr for `gitbook` format of bookdown package?

I would like to use latex expression of chemarr for gitbook format of bookdown package.
\begin{equation}
[C] + [R]
\xrightleftharpoons[k_{-1}]{k_1}
[CR] + [C]
\xrightleftharpoons[k_{-2}]{k_2}
[C2R]
(\#eq:multiplebinding)
\end{equation}
For PDF format, there is no problem of displaying the equation.
On the other hand, for gitbook format there is an error message.
It's mainly because I cannot define the following yaml header for gitbook format.
header-includes:
- \usepackage{chemarr}
Is there a way to use latex expression of chemarr for gitbook format of bookdown package? If it's impossible, is there a way to use include_graphics function and add equation numbering (say, 19.16 in this example)?
Yihui Xie already gave a good hint to the solution:
Put this code into your document (where you want the equation to appear):
$$
\require{mhchem}
\begin{equation}
[C] + [R]
\xrightleftharpoons[k_{-1}]{k_1}
[CR] + [C]
\xrightleftharpoons[k_{-2}]{k_2}
[C2R]
(\#eq:multiplebinding)
\end{equation}
$$
Needs to use the arrow of mhchem instead of chemarr since only the first is part of MathJax. But I think it should anyway be basically the same.
This method should work for all Latex commands supported by MathJax (http://docs.mathjax.org/en/latest/input/tex/macros/index.html).

"Erroneous nesting of equation structures" in using "\begin{align}" in a multi-line equation in rmarkdown to knit+pandoc pdf

I am writing some multi-line equations in R Markdown - LaTeX, using auto-numbering and \begin{align}. Here's a working the example:
---
title: "test"
output: html_document
---
(#eq01) $$
\begin{align}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{align}
$$
This works great when the output is html_document. Here's the result:
But when I change the output document to pdf:
output: pdf_document
I get the following error (I am using RStudio latest Version 0.98.1056):
I've been trying to read the documentation as suggested in the error message, but I do not seem to get a handle on it. I've checked Stack Overflow and Google and although there are some related posts/questions (for example here, here, here), none of them solve the problem (or apply to my problem).
I've also tried to tweak everything. The most evident solution would be to get rid of the \begin{align} environment,
(#eq01) $$
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
$$
but it does not work for two reasons. First, the html version does not work as nicely because the auto-numbering does not appear centered in the multi-line equation, but rather on the first line (and I don't like it like that).
Second, although the pdf version in this case does compile and produce the pdf, it does not recognize that it is a multi-line equation (it's like it does not recognize the new line command \).
Any ideas are really appreciated. I've been struggling with this for a while and I cannot find a solution. I kinda love R Markdown because it really integrates the analysis with writing and communicating in a single tool (rather than using many different tools going back and forth). However, it seems there is still a long way to go before we can write one single source file and that it renders appropriately in several different output formats.
I was receiving the same error when trying to send an aligned block to PDF. Try changing the following:
$$
\begin{align}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{align}
$$
to the following:
$$
\begin{aligned}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{aligned}
$$
\begin{align} is a self-contained math environment, whereas \begin{aligned} needs to be placed inside an existing math environment. Since Rmd delineates math sections with $$...$$, it seems like \begin{align} was trying to start a second math environment within the first and causing problems.

Is there a way to prevent line break in the HTML when results='hide' and echo=FALSE?

In R, using knitr, is there a way to prevent line breaks in the HTML when results='hide' and echo=FALSE?
In this case:
First I do this,
```{r results='hide', echo=FALSE}
x=4;x
```
then I do that.
I get:
First I do this,
then I do that.
with both a break and an extra line between.
I'd like to get:
First I do this, then I do that.
instead.
Generally speaking, I'd like for code chunks to not insert new lines so that markdown is free to eat the one after the first line of text.
Thanks,
I assume you're creating an HTML document from an R Markdown document. In that case, you can use the inline R code capability offered by knitr by using the ` characters starting with the letter r.
Example:
In your R Markdown, write:
First I do this,`r x=4` then I do that. I can call x by doing `r x`.
And as output, you get:
First I do this, then I do that. I can call x by doing 4.
Note that in my example, I evaluated the variable x, but if you do not want to evaluate it, you do not have to. The variable x should still be assigned a value of 4 from the
`r x=4`
part of the R Markdown.
This is Inline R Code, and is documented here under the section "Inline R Code".
EDIT:
Note that Inline R Code has properties that are analogous to "echo=FALSE". And if you want to hide the results from inline R code, you can use base R functions to hide the output. See this question.
Try something like:
``` {r , results="asis", echo=F, eval=T}
if(showMe){
cat("printed")
} else {
cat("<!-- no break line -->")
}
```

How to place a character below a function in Latex?

Is it possible to place a character or a formula below an other part of a larger formula in Latex?
foo f(x) = ...
x
In case that this example is not clear. I'd like to make one of my custom functions - just defined as \text{foo} in a math environment - look like one of the built-in functions like sum, min or max which accept parameters that are placed above or below the function symbol.
Simply using \text{foo}_x does not work.
The function you're looking for is \underset provided by the amsmath package. Here's an example:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\underset{below}{above}$
\end{document}
Output:
You can declare the foo as a math operator. Then use it in any equation environment. See below
\DeclareMathOperator*{\foo}{foo} %in preamble
\begin{align}
\foo_x f(x) = ...
\end{align}
Following the line of the answer by #hanoosh, if you need you can even do:
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator*{\foo}{foo}
\begin{document}
\begin{displaymath}
\foo_{x\atop y} f(x,y)
\end{displaymath}
\end{document}
Only \DeclareMathOperator* is powered by package amsmath while, for example,
\documentclass{article}
\begin{document}
\begin{displaymath}
\sum_{x\atop y} f(x,y)
\end{displaymath}
\end{document}
compiles not needing any AMS package.

Resources