Here’s what I have:
{a} {b} over {c} {d}
{a} {{b} over {c}} {d}
(I have some text, and it has such expressions) And I want to convert both to:
{a} \frac{b}{c} {d}
Here’s my best bet:
(goto-char (point-min))
(while (search-forward-regexp "{+?\\(.+?\\)} +over +{\\(.+?\\)}+?" nil t)
(replace-match (concat "\\\\" "frac{\\1}{\\2}") t nil))
But it doesn’t work. I get:
\frac{a} {b}{c} {d}
\frac{a} {{b}{c}} {d}
respectively.
The problem is — my regexp captures as much as possible on the left. While I need it to capture as little as possible.
Edit:
The accepted answer doesn’t work for more complex cases (which I also need):
{d^{2} q} over {d t^{2} }
{{d^{2} q} over {d t^{2} }}
(because of nesting)
Here’s a working version of parser (the interesting part of the code start from while):
(defun over2frac ()
(interactive)
(let (start end
(case-fold-search nil)
lStart lEnd lStr
rStart rEnd rStr)
(if (use-region-p)
(progn (setq start (region-beginning)) ;; then
(setq end (region-end)))
(progn (setq start (line-beginning-position)) ;; else
(setq end (line-end-position))))
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward-regexp "{\\(.+\\)}\s*over\s*{\\(.+?\\)}" nil t)
;; r:
(goto-char (match-beginning 2))
(backward-char 1)
(setq rStart (point))
(forward-sexp)
(setq rEnd (point))
(setq rStr (buffer-substring-no-properties rStart rEnd))
;; l:
(goto-char (match-end 1))
(forward-char)
(setq lEnd (point))
(backward-sexp)
(setq lStart (point))
(setq lStr (buffer-substring-no-properties lStart lEnd))
(delete-region lStart rEnd)
(insert "\\" "frac" lStr rStr)
))))
Since it uses forward-sexp and backward-sexp — and doesn’t define syntax-table — it only works in appropriate modes (such as rst-mode and text-mode — but not emacs-lisp-mode. For the above complex examples it gives:
\frac{d^{2} q}{d t^{2} }
{\frac{d^{2} q}{d t^{2} }}
Edit 2:
(defun backslash-func--args-on-both-sides (Find Replace)
(goto-char (point-min))
(while (search-forward-regexp (concat "\\([^{]+\\)}\s*" Find "\s*{\\([^}]+\\)") nil t)
;; r:
(goto-char (match-beginning 2))
(backward-char 1)
(setq rStart (point))
(forward-sexp)
(setq rEnd (point))
(setq rStr (buffer-substring-no-properties rStart rEnd))
;; l:
(goto-char (match-end 1))
(forward-char)
(setq lEnd (point))
(backward-sexp)
(setq lStart (point))
(setq lStr (buffer-substring-no-properties lStart lEnd))
;; s:
(goto-char lStart)
(if (looking-back "{")
(setq lStart (1- (point))))
(goto-char rEnd)
(if (looking-at "}")
(setq rEnd (1+ (point))))
(delete-region lStart rEnd)
(insert "\\" Replace lStr rStr))
(goto-char (point-min))
(while (search-forward-regexp (concat "\\([0-9a-zA-Z_^]+\\)\s*" Find "\s*\\([0-9a-zA-Z_^]+\\)") nil t) (replace-match (concat "\\\\" Replace "{\\1}{\\2}") t nil))
)
This converts both things:
{d^{2} q} over {d t^{2} }
{{d^{2} q} over {d t^{2} }}
to
\frac{d^{2} q}{d t^{2} }
I don’t use let here because all the vars are local already — and the defun itself is in the other defun. Full code.
Use this expression: http://regex101.com/r/kY8jY5
/{+([^{}]+)}\s*over\s*{([^{}]+)}+/gThe expression should be compatible with emacs.
Replace with:
\frac{\1}{\2}Note: I am unsure if you use
\1or$1in emacs.