Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:22:01+00:00 2026-06-10T23:22:01+00:00

Here’s what I have: {a} {b} over {c} {d} {a} {{b} over {c}} {d}

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:22:03+00:00Added an answer on June 10, 2026 at 11:22 pm

    Use this expression: http://regex101.com/r/kY8jY5

    /{+([^{}]+)}\s*over\s*{([^{}]+)}+/g

    The expression should be compatible with emacs.

    Replace with:

    \frac{\1}{\2}

    Note: I am unsure if you use \1 or $1 in emacs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here are some facts about my app followed by a question My app has
Here are the tables I have: Table A which has entries with item and
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
I have just tried to save a simple *.rtf file with some websites and
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example: I have the generic type called Account. I wish to
Here is a simple timepicker to jQuery UI's datepicker <script type=text/javascript> /* <![CDATA[ */

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.