Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Latex' \newcommand in Rmd?

I would like to make the following work

---
title: "Untitled"
author: "SQC"
date: "21 September 2018"
output: html_document
---

\newcommand{\short}{AreallylongwordIhavetotypefrequently}

# My Test
I would like to write \short which does not work, $\short$ however is close... 
Snippets do not work in Rmd plain text (= Rstudio's "Shift", see link below).

But I could not find a solution. It would be great if there is something around! The following links are helpful, but didn't suggest a solution: pandoc doc, \newcommand in Rmd formula and RStudio snippets.

like image 672
Christoph Avatar asked Sep 21 '18 07:09

Christoph


People also ask

Can I use LaTeX code in R markdown?

By default, Pandoc will preserve raw LaTeX code in Markdown documents when converting the document to LaTeX, so you can use LaTeX commands or environments in Markdown.

How do you write square root in R markdown?

We indicate a square root using the \sqrt operator.

How do I add to preamble Rmarkdown?

If you want to add anything to the preamble, you have to use the includes option of pdf_document . This option has three sub-options: in_header , before_body , and after_body . Each of them takes one or multiple file paths. The file(s) specified in in_header will be added to the preamble.


2 Answers

How about using R instead:

---
title: "Untitled"
author: "SQC"
date: "21 September 2018"
output: html_document
---

```{r, include = FALSE}
short <- "AreallylongwordIhavetotypefrequently"
```

# My Test
I would like to write `r short` instead ...
like image 200
Ralf Stubner Avatar answered Oct 03 '22 22:10

Ralf Stubner


If you still need to define a \newcommand using LaTeX in Rmarkdown, you can do so by e.g.

Some text with the following equation:

\begin{equation} \newcommand{\matr}[1]{\mathbf{#1}}
\matr{Y} =
\begin{pmatrix}
\matr{y_1} \\
\matr{y_2} \\
\end{pmatrix}
\end{equation}

```{r}
y1 + y2
```

Note, the bm package must be loaded for this to work, e.g., by using this in the YAML header:

---
title: "My R Report" 
author: "Me" 
date: "2020-01-31" 
header-includes: 
  - \usepackage{bm} 
output: 
  pdf_document: 
    toc: true 
    number_sections: true
---
like image 24
This_is_it Avatar answered Oct 03 '22 21:10

This_is_it