如何在Rmd中使用Latex的\ newcommand?

时间:2018-09-21 07:22:55

标签: latex r-markdown

我想做以下工作

---
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).

但是我找不到解决方案。如果周围有东西,那就太好了! 以下链接是有用的,但没有提出解决方案:pandoc doc\newcommand in Rmd formulaRStudio snippets

2 个答案:

答案 0 :(得分:4)

如何改用R:

---
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 ...

答案 1 :(得分:0)

如果您仍然需要在Rmarkdown中使用LaTeX定义\newcommand,则可以例如通过

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
```

请注意,必须加载bm软件包才能使其正常运行,例如,通过在YAML标头中使用以下软件包:

---
title: "My R Report" 
author: "Me" 
date: "2020-01-31" 
header-includes: 
  - \usepackage{bm} 
output: 
  pdf_document: 
    toc: true 
    number_sections: true
---