6

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.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
Christoph
  • 6,841
  • 4
  • 37
  • 89

2 Answers2

7

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 ...
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
1

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
---
This_is_it
  • 11
  • 1