Automatically replace environment align by equation+aligned combination
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{equation}
\begin{aligned}
x & = y + z, \\
\alpha &= \beta + \gamma.
\end{aligned}
\end{equation}
\end{document}
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{equation}\begin{aligned}
x &= y + z, \\
\alpha &= \beta + \gamma.
\end{aligned}\end{equation}
\end{document}