R Markdown similar feature to “newcommand” in LaTex?

我们两清 提交于 2020-01-10 02:35:28

问题


Does R Markdown have a similar construct to LaTex's "newcommand"? I would like to be able to define things like \var to be \mathrm{Var} to avoid the extra typing in math mode. If not, what do people do to reduce repetition in typesetting equations in markdown?


回答1:


Use \newcommand{\var}{\mathrm{Var}} exactly like you would in LaTeX:

---
title: "Untitled"
author: "An Author"
date: "January 15, 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\newcommand{\var}{\mathrm{Var}}

## R Markdown

This is an R Markdown document. $\var+2$ Markdown is a simple formatting syntax for 
authoring HTML, PDF, and MS Word documents. For more details on using R Markdown 
see <http://rmarkdown.rstudio.com>.

Note that in order for it to be processed correctly in the output, you'll have to use $...$.




回答2:


To get around the requirement of \DeclareMathOperator needing to be in the preamble, use \operatorname:

\newcommand{\Var}{\operatorname{Var}}

$\Var(X)$

(\operatorname handles spacing better than \mathrm)

To use \newcommand properly in HTML output, your LaTeX should be in-line with single $ or in double $$. This applies to environments like \begin{align*} too.

---
title: "Test"
author: "qwr"
date: "January 22, 2019"
output: html_document
---

\newcommand{\Var}{\operatorname{Var}}

$\Var(X)$

$$
\begin{align*}
\Var[Y] &= x \\
&= 3
\end{align*}
$$



回答3:


I'm using bookdown and need to have something that works consistently across pdf, html, and docx output. None of the above solutions worked for my case. Here is the hack I settled on:

preamble.tex

\usepackage{amsthm}
\DeclareMathOperator*{\argmin}{argmin}
\newcommand{\var}{\mathrm{Var}}

YAML Header:

--- 
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output: 
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
    toc: no
  bookdown::word_document2:
    reference_docx: template.docx
  bookdown::gitbook:
    split_by: none
documentclass: article
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
---

<!--- For HTML Only --->
`r if (!knitr:::is_latex_output()) '
$\\DeclareMathOperator*{\\argmin}{argmin}$
$\\newcommand{\\var}{\\mathrm{Var}}$
'`

<!--- For DOCX Only --->
`r if (!knitr:::is_latex_output() & !knitr:::is_html_output()) '
\\DeclareMathOperator*{\\argmin}{argmin}
\\newcommand{\\var}{\\mathrm{Var}}
'`
# Prerequisites

This is a _sample_ book written in **Markdown**.



回答4:


I had issues with the above solution when outputting as a beamer presentation, particularly when using the equation mode ($$.$$ rather than $.$). Putting the new commands in a separate file fixed the issue for me.

---
title: Title
author: Author
date: "8/22/2018"
output:
  beamer_presentation:
    includes:
      in_header: preamble.tex
---

Where preamble.tex contains your user defined command(s)

\newcommand{\var}{\mathrm{Var}}

Then you can use the command both inline ($\var$) and in equation mode ($$\var$$)

You can also put other stuff in preamble.tex like frame numbering, etc.



来源:https://stackoverflow.com/questions/41655383/r-markdown-similar-feature-to-newcommand-in-latex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!