我正在写一篇大型论文,最近发现了一个autonum
包,它会自动仅有的文本中引用的数字方程。这似乎是对我序言的一个很好的补充,但不幸的是,包括autonum
将取消定义所有带星号的amsmath
环境(对我的情况影响最大align*
),因此使用任何带星号的环境的文档都不会编译。
现在我还不能 100%确定我autonum
是否要使用该包,因此我不想用 替换所有实例align*
(align
这在以后很难撤消),而是想简单地重新定义align*
环境来执行与 相同的操作align
。
我迄今为止的尝试
\newenvironment{align*}
{\begin{align}}{\end{align}}
失败并出现错误Command \align* already defined
。所以我尝试了\renew...
\renewenvironment{align*}
{\begin{align}}{\end{align}}
但失败并出现错误Environment align* undefined
。
最小工作示例(或者非工作示例)
\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}
% this gives the errors
% Command \align* already defined. {\begin{align}}{\end{align}}
% Environment align* undefined. \begin{align*}
% \newenvironment{align*}
% {\begin{align}}{\end{align}}
% this gives the error
% Environment align* undefined. \begin{align*}
% \renewenvironment{align*}
% {\begin{align}}{\end{align}}
\begin{document}
\begin{align} % labelled and referenced equation -- will get numbered
a = b + c
\label{eq:eq1}
\end{align}
Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
a = b + c
\label{eq:eq2}
\end{align}
\begin{align*} % starred envir, will crash compilation
a = b + c
\end{align*}
\end{document}
答案1
您可以通过在开始文档时进行适当的定义来完成所有带星号的环境:
\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}
\makeatletter
\newcommand{\restore@Environment}[1]{%
\AtBeginDocument{%
\csletcs{#1*}{#1}%
\csletcs{end#1*}{end#1}%
}%
}
\forcsvlist\restore@Environment{alignat,equation,gather,multline,flalign,align}
\makeatother
\begin{document}
\begin{align} % labelled and referenced equation -- will get numbered
a = b + c
\label{eq:eq1}
\end{align}
Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
a = b + c
\label{eq:eq2}
\end{align}
\begin{align*}
a = b + c
\end{align*}
\end{document}
在我的实验中,使用\tag
将添加标签,而不管标记的方程式是否被引用。
答案2
我不知道我是否正确理解了你的要求,但看看这是否适合你:
\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}
% this gives the errors
% Command \align* already defined. {\begin{align}}{\end{align}}
% Environment align* undefined. \begin{align*}
% \newenvironment{align*}
% {\begin{align}}{\end{align}}
% this gives the error
% Environment align* undefined. \begin{align*}
% \renewenvironment{align*}
% {\begin{align}}{\end{align}}
\begin{document}
\makeatletter
\@namedef{align*}{%
\start@align\@ne\st@rredfalse\m@ne
}
\@namedef{endalign*}{\endalign}
\makeatother
Some text.
\begin{align} % labelled and referenced equation -- will get numbered
a = b + c
\label{eq:eq1}
\end{align}
Some text. Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
a = b + c
\label{eq:eq2}
\end{align}
Some text.
\begin{align*} % starred envir, will crash compilation
a = b + c
\end{align*}
\end{document}
请注意,必须插入命令补丁后 \begin{document}
。
答案3
如果你可以自由使用 LuaLaTeX,那么在序言中添加以下七行代码将自动{align*}
用{align}
“on the fly”替换所有实例,前TeX 的“眼睛”开始处理。这样,LaTeX 就永远不会“看到”align*
环境,因为它们都已转换为align
环境。
\usepackage{luacode}
\begin{luacode}
function alignstar ( buff )
return ( string.gsub ( buff, "{align%*}", "{align}" ) )
end
luatexbase.add_to_callback ( "process_input_buffer", alignstar, "alignstar" )
\end{luacode}
完整的 MWE:
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath,cleveref,autonum}
\usepackage{luacode}
\begin{luacode}
function alignstar ( buff )
return ( string.gsub ( buff, "{align%*}", "{align}" ) )
end
luatexbase.add_to_callback ( "process_input_buffer", alignstar, "alignstar" )
\end{luacode}
\begin{document}
\begin{align} % labelled and referenced equation -- will get numbered
a = b + c
\label{eq:eq1}
\end{align}
Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
a = b + c
\label{eq:eq2}
\end{align}
\begin{align*} % starred envir, will no longer crash compilation
a = b + c
\end{align*}
\end{document}
附录:如果你不仅需要修复所有align*
环境,还需要修复所有gather*
环境,那么你需要做的就是替换以下行
return ( string.gsub ( buff, "{align%*}", "{align}" ) )
和
buff = string.gsub ( buff, "{align%*}", "{align}" )
buff = string.gsub ( buff, "{gather%*}", "{gather}" )
return buff