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?

2
  • 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. Commented Sep 25, 2014 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 +. Commented Sep 25, 2014 at 12:17

2 Answers 2

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.

3
  • 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
    Commented Oct 16, 2014 at 11:23
  • (require 'tex-site) should suffice after make install was completed.
    – abo-abo
    Commented Oct 16, 2014 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
    Commented Oct 17, 2014 at 11:06
5

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))

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.