3

This is related to this Emacs.SE question.

I use the code from the answer:

(setq LaTeX-begin-regexp "\\(?:begin\\|if@\\)\\b")
(setq LaTeX-end-regexp "\\(?:end\\|else\\|fi\\)\\b")
(defun LaTeX-indent-level-count ()
  "Count indentation change caused by all \\left, \\right, \\begin, and
\\end commands in the current line."
  (save-excursion
    (save-restriction
      (let ((count 0))
        (narrow-to-region (point)
                          (save-excursion
                            (re-search-forward
                             (concat "[^" TeX-esc "]"
                                     "\\(" LaTeX-indent-comment-start-regexp
                                     "\\)\\|\n\\|\\'"))
                            (backward-char)
                            (point)))
        (while (search-forward TeX-esc nil t)
          (cond
            ((looking-at "left\\b")
             (setq count (+ count LaTeX-left-right-indent-level)))
            ((looking-at "right\\b")
             (setq count (- count LaTeX-left-right-indent-level)))
            ((looking-at LaTeX-begin-regexp)
             (setq count (+ count LaTeX-indent-level)))
            ((looking-at "else\\b"))
            ((looking-at LaTeX-end-regexp)
             (setq count (- count LaTeX-indent-level)))
            ((looking-at (regexp-quote TeX-esc))
             (forward-char 1))))
        count))))

But instead of getting this indentation:

\ifx\condition
  \dosomething
\else
  \dosomethingelse
\fi

I do get

\ifx\condition
  \dosomething
  \else
  \dosomethingelse
\fi

Hoe to get the \else in line with \if and \fi?

1
  • The LaTeX-begin-regexp seems to take into account two alternatives: begin and if@. What happens If you add the third alternative to the expression, such as "\\(?:begin\\|if@\\|ifx\\)\\b?
    – Celdor
    Commented Oct 25, 2023 at 8:49

1 Answer 1

1

It needs more tweaking than the code in the question as you can see in this commit. At any rate, this feature is available in AUCTeX OOTB and if you mark this example:

\ifx\condition
\dosomething
\else
\dosomethingelse
\fi

and run indent-region, you get:

\ifx\condition
  \dosomething
\else
  \dosomethingelse
\fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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