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

时间:2022-05-04 06:19:00

UPDATE (13th June 2012): RStudio now supports a range of mathjax delimtiers including single dollar signs and double dollar signs without latex.

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


In 0.96 RStudio changed its Mathjax syntax from $<equation>$ to $latex <equation>$ for inline equations and from $$<equation>$$ to $$latex <equation>$$ for displayed equations.

在0.96中,RStudio将其Mathjax语法从$ $更改为$ latex $用于内联方程式,并从$$ $$更改为$$ latex $$用于显示的方程式。

Thus, in summary:

因此,总结如下:

The revised syntax adds a latex qualifier to the $ or $$ equation begin delimiter.

修订后的语法将乳胶限定符添加到$或$$方程开始分隔符。

I have some existing scripts that use the original $ delimiter and I would like to update them to use the new $latex delimiter. I was thinking that sed or awk might be suitable.

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

Also dollars that appear in r code blocks like this should not be altered.

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

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

Question

  • What would be a good simple command-line program perhaps using sed or awk to update my R Markdown code to use the newer mathjax delimiter in R Studio?
  • 什么是一个很好的简单命令行程序可能使用sed或awk来更新我的R Markdown代码以在R Studio中使用更新的mathjax分隔符?

Working example 1

Original text:

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

after transformation becomes

转型后变成了

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

Working example 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)$$

should become

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

2 个解决方案

#1


2  

This might work for you:

这可能对你有用:

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

N.B. The r codeblock code may need to be extended/altered as from the example code it is not obvious what it constitutes.

注: r代码块代码可能需要从示例代码扩展/改变,它不明显是什么构成。

#2


4  

Using perl and a look-back, should do the trick:

使用perl和回顾,应该做的诀窍:

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

Make the changes in-line with the -i flag:

使用-i标志进行更改:

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

EDIT:

Try this monster:

试试这个怪物:

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

HTH

#1


2  

This might work for you:

这可能对你有用:

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

N.B. The r codeblock code may need to be extended/altered as from the example code it is not obvious what it constitutes.

注: r代码块代码可能需要从示例代码扩展/改变,它不明显是什么构成。

#2


4  

Using perl and a look-back, should do the trick:

使用perl和回顾,应该做的诀窍:

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

Make the changes in-line with the -i flag:

使用-i标志进行更改:

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

EDIT:

Try this monster:

试试这个怪物:

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

HTH