# Using multline or equation based on a boolean flag

I am trying to define a new environment myeq which can either be an equation or a multline. My use case is that in the preamble, I will define myeq to be either replicate the functionaly of equation or multline and then the actual document will only contain myeq environments so that by changing the definition in the preamble, it is possible to get the spacing characteristics either of mutline or of equation.

In particular, this requires that I have to define myeq to simply replicate multline, as follows:

\newenvironment{myeq}{\begin{multline}}{\end{multline}}


However, this produces the following error (see MWE below)

\begin{multline} on input line 8 ended by \end{myeq}.

The error disappears if I replace the multline in the definition of myeq by equation.

MWE:

\documentclass{article}
\usepackage{amsmath}
\newenvironment{myeq}{\begin{multline}}{\end{multline}}

\begin{document}
\begin{myeq}
t
\end{myeq}
\end{document}

• I don't recommend this but \newenvironment{myeq}{\multiline}{\endmultiline}
– user31729
Mar 9, 2016 at 21:51
• I'm not sure what you intend with this: multline should never be used with a single line equation; perhaps you mean to use gather. It should be \newenvironment{myeq}{\gather}{\endgather}. Don't do it. Mar 9, 2016 at 21:52
• Yes, that seems to work, thank you. But I am still wondering if there is a downside to this, and if it is possible to do something similar to the case of equation Mar 9, 2016 at 21:53
• @egreg: I have some text that needs to be formatted both for a 2-column format as well as a single column format. This means that some equations have to have line breaks inserted in the 2-column version. Since the equation environment simply ignores the newline (\) commands, I was wondering if I can use that behavior to keep a single copy of each equation in the main text, and control the 2-column vs 1-column spacing differences by means of a flag in the preamble. Mar 9, 2016 at 21:56

The failure has a technical reason that's hinted at in texdoc technote, section 6.

I suggest something like

\iftrue
\newenvironment{myeq}{\equation}{\endequation}
\else
\newenvironment{\myeq}
{\def\eqbreak{\\}\multline}
{\endmultline}
\fi
\newcommand{\eqbreak}{}


with \eqbreak rather than \\ (that's not really ignored inside equation) inside myeq.

Change \iftrue into \iffalse if you want multline for two column documents.

A possibly better implementation:

\documentclass[
% twocolumn
]{article}
\usepackage{amsmath,amssymb}

\makeatletter
\newenvironment{multiequation}
{%
\if@twocolumn
\def\eqbreak{\\}%
\expandafter\multline
\else
\def\eqbreak{}%
\expandafter\equation
\fi
}
{%
\if@twocolumn
\expandafter\endmultline
\else
\expandafter\endequation
\fi
}
\makeatother
\begin{document}

Some text before the equation to see what happens
and embed the equation into a real paragraph
which should break nicely into a few lines
\begin{multiequation}
\biggl(\int_{-\infty}^{\infty}e^{-x^{2}}\,dx\biggr)^{\!2}
\eqbreak
=\biggl(\int_{-\infty}^{\infty}e^{-x^{2}}\,dx\biggr)
\biggl(\int_{-\infty}^{\infty}e^{-y^{2}}\,dy\biggr)
\eqbreak
=\int\limits_{\mathbb{R}^{2}}e^{-x^{2}-y^{2}}\,dx\,dy
\end{multiequation}
and some text after the equation to see what happens
and embed the equation into a real paragraph
which should break nicely into a few lines

\end{document}


## Output with twocolumn active

Other than for this application, multline should never be used for single line displays. Maybe gather, with

\newenvironment{myeq}{\gather}{\endgather}


but you risk something like the following:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

A paragraph that ends with a short line preceding a display,
let's see if we can
$$(a+b)^2=a^2+2ab+b^2$$

A paragraph that ends with a short line preceding a display,
let's see if we can
\begin{gather}$$(a+b)^2=a^2+2ab+b^2$$\end{gather}

A paragraph that ends with a short line preceding a display,
let's see if we can

\end{document}


No doubt the second one is ugly.

• Thanks. The reason I want to format a 'single line display' as a multline is because such a display does not remain a 'single line display' anymore when I move to a two-column format (since the width of the line decreases). All I am trying to do is to be able to generate both the single column and two-column versions from the same version of the equation that differs only in the placement of the newline commands. Mar 9, 2016 at 22:10
• +1 for citing technote. the equation environment was restored to the original latex behavior (after many complaints) so that it can be accessed by commands such as \beq ... \eeq, but adapting the multi-line display environments was prohibitively complicated. however, regarding the desire to switch between one- and two-column pages, would it not be possible to test for the number of columns, and then launch equation or gather as appropriate ("switching off" a necessary \\  for the one-line case)? Mar 9, 2016 at 22:13
• @Piyush I added a better version according to barbara beeton's comment Mar 9, 2016 at 22:29
• @barbarabeeton Good idea! Mar 9, 2016 at 22:29
• very nice example. too bad i can't add another +1. Mar 9, 2016 at 22:35

Here's a LuaLaTeX-based approach, which acts as a pre-processor: Before TeX's eyes get going, the Lua function switch_eq sweeps over all input lines and replaces any instances of \begin{myeq} with either or \begin{gather} depending on whether the Boolean variable Gather is true or false. Ditto with all instances of \end{myeq}.

The code further provides two LaTeX macros, named \GatherTrue and \GatherFalse, which set the value of the Boolean variable Gather to either true or false.

In the following screenshot, note that equations (1) and (3) are spaced tightly, whereas (2) -- the one that's generated by gather rather than by equation -- is spaced profligately.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for "gather" environment

%% Lua-side code
\usepackage{luacode}
\begin{luacode}
Gather = false -- default value of "Gather" is "false"
function switch_eq ( buff )
if string.find ( buff , "\\begin{myeq}" ) then
if Gather == true then
return ( string.gsub ( buff, "\\begin{myeq}", "\\begin{gather}" ) )
else
return ( string.gsub ( buff, "\\begin{myeq}", "\$$" ) ) end elseif string.find ( buff , "\\end{myeq}" ) then if Gather == true then return ( string.gsub ( buff, "\\end{myeq}", "\\end{gather}" ) ) else return ( string.gsub ( buff, "\\end{myeq}", "\$$" ) )
end
end
end
luatexbase.add_to_callback ( "process_input_buffer" , switch_eq , "switch_eq" )
\end{luacode}

%% TeX-side code
\newcommand\GatherTrue{\directlua{Gather = true}}
\newcommand\GatherFalse{\directlua{Gather = false}}

\setlength\textwidth{8cm} % just for this example
\begin{document}
----------------------------

\begin{myeq}
1+1=2
\end{myeq}

----------------------------

\GatherTrue
\begin{myeq}
1+1=2
\end{myeq}

----------------------------

\GatherFalse
\begin{myeq}
1+1=2
\end{myeq}

----------------------------
\end{document}