0

I want to write a script in order to correct spaces inside section command in a LaTeX code, that's to say

\section{Lorem ipsum dolor
  sit amet consectetuer adipiscing
  elit}

should be

\section{Lorem ipsum dolor sit amet consectetuer adipiscing elit}

I do this with the following script:

(defun headings-on-same-line ()
  "Delete new lines inside section commands."
  (interactive)

  (goto-char (point-min))

  (while (search-forward "\\section" nil t)
    (setq sec1 (make-marker))
    (set-marker sec1 (point))
    (set-marker-insertion-type sec1 t)
    (forward-sexp)
    (setq sec2 (make-marker))
    (set-marker sec2 (point))
    (set-marker-insertion-type sec2 t)

    (perform-replace "\\(?:[ ]*\\)?
\\(?:[ ]*\\)?"
             " " t t nil 1 nil sec1 sec2)

    ))

I have problems with unbalanced parentheses like this:

\section{Lorem ipsum $(a\rangle$
   dolor sit amet
   consectetuer adipiscing elit}

I have thought to use an if-else construct, so I have rewritten the code above like this:

(defun headings-on-same-line-unbalanced ()
  "Delete new lines inside section commands."
  (interactive)

  (goto-char (point-min))

  (while (search-forward "\\section" nil t)
    (setq sec1 (make-marker))
    (set-marker sec1 (point))
    (set-marker-insertion-type sec1 t)
    (if (forward-sexp)
    (progn
      (setq sec2 (make-marker))
      (set-marker sec2 (point))
      (set-marker-insertion-type sec2 t)

      (perform-replace "\\(?:[ ]*\\)?
\\(?:[ ]*\\)?"
               " " t t nil 1 nil sec1 sec2)
      )
  (progn
    (insert "Warning"))
  )))

but it does not work. How can I fix it?

0

Basically use (skip-chars-forward "^}") Needs a helper-function, to check if it's a escaped }:

(defun ar-backslashed-p ()
  (save-excursion
    (and (eq (char-before) ?\\)
     (< 0 (% (abs (skip-chars-backward (regexp-quote (char-to-string ?\\)) (line-beginning-position))) 2)))))

(defun no-newline-in-section ()
  (interactive "*")
  (let (beg end)
    (while (search-forward "\\section" nil t)
      (when (and (eq (char-after) ?{)
         (not (ar-backslashed-p)))
    (setq beg (point))
    (while (and (or (< 0 (abs (skip-chars-forward "^}")))(eq (char-after) ?}))
            (eq (char-after) ?})
            (or
             ;; check for nested braces
             (< 1 (nth 0 (parse-partial-sexp beg (point))))
            (ar-backslashed-p))
            (progn (forward-char 1) t)))
    (when (eq (char-after) ?})
      (save-restriction
        (narrow-to-region beg (point))
        (goto-char (point-min))
        (while (search-forward "\n" nil t 1)
          (replace-match ""))))))))
  • Thank you a lot! Sometimes I have sections with `{...}' inside their arguments. Do you know how to do in this case? – Onner Irotsab Aug 15 at 14:59
  • @OnnerIrotsab You mean nested braces? – Andreas Röhler Aug 15 at 16:40
  • Yes, something like this: \section{Lorem $x^{4}$ ipsum~(\ref{eq}) dolor $x_{\text{1-loop}}$}. – Onner Irotsab Aug 15 at 17:16
  • @OnnerIrotsab Should handle nested braces now. – Andreas Röhler Aug 15 at 17:58
  • Ok, if I combine this new code with the previous one, all works fine, thank you! – Onner Irotsab Aug 15 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.