# Automatically replace environment align by equation+aligned combination

I would like to automatically replace the align environment by an equation + aligned combination. Specifically, whenever \begin{align} occurs, it should automatically read it as \begin{aligned}. Similarly, \end{align} should be replaced by \end{aligned}. Occurrences of \nonumber also need to be gobbled up and ignored.

\documentclass[a4paper]{article}
\usepackage{amsmath}

\begin{document}

Any align such as
\begin{align}
x & = y + z, \nonumber \\
\alpha &= \beta + \gamma,
\end{align}
should automatically be replaced by an equation + aligned combination, effectively becoming
\begin{aligned} x & = y + z, \\ \alpha &= \beta + \gamma. \end{aligned}

\end{document}


Ideally, I would like to achieve this without having to perform a manual replaceall throughout my document. The simplest solution I see would be to create a new environment that achieves the required effects, but then I would still need to replace all existing uses of the align environment with the new environment.

I've not been able to find or create an implemention of this. The most related answer I've been able to find is: Modify eqnarray to match amsmath align.

• By help of TeX editor replace \begin{align} with \begin{aligned} and similarly \end{align} with end{align}. Editors are able to replace all occurrences of this replacing without any manual intervention. Sep 30 '15 at 10:36
• @Zarko I would like to avoid using my TeX editor to achieve this. I'm looking for a method that I can apply to a complicated document structure, with several dozens of files, and, each file is actually part of different projects that preferably should not be changed. I would therefore like to avoid a hard replacement of \begin{align} and \end{align} in my source files. I'm instead looking for something along the lines of a redefined environment, or tying into environment hooks. Sep 30 '15 at 11:00

Use environ.

\documentclass[a4paper]{article}
\usepackage{amsmath,environ}

\RenewEnviron{align}{%
\let\nonumber\relax % this is local to this environment
\let\notag\relax % this is local to this environment
\let\tag\relaxtag % this is local to this environment
\equation % start equation
\!% see http://tex.stackexchange.com/questions/98482
\aligned % start aligned
\BODY % the contents
\endaligned % end aligned
\endequation % end equation
}
\def\relaxtag#1#{\relaxrelaxtag}
\def\relaxrelaxtag#1{}

\begin{document}

Any align such as
\begin{align}
x & = y + z, \nonumber \\
\alpha &= \beta + \gamma,
\end{align}
should automatically be replaced by an equation + aligned
combination, effectively becoming
\begin{aligned} x & = y + z, \\ \alpha &= \beta + \gamma. \end{aligned}

\end{document}


• I guess the \! you put before \aligned is there to cancel the \null\, in \start@aligned. Do you have any idea as why this small space is there in the first place? Sep 30 '15 at 11:07
• @campa It has been discussed on the site, IIRC Sep 30 '15 at 11:45
• Found it, thanks. Sep 30 '15 at 12:09
• I don't think the \! directive is needed if the objective is simply to replicate the look of a combined equation/aligned environment. For sure, with your code the first equation is shifted to the left -- by \! -- relative to the 'true' equation/aligned output shown at the bottom.
– Mico
Oct 2 '15 at 5:54

Just for completeness, here's a LuaLaTeX-based solution. It defines a Lua function called replace_align that does the string replacements. align environments are replaced with equation/aligned combinations, and align* environments are replaced with equation*/aligned combinations.

The Lua function is assigned to the process_input_buffer callback, which operates at a very early stage on the contents of the tex file, before the TeX-side of LuaTeX does any real processing.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for "align", "aligned", and "equation*" environments

%% Lua-side code
\usepackage{luacode}
\begin{luacode*}
in_align = false -- Set up a Boolean toggle variable

function replace_align ( buff )
if string.find ( buff , "\\begin{align" ) then
buff = string.gsub ( buff , "\\begin{align(*?)}" ,
"\\begin{equation".."%1".."}\\!\\begin{aligned}" )
in_align = true     -- Set in_align to "true"
elseif string.find ( buff , "\\end{align" ) then
buff = string.gsub ( buff , "\\end{align(*?)}" ,
"\\end{aligned}\\end{equation".."%1".."}" )
in_align = false    -- Set in_align to "false"
elseif in_align then  -- Gobble "\nonumber" and "\notag"
buff = string.gsub ( buff , "\\nonumber" , "")
buff = string.gsub ( buff , "\\notag" , "")
end
return ( buff )
end

luatexbase.add_to_callback ( "process_input_buffer", replace_align, "replace_align" )
\end{luacode*}

\begin{document}
Any \verb+align+ environment, such as
\begin{align}
x      &= y + z,  \nonumber \\
\alpha &= \beta + \gamma,
\end{align}
should automatically be replaced by an \verb+equation/aligned+ combination, effectively becoming
\begin{aligned} x &= y + z, \\ \alpha &= \beta + \gamma. \end{aligned}
\end{document}