UPDATE (13th June 2012): RStudio now supports a range of mathjax delimtiers including single dollar signs and double dollar signs without latex
.
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.
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.
Also dollars that appear in r code blocks like this should not be altered.
```{r ...}
x <- Data$asdf
```
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$$
`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)$$
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .
Using perl
and a look-back, should do the trick:
perl -pe 's/\b(?<=\$)(\w+)\b /latex $1 /g' file.txt
Make the changes in-line with the -i
flag:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With