7

更新(2012 年 6 月 13 日): RStudio 现在支持一系列 mathjax 分隔符,包括不带latex.


在 0.96中, RStudio 将其 Mathjax 语法$<equation>$to更改为$latex <equation>$用于内联方程,从$$<equation>$$to 更改$$latex <equation>$$为用于显示方程。

因此,总而言之:

修改后的语法将latex限定符添加到 $ 或 $$ 等式开始分隔符。

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

像这样出现在 r 代码块中的美元也不应该被更改。

```{r ...}
x <- Data$asdf
```

问题

  • 什么是一个好的简单命令行程序,可能使用 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$$

变身后变成

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

工作示例 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)$$

应该成为

`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)$$
4

2 回答 2

4

使用perl并回顾一下,应该可以解决问题:

perl -pe 's/\b(?<=\$)(\w+)\b /latex $1 /g' file.txt

使更改与-i标志一致:

perl -pe -i 's/\b(?<=\$)(\w+)\b /latex $1 /g' file.txt

编辑:

试试这个怪物:

perl -pe 's/\b(?<=\$)(\w+)\b(\$?)([ =])/latex $1$2$3/g;' -pe 's/(?<=\$)(\\\w+)/latex $1/g' file.txt

高温高压

于 2012-05-29T05:37:21.543 回答
2

这可能对您有用:

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

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

于 2012-05-30T19:37:16.997 回答