[Tex/LaTex] Flexible equation, align, multline environment

alignequationsmultline

I'm looking for a command (say \foo{}) or pair of commands (say \foos, \fooe) that behave like:

equation environment if there is no \\ nor &

multline if there is a single \\

align if there are both & and \\

Also, automatically switches between the * variants if there is no \label command.

The commands should be compatible with usual packages (amsmath etc), and should support the usual \tag, \label, \ref commands. Just to be clear, using it should not slow down compilation by any perceptible amount.

Bonus gratitude if you also give me a command to switch the behavior of \[ \] from the usual one to the above one, back-and-forth in a paper.

I am constantly switching between the three environments and it is really annoying. Thanks!

Note: I am telling LaTeX where to break the equation. All I want is LaTeX to be smart enough to use the appropriate environment!

Best Answer

No, there isn't such a "do-it-all" environment. Each one of equation, multline, align and gather (you forgot about this one) solves a different problem caused by different sets of math mode material to display.

Breaking math mode material requires judgment that a machine can't have.

There are packages such as nath and breqn that try to automatize this process, but my opinion is that there's no easy algorithm to do breaking properly, because it requires to look at the result.

However, here it is a working doitall environment:

\documentclass[a4paper]{article}
\usepackage{amsmath,environ,xstring}
\newif\iflabel
\newif\ifdbs
\newif\ifamp
\NewEnviron{doitall}{%
  \noexpandarg
  \expandafter\IfSubStr\expandafter{\BODY}{\label}{\labeltrue}{\labelfalse}%
  \expandafter\IfSubStr\expandafter{\BODY}{\\}{\dbstrue}{\dbsfalse}%
  \expandafter\IfSubStr\expandafter{\BODY}{&}{\amptrue}{\ampfalse}%
  \iflabel\def\doitallstar{}\else\def\doitallstar{*}\fi
  \ifdbs
    \ifamp
      \def\doitallname{align}%
    \else
      \def\doitallname{multline}%
    \fi
  \else
    \def\doitallname{equation}
  \fi
  \begingroup\edef\x{\endgroup
    \noexpand\begin{\doitallname\doitallstar}%
    \noexpand\BODY
    \noexpand\end{\doitallname\doitallstar}%
  }\x
}

\begin{document}
Numbered

\begin{doitall}
a=b\label{1}
\end{doitall}
\begin{doitall}
a\\b\label{2}
\end{doitall}
\begin{doitall}
a&=b\label{3}\\
c&=dxxxxx
\end{doitall}

Now no numbers

\begin{doitall}
a=b
\end{doitall}
\begin{doitall}
a\\b
\end{doitall}
\begin{doitall}
a&=b\\
c&=dxxxxx
\end{doitall}
\end{document}

I can understand your desire of a do-it-all environment; but you're losing information.

If you want to use \[ and \] as delimiters, just say

\def\[#1\]{\begin{doitall}#1\end{doitall}}

in the preamble. Switching the meaning of \[...\] back and forth is completely useless, of course.

Limitations

You can't use aligned or gathered environments inside the doitall environment, as these have & and \\ inside them. It's of course impossible to use also the split environment.

Related Question