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 8251421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:13:54+00:00 2026-06-08T00:13:54+00:00

does anyone know of some good elisp macros for cleaning up LaTeX code? I

  • 0

does anyone know of some good elisp macros for cleaning up LaTeX code?

I do a lot of LaTeX editing of other peoples sources and I’d like to extend my set of clean up tools since not everyone organize their code in the manner I like it 😉

One in particular would be interesting, to run function X on a buffer and get all LaTeX environments (\begin{…} and \end{…} pairs) to sit on lines of their own, this helps readability of the code.

I could try this myself, but would like to hear suggestions as to a best practice for programming such a function, e.g. it should of course not introduce blank lines of its own.

suggestions?

Edit: For the archives, here are my current version based on the answer given (assumes the use of auctex). It more or less suits my needs at the moment. I added the y-or-n test just to be able to detect corner cases that I had not thought of.

(defun enviro-split ()
  "Find begin and end macros, and put them on their own line."
  (interactive)
  (save-excursion
(beginning-of-buffer)

;; loop over document looking for begin and end macros
(while (re-search-forward "\\\\\\(begin\\|end\\)" nil t)
  (catch 'continue 

    ; if the line is a pure comment, then goto next
    (if (TeX-in-commented-line)
    (throw 'continue nil)
      )
    ;; when you find one, back up to the beginning of the macro
    (search-backward "\\")

    ;; If it's not at the beginning of the line, add a newline
    (when (not (looking-back "^[ \t]*"))
      (if (y-or-n-p "newline?")
      (insert "\n")
    )
      )

    ;; move over the arguments, one or two pairs of matching braces
    (search-forward "{")        ; start of the argument
    (forward-char -1)
    (forward-sexp)          ; move over the argument
    (if (looking-at "[ \t]*{")  ; is there a second argument?
    (forward-sexp)
      )             ; move over it if so
    (if (looking-at "[ \t]*\\[")    ; is there a second argument?
    (forward-sexp)
      )             ; move over it if so
    (when (looking-at (concat "[ \t]*" (regexp-quote TeX-esc) "label"))
      (goto-char (match-end 0))
      (forward-sexp)
      )

    (if (looking-at (concat "[ \t]*%" ))
    (throw 'continue nil)
      ) 

    ;; If there is anything other than whitespace following the macro,
    ;; insert a newline
    (if (not (looking-at "\\s *$"))
    ;;(insert "\n")
    (if (y-or-n-p "newline (a)?")
        (insert "\n")
      )
      )
    ) ; end catch 'continue
  )
(LaTeX-fill-buffer 'left)
)
  )
  • 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-08T00:13:55+00:00Added an answer on June 8, 2026 at 12:13 am

    You could probably work up a single regexp and do a regexp replace for this. However, I find the logic of these manipulations becomes pretty hairy, particularly when you want to account for various edge-cases. In your example, you need to deal with some environments taking one argument, while others take two. I think it is easier to combine a series of simple regexps with basic text editing commands for this:

    (defun enviro-split ()
      "Find begin and end macros, and put them on their own line."
      (interactive)
      (save-excursion
        (beginning-of-buffer)
    
        ;; loop over document looking for begin and end macros
        (while (re-search-forward "\\\\\\(begin\\|end\\)" nil t)
    
          ;; when you find one, back up to the beginning of the macro
          (search-backward "\\")
    
          ;; If it's not at the beginning of the line, add a newline
          (when (not (looking-at "^"))
            (insert "\n"))
    
          ;; move over the arguments, one or two pairs of matching braces
          (search-forward "{")              ; start of the argument
          (forward-char -1)
          (forward-sexp)                    ; move over the argument
          (if (looking-at "\\s *{")         ; is there a second argument?
              (forward-sexp))               ; move over it if so
    
          ;; If there is anything other than whitespace following the macro,
          ;; insert a newline
          (if (not (looking-at "\\s *$"))
              (insert "\n")))))
    

    This approach has the advantage of using Emacs’ built-in functions for moving over sexps, which is much easier than coming up with your own regexp that can handle multiple, potentially nested, expressions inside braces.

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

Sidebar

Related Questions

does anyone know about some good sources about counting complexity of recursive algorithms? somehow
Does anyone know how to or have some code on counting the number of
Does anyone know a good and simple to use in production code R-tree implementation?
does anyone know some good alternatives for clucene as a search and indexing framework
Does anyone know of a good repository to get sample code for the BlackBerry?
Does anyone know of some good tutorials that explain how to use the JQuery
Does anyone know some good easy to learn openCV c/c++ tutorial you can recommend?
does anyone know some good open source editors / IDES that are using SynEdit?
Does anyone know of some good resources related to setting up heirarchical user account
Does anyone know of some good reference implementations of common design patterns in Ada?

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.