15

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{equation}\begin{aligned}. Similarly, \end{align} should be replaced by \end{aligned}\end{equation}. 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{equation}
\begin{aligned}
x & = y + z, \\
\alpha &= \beta + \gamma.
\end{aligned}
\end{equation}

\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.

  • 1
    By help of TeX editor replace \begin{align} with \begin{equation}\begin{aligned} and similarly \end{align} with end{align}\end{equation}. Editors are able to replace all occurrences of this replacing without any manual intervention. – Zarko 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. – Stemic Sep 30 '15 at 11:00
14

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}

enter image description here

  • 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? – campa Sep 30 '15 at 11:07
  • @campa It has been discussed on the site, IIRC – egreg Sep 30 '15 at 11:45
  • Found it, thanks. – campa 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
6

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.

enter image description here

% !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} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.