1

I have this LaTeX code:

Lorem $math$ ipsum $math$dolor sit consecteruer adipscing elit,
sed do eiusmod tempor incidunt ut labore et dolore magna aliqua.
$math$Ut $math$ enim ad minim veniam$math$, quis nostrum exercitationem
ullamco laboriosam, $math$nisi ut aliquid ex ea commodi consequatur$math$.

I want to split math from following and preceeding text, that's to say, I want to replace

$math$word

word$math$

with

$math$ word

word $math$

So, I have tried with the following:

M-x query-replace-regexp RET \$\([^$]+?\)\$\\([^\S]\) RET $\1$ \2

and

M-x query-replace-regexp RET \([^S]\)\$\([^$]+?\) RET \1 $\2$

The problem is that these regularx expressions match too much than it is necessary.

Indeed, first replacement matches

"$math$i"

"$math$d"

"$math$U"

"$math$ "

"$math$,"

"$math$n"

"$math$."

and only the first, second, third and sixth matches are necessary.

The second replacement matches:

" $math$"

" $math$"

"^J $match$"

" $math$"

"m$math$"

" $math$"

"r$math$"

and only the fifth and seventh matches are necessary.

How can I match only $math$word and word$math$?

Onner Irotsab
  • 431
  • 2
  • 9
  • `isearch-forward-regexp` will help designing your search regex. `\s-` will match a single whitespace character. `\S-` will match a single non-whitespace character. This is different from the perl-type regexes you may be used to. https://www.emacswiki.org/emacs/RegularExpression is an excellent page on Emacs-flavored regexes – nega Aug 21 '19 at 14:58
  • `re-builder`/`regexp-builder` (they're aliases) will be helpful too. https://www.masteringemacs.org/article/re-builder-interactive-regexp-builder is a nice explainer. Note the magic `C-c C-w` command! – nega Aug 21 '19 at 15:16
  • @nega I have tried with `\([^ ]?\)`, `\([^\s-]?\)` and `\([^\S-]?\)`, but they match too much cases, many of them are not necessary. `isearch-forward-regexp` is very useful and documentation on "masteringemacs.org" too! – Onner Irotsab Aug 22 '19 at 07:46
  • This is not a regexp solution. But if you are asking because orgmode does not render $math$word, I wanted to point out that \\(math\\)word is rendered without issues. :) – Quarky Quanta Aug 24 '19 at 08:58

0 Answers0