我该如何在Rmd中使用Latex的\newcommand命令?

6
我希望你能帮我完成如下工作。
---
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文档\newcommand在Rmd公式中的应用RStudio代码片段
2个回答

7
如何使用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 ...

太棒了 - 如此简单!! - Christoph
也许你知道这个问题的答案 在这里?;-) - Christoph
@Christoph 抱歉,我不知道如何解决这个问题。 - Ralf Stubner

1
如果你仍需要在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
---

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接