12

Let's say I'm editing a latex in Auctex's latex-mode, and I have the following equation.
This is just indented with indent-region.

\begin{align}
    \phi & = a + b
    + c + d \\
    & = a + b
    + c + d + e
\end{align}

The above alignment is what I get if I select everything and hit TAB, that is, if I'm just using indent-region.

What I would like to get is the following:
Note how the + c + d is aligned with the text after the &. This would make the most sense to be, as the + c + d are part of the same "column" as = a + b.

\begin{align}
    \phi & = a + b
           + c + d \\
         & = a + b
           + c + d + e
\end{align}

Below is what I get if I call align-current.

\begin{align}
    \phi & = a + b
    + c + d \\
         & = a + b
         + c + d + e
\end{align}

Which almost gets there, but not quite. The difference is clear.
Also, if I decide to remove the \phi, align-current does something simply bizarre:

\begin{align}
 & = a + b
    + c + d \\
 & = a + b
         + c + d + e
\end{align}

Is there a way I can customize align-current or use something else to get the desired effect?

Malabarba
  • 22,878
  • 6
  • 78
  • 163
  • 1
    I am not at my computer currently to write a proper answer. But you can select that region and use `align-regexp` and provide the regex of characters you want to align `+|=` . You might have to escape some of those characters. – Kaushal Modi Sep 25 '14 at 10:32
  • The regex you enter is actually `[+=]`. But it won't give exactly what you wanted.. it won't align the `&` along with the `+`. – Kaushal Modi Sep 25 '14 at 12:17

2 Answers2

6

The result that you want is already an AUCTeX feature since October 2013. This is not yet present in the current release (11.87). All you have to do is mark the region and hit TAB. See LaTeX-hanging-ampersand-position for how the indentation is implemented.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • Ok, TAB indeed works! Now how do I turn the git repo into a package? I managed to install it fine. But I have elpa packages that depend on auctex, and it seems calling `make install` from the git repo doesn't seem to define an "auctex" package so those other packages fail to initialize. – Malabarba Oct 16 '14 at 11:23
  • `(require 'tex-site)` should suffice after `make install` was completed. – abo-abo Oct 16 '14 at 11:40
  • @abo-abo That activates auctex, but package.el still doesn't see it for me. In any case, I got around this by also installing Auctex from Elpa and just removing everything but the `-pkg` file from its installation directory. – Malabarba Oct 17 '14 at 11:06
4

Assuming that you don't have any blank lines between the \begin and \end of your equations, you can call this function while your cursor is anywhere within the \begin-\end region.

(defun my/align-latex-eq ()
  "Align the & chars and then align the +/= chars."
  (interactive)
  (backward-paragraph)
  (mark-paragraph)
  ;; align-regexp syntax:  align-regexp (beg end regexp &optional group spacing repeat)
  (align-regexp (region-beginning) (region-end) "\\(\\s-*\\)&" 1 1 nil)
  (mark-paragraph)
  (align-regexp (region-beginning) (region-end) "\\(\\s-*\\)[+=]" 1 1 nil))
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179