命令行程序更新R Markdown代码以使用`$ latex`分隔符

Jer*_*lim 7 awk r sed mathjax rstudio

更新(2012年6月13日): RStudio现在支持一系列mathjax分隔符,包括单个美元符号和双美元符号latex.


在0.96 RStudio改变其Mathjax语法$<equation>$$latex <equation>$用于在线方程和从$$<equation>$$$$latex <equation>$$用于显示的方程.

因此,总结如下:

修订后的语法latex为$或$$方程开始分隔符添加了限定符.

我有一些使用原始$分隔符的现有脚本,我想更新它们以使用新的$latex分隔符.我以为sed或awk可能是合适的.

此类r代码块中出现的美元也不应改变.

```{r ...}
x <- Data$asdf
```
Run Code Online (Sandbox Code Playgroud)

  • 什么是一个很好的简单命令行程序可能使用sed或awk来更新我的R Markdown代码以在R Studio中使用更新的mathjax分隔符?

工作示例1

原文:

$y = a + b x$ is the formula.
This is some text, and here is a displayed formula
$$y = a+ bx\\
x = 23$$

```{r random_block}
y <- Data$asdf
```

and some more text     
$$y = a+ bx\\
x = 23$$
Run Code Online (Sandbox Code Playgroud)

转型后变成了

$latex y = a + b x$ is the formula.
This is some text, and here is a displayed formula
$$latex y = a+ bx\\
x = 23$$

```{r random_block}
y <- Data$asdf
```

and some more text     
$$latex y = a+ bx\\
x = 23$$
Run Code Online (Sandbox Code Playgroud)

工作示例2

`r opts_chunk$set(cache=TRUE)`
<!-- some comment -->

Some text

<!-- more -->
Observed data are $y_i$ where $i=1, \ldots, I$.  
$$y_i \sim N(\mu, \sigma^2)$$

Some text $\sigma^2$ blah blah $\tau$. 

$$\tau = \frac{1}{\sigma^2}$$

blah blah $\mu$ and $\tau$

$$\mu \sim N(0, 0.001)$$
$$\tau \sim \Gamma(0.001, 0.001)$$
Run Code Online (Sandbox Code Playgroud)

应该成为

`r opts_chunk$set(cache=TRUE)`
<!-- some comment -->

Some text

<!-- more -->
Observed data are $latex y_i$ where $latex i=1, \ldots, I$.  
$$latex y_i \sim N(\mu, \sigma^2)$$

Some text $latex \sigma^2$ blah blah $latex \tau$. 

$$latex \tau = \frac{1}{\sigma^2}$$

blah blah $latex \mu$ and $latex \tau$

$$latex \mu \sim N(0, 0.001)$$
$$latex \tau \sim \Gamma(0.001, 0.001)$$
Run Code Online (Sandbox Code Playgroud)

pot*_*ong 2

这可能对你有用:

sed '/^```{r/,/^```$/b;/^`r/b;:a;/\\\\$/{$!{N;ba}};s/\(\$\$\)\([^$]*\(\$[^$]*\)*\$\$\)\|\(\$\)\([^$]*\$\)/\1\4latex \2\5/g' file
Run Code Online (Sandbox Code Playgroud)

注意:该r codeblock代码可能需要扩展/更改,因为从示例代码来看,它的构成并不明显。