1

I want to write a very long equation (3-4 lines) in LaTeX. For this equation I want to use align environment. What I don't like is how Emacs formats the LaTeX code: it adds a lot of unnecessary space, something like

\begin{align*}
  \|a+b+c\|^4 & = \left(\|a\|^2 + \|b\|^2 + \|c\|^2 + 2 \langle a, b
                \rangle + 2\langle b,c \rangle + 2 \langle a, c\rangle
                \right)^2\\ &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2 +
                              2\langle a, b\rangle +2\langle
                              b,c\rangle +2\langle a, c\rangle\right)^2
\end{align*}

And it becomes even worse if I edit it. Obviously, it is very inconvenient to edit such a mess in the future. How to force align not to use any automatic formatting and behave like multline, for example?

\begin{multline*}
  \|a+b+c\|^4 = \left(\|a\|^2 + \|b\|^2 + \|c\|^2 +2 \langle a, b
    \rangle + 2\langle b, c \rangle +2 \langle a,c\rangle\right)^2\\
  = \left(\|a\|^2 + \|b\|^2 + \|c\|^2 +2 \langle a, b \rangle +
    2\langle b, c \rangle +2 \langle a,c\rangle\right)^2
\end{multline*}

I use Emacs 26.3 and AuCTeX 12.2.0.

1

You can remove ("align*" LaTeX-indent-tabular) from LaTeX-indent-environment-list and get the standard indent behaviour like multilines environment.

You can get more information about this variable with

(describe-variable 'LaTeX-indent-environment-list)

You can also compose your own align* environment formatting function...

| improve this answer | |
  • Thanks, this is perfect! – cheyp Jun 8 at 15:21
2

I think you can get rid of any extra space that auctex adds, if you write it like this:

\begin{align*}
  \|a+b+c\|^4
  &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2 + 2 \langle a, b
    \rangle + 2\langle b,c \rangle + 2 \langle
    a, c\rangle\right)^2 \\
  &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2 + 2\langle a,
    b\rangle +2\langle b
    ,c\rangle +2\langle a, c\rangle\right)^2
\end{align*}

putting each &= on a new line. That has the additional advantage that the text in your emacs buffer has the two &= aligned, just as the = signs will be in the final output.

I would advise you not to split the \langle ... \rangle construct over multiple lines: find better places to split it where it makes mathematical sense. E.g.

\begin{align*}
  \|a+b+c\|^4
  &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2
    + 2\langle a, b \rangle + 2\langle b,c \rangle + 2\langle a, c\rangle\right)^2 \\
  &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2
    + 2\langle a, b\rangle +2\langle b ,c\rangle +2\langle a, c\rangle \right)^2
\end{align*}

I actually find it more readable if I go back to the plentiful initial space that you don't like:

\begin{align*}
  \|a+b+c\|^4  &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2
                 + 2\langle a, b \rangle + 2\langle b,c \rangle + 2\langle a, c\rangle\right)^2 \\
               &= \left(\|a\|^2 + \|b\|^2 + \|c\|^2
                 + 2\langle a, b\rangle +2\langle b ,c\rangle +2\langle a, c\rangle \right)^2
\end{align*}

That corresponds much more closely to what the final output will look like.

Finally, I would define a new command with two arguments to shorten the oft-occurring `\langle .., .. \rangle' and use it, like this:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\pair}[2]{\ensuremath{\langle #1, #2 \rangle}}

\begin{document}
...
\begin{align*}
  \|a+b+c\|^4 & = \left(\|a\|^2 + \|b\|^2 + \|c\|^2
                + 2\pair{a}{b} + 2\pair{b}{c} + 2\pair{a}{c}\right)^2\\
              & = \left(\|a\|^2 + \|b\|^2 + \|c\|^2
                + 2\pair{a}{b} + 2\pair{b}{c} + 2\pair{a}{c}\right)^2
\end{align*}
...

That's easy to parse by eye and even six months later, you should be able to see what it does.

Apart from the first example, the rest have nothing to do with emacs, just with how to format math in TeX/Latex. IOW, that's how I would write them even if I did not use emacs and auctex.

| improve this answer | |
  • Thanks, this looked better. Unfortunately, when several people write into one tex file, it's often hard to have its style consistent. And of course, for \langle \rangle I use a shortcut exactly as you said, it was just an example. Also with yasnippet you can just type lr + tab and get the same. – cheyp Jun 8 at 15:20
  • If there are multiple people contributing, then I would argue it is even more important to keep the style consistent. It's not only you who would need to understand it again after six months, it's every contributor needing to understand it again after every revision. – NickD Jun 8 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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