自动用方程+对齐组合替换环境对齐

自动用方程+对齐组合替换环境对齐

我想自动用方程式 + 对齐组合替换对齐环境。具体来说,每当\begin{align}发生时,它都应自动将其读为\begin{equation}\begin{aligned}。同样,\end{align}应替换为\end{aligned}\end{equation}。出现的\nonumber也需要被吞噬和忽略。

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

理想情况下,我希望实现这一点无需在整个文档中执行手动替换。我看到的最简单的解决方案是创建一个能够实现所需效果的新环境,但我仍然需要用新环境替换所有现有的对齐环境的使用。

我找不到或创建这个的实现。我能找到的最相关的答案是:修改 eqnarray 以匹配 amsmath align

答案1

使用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}

在此处输入图片描述

答案2

为了完整起见,这里有一个基于 LuaLaTeX 的解决方案。它定义了一个名为的 Lua 函数replace_align,用于执行字符串替换。align环境被替换为equation/aligned组合,align*环境被替换为equation*/aligned组合。

Lua 函数被分配给process_input_buffer回调,该回调在非常早期的阶段对 tex 文件的内容进行操作,LuaTeX 的 TeX 端进行任何实际处理。

在此处输入图片描述

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

相关内容