Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

AucTex works fine automatically indenting most environments, for example

\begin{displaymath}
  \cos x
\end{displaymath}

but doesn't indent when I use \[ and \], and automatically takes away any indentation I have done manually. It looks like this

\[
\cos x
\]

but I want it to look like this

\[
  \cos x
\]

It seems that AucTeX doesn't recognise \[...\] as a displaymath environment for indentation. I've looked at AucTeX customisation for indentation but there doesn't seem to be a way to add an environment to be indented. As I understand it the user option 'LaTeX-indent-environment-list' is for special exceptions.

Is there any way to get AucTeX to indent between \[ and \] ?

share|improve this question
    
Is using equation* environment in place of \[...\] an acceptable workaround? –  giordano Aug 15 at 17:07
    
Not really. If I can't get it to work I'll keep using \[..\] without indentation. I know this is a bit fussy for such a silly little thing, but I would like to know if it is possible to get it to behave as I would like. –  Tom Aug 15 at 17:27
1  
equation* is a LOT better, when you are using auctex. Then if you need align* instead, you just use C-u C-c C-e align* and hit return. You cannot do that for \[... \] –  daleif Aug 15 at 22:31
    
@ daleif I did wonder if there was a reason not to use \[...\]. The reason I use it is because it gives a bit more white space around the maths, and I find that helps me to read it more easily. I take your point though and will see how it goes. –  Tom Aug 16 at 6:50
1  
@Tom imo \[\] makes the code less readable. \begin... \end makes much more obvious block structures in the code. Plus as I said, auctex provide a load of cool features that work on environments (in the \begin... \end sence). Trust us, once you get deeper into auctex, you do not want to use this construction. In my personal setup I have a quick macro to turn all of them into equation* –  daleif Aug 16 at 10:04

2 Answers 2

up vote 5 down vote accepted

Add the following code to your initialization file and restart Emacs

(eval-after-load "latex"
  '(progn
     (defun LaTeX-indent-calculate (&optional force-type)
       "Return the indentation of a line of LaTeX source.
FORCE-TYPE can be used to force the calculation of an inner or
outer indentation in case of a commented line.  The symbols
'inner and 'outer are recognized."
       (save-excursion
     (LaTeX-back-to-indentation force-type)
     (let ((i 0)
           (list-length (safe-length docTeX-indent-inner-fixed))
           (case-fold-search nil)
           entry
           found)
       (cond ((save-excursion (beginning-of-line) (bobp)) 0)
         ((and (eq major-mode 'doctex-mode)
               fill-prefix
               (TeX-in-line-comment)
               (progn
             (while (and (< i list-length)
                     (not found))
               (setq entry (nth i docTeX-indent-inner-fixed))
               (when (looking-at (nth 0 entry))
                 (setq found t))
               (setq i (1+ i)))
             found))
          (if (nth 2 entry)
              (- (nth 1 entry) (if (integerp comment-padding)
                       comment-padding
                     (length comment-padding)))
            (nth 1 entry)))
         ((looking-at (concat (regexp-quote TeX-esc)
                      "\\(begin\\|end\\){\\("
                      LaTeX-verbatim-regexp
                      "\\)}"))
          ;; \end{verbatim} must be flush left, otherwise an unwanted
          ;; empty line appears in LaTeX's output.
          0)
         ((and LaTeX-indent-environment-check
               ;; Special environments.
               (let ((entry (assoc (or LaTeX-current-environment
                           (LaTeX-current-environment))
                       LaTeX-indent-environment-list)))
             (and entry
                  (nth 1 entry)
                  (funcall (nth 1 entry))))))
         ((looking-at (concat (regexp-quote TeX-esc)
                      "\\("
                      LaTeX-end-regexp
                      "\\)"))
          ;; Backindent at \end.
          (- (LaTeX-indent-calculate-last force-type) LaTeX-indent-level))
         ((looking-at (concat (regexp-quote TeX-esc) "right\\b"))
          ;; Backindent at \right.
          (- (LaTeX-indent-calculate-last force-type)
             LaTeX-left-right-indent-level))
         ((looking-at (concat (regexp-quote TeX-esc)
                      "\\("
                      LaTeX-item-regexp
                      "\\)"))
          ;; Items.
          (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
         ((looking-at "}")
          ;; End brace in the start of the line.
          (- (LaTeX-indent-calculate-last force-type)
             TeX-brace-indent-level))
         ((and (texmathp)
               ;; Display math \[...\], treat as a generic environment.
               (equal "\\[" (car texmathp-why)))
          LaTeX-indent-level)
         (t (LaTeX-indent-calculate-last force-type))))))
     ;; Treat \] as a generic \end{...}
     (setq LaTeX-end-regexp "end\\b\\|\\]")))

This redefines LaTeX-indent-calculate to cater for \[...\] math mode. Not tested thoroughly, may need some fix.

share|improve this answer
    
Thanks so much! This worked. I'm completely new to emacs and lisp so I have no idea why it worked, but it gives me something to puzzle over. There's no way I would have gotten to this answer on my own any time soon. –  Tom Aug 15 at 19:33
    
@Tom Actually I slightly changed the existing LaTeX-indent-calculate function to fit your needs, I didn't write that code from the scratch :-) Elisp is pretty easy to know as it is widely documented within Emacs itself, C-h f, C-h v, C-h k, and C-h i are your friends ;-) –  giordano Aug 15 at 19:36

Following and using the magnificent answer from @giordano, as base, to a follow up of the question, I made the changes directly in the file latex.el, and later configured and compiled AucTeX.

The changes were mostly to try out \( instead of \[.

         ;; Items.
     (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
    ((looking-at "}")
     ;; End brace in the start of the line.
     (- (LaTeX-indent-calculate-last force-type)
    TeX-brace-indent-level))
    ((and (texmathp)
      ;;Display math \[...\], treat as a generic environment
      (equal "\\[" (car texmathp-why)))
     LaTeX-indent-level)
    ((or (texmathp)
     (equal "\\(" (car texmathp-because)))
     LaTeX-indent-level)
    (t (LaTeX-indent-calculate-last force-type))))))

But the above is a mediocre attempt with the current version. \( does, what it's supposed to...but that's about it, it's useless.

The following, on the other hand, is less sub par than the above:

              ;; Items.
 (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
((looking-at "}")
 ;; End brace in the start of the line.
 (- (LaTeX-indent-calculate-last force-type)
TeX-brace-indent-level))
((and (texmathp)
  ;;Display math \[...\], treat as a generic environment
  (equal "\\[" (car texmathp-why)))
 LaTeX-left-right-indent-level)
(t (LaTeX-indent-calculate-last force-type))))))

Third edit and last: This is what I'm talking about! Shame on me for not seeing it.

Both \( and \[ were compiled with the latest AucTeX.

              ;; Items.
 (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
((looking-at "}")
 ;; End brace in the start of the line.
 (- (LaTeX-indent-calculate-last force-type)
TeX-brace-indent-level))
((and (texmathp)
  ;;Display math \[...\], treat as a generic environment
  (equal "\\[" (car texmathp-why)))
 LaTeX-indent-level)
((and (texmathp)
  (equal "\\(" (car texmathp-why)))
 LaTeX-indent-level)
 (t (LaTeX-indent-calculate-last force-type)))))) 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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