3

Is it possible to encourage AUCTeX to indent specific environment contents deeper than other environments? My specific case is getting the following indentation for chorus:

\begin{song}
\begin{verse}
  Normal 2-spaces indentation
\end{verse}
\begin{chorus}
      Would be great to have it indented
      deeper.
\end{chorus}
\end{song}

I tried playing with LaTeX-indent-environment-list. It works well to avoid indenation where inappropriate, the following

(add-to-list 'LaTeX-indent-environment-list '("song"  (lambda() 0))) 

nicely disabled indentation inside song, but similar trick:.

 (add-to-list 'LaTeX-indent-environment-list
     '("chorus"  (lambda() (* 3 LaTeX-indent-level))))

ends up closing such environment incorrectly:

\begin{verse}
  Normal 2-spaces indentation
\end{verse}
\begin{chorus}
      Would be great to have it indented
      deeper.
      \end{chorus}

Is there any workaround?

1 Answer 1

4

The function listed in LaTeX-indent-environment-list should also cater for backindenting the \end, see for example LaTeX-indent-tabular. Add the following code to your init file:

(defun mg-LaTeX-indent-chorus ()
  (cl-destructuring-bind
   (beg-pos . beg-col)
   (LaTeX-env-beginning-pos-col)
   (cond ((looking-at "\\\\end{chorus}")
      beg-col)

     (t
      (+ (* 3 LaTeX-indent-level) beg-col)))))

(defun mg-LaTeX-indent-song ()
  (save-excursion
    (LaTeX-find-matching-begin)
    (current-column)))

(add-to-list 'LaTeX-indent-environment-list '("song"   mg-LaTeX-indent-song))
(add-to-list 'LaTeX-indent-environment-list '("chorus" mg-LaTeX-indent-chorus))

Now the following LaTeX code is correctly indented:

\begin{foo}
  \begin{song}
  \begin{verse}
    Normal 2-spaces indentation
  \end{verse}
  \begin{chorus}
        Would be great to have it indented
        deeper.
  \end{chorus}
  \end{song}
\end{foo}
1
  • Is there any way to indent every begin...end like for...else inside a function in C/C++ source code?
    – CodyChan
    Commented Sep 2, 2016 at 4:25

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.