# Flexible equation, align, multline environment

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!

-
I meant double back-slash, but it didn't show. – Emanuele Viola Nov 27 '11 at 22:22
A tip: you can enclose TeX code in back quotes as I've done in my edit. – egreg Nov 27 '11 at 22:27
It should be possible to define a custom environment that parses its contents to determine which one is appropriate based on the algorithm you describe and then pass the environment \BODY to the appropriate environment, at least for simple cases. But that is beyond my skill set. – Peter Grill Dec 2 '11 at 1:01

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.

-
Thanks (grazie) for the pointers. However you are misinterpreting my question. I am telling LaTeX where to break the equation. All I want is LaTeX to be smart enough to use the appropriate environment! One can write e.g. a Perl script that does the job. Can't it be done automatically by LaTeX? – Emanuele Viola Nov 28 '11 at 13:54
@EmanueleViola I've asked the moderator to remove the $100 offer, which is quite inappropriate here. The solution is actually rather easy; but it's not something I would use. – egreg Dec 5 '11 at 13:55 THANK YOU! You just made my day. And of course I stand by my previous$100 offer. I am really glad to see this solved and that the solution isn't complicated. – Emanuele Viola Dec 5 '11 at 20:56