summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Master/texmf-dist/doc/latex/autonum/autonum.pdfbin238973 -> 259796 bytes
-rw-r--r--Master/texmf-dist/doc/latex/autonum/test-autonum.pdfbin55352 -> 60042 bytes
-rw-r--r--Master/texmf-dist/source/latex/autonum/autonum.dtx465
-rw-r--r--Master/texmf-dist/source/latex/autonum/autonum.ins199
-rw-r--r--Master/texmf-dist/tex/latex/autonum/autonum.sty204
5 files changed, 664 insertions, 204 deletions
diff --git a/Master/texmf-dist/doc/latex/autonum/autonum.pdf b/Master/texmf-dist/doc/latex/autonum/autonum.pdf
index d6b3d8f329e..d7b81e5641c 100644
--- a/Master/texmf-dist/doc/latex/autonum/autonum.pdf
+++ b/Master/texmf-dist/doc/latex/autonum/autonum.pdf
Binary files differ
diff --git a/Master/texmf-dist/doc/latex/autonum/test-autonum.pdf b/Master/texmf-dist/doc/latex/autonum/test-autonum.pdf
index 9704049a3ee..d4141adec2f 100644
--- a/Master/texmf-dist/doc/latex/autonum/test-autonum.pdf
+++ b/Master/texmf-dist/doc/latex/autonum/test-autonum.pdf
Binary files differ
diff --git a/Master/texmf-dist/source/latex/autonum/autonum.dtx b/Master/texmf-dist/source/latex/autonum/autonum.dtx
index 66f3604fe97..4e9bb08f028 100644
--- a/Master/texmf-dist/source/latex/autonum/autonum.dtx
+++ b/Master/texmf-dist/source/latex/autonum/autonum.dtx
@@ -21,6 +21,7 @@ Usage: \usepackage{autonum}
Everything else should happen automatically.
To use autonum together with other packages, load autonum last.
%</readme>
+%<readme>\endbatchfile
%<*internal>
\fi
\def\nameofplainTeX{plain}
@@ -85,8 +86,11 @@ and the derived files autonum.ins,
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{autonum}[2012/05/26 v0.1 autonum LaTeX package]
+\ProvidesPackage{autonum}[2012/06/21 v0.2 autonum LaTeX package]
+\PassOptionsToPackage{hypertexnames=false}{hyperref}
\RequirePackage{etoolbox}
+\RequirePackage{amsmath}
+\RequirePackage[absolute]{textpos}
%</package>
%<*driver>
\documentclass{ltxdoc}
@@ -122,11 +126,12 @@ and the derived files autonum.ins,
%\maketitle
%
%\changes{v0.1}{2012/05/26}{First public release}
+%\changes{v0.2}{2012/06/21}{Support multi-line environments. Redefine \cmd{\[} to use the new \cmd{\equation}.}
%
% \section{Introduction}
-% In \LaTeX, the user has to decide manually which equation gets a number by optionally adding a star in the math environments. Authors who do not want to think about the numbering during the writing (and maybe they should not), often use the environments without stars. As default these users get all equations numbered, although there are \href{http://tex.stackexchange.com/q/29267/7323}{different opinions} about what should be numbered.
+% With \LaTeX, the user has to decide manually to not number an equation by adding a star in the math environment. Authors who do not want to think about the numbering during the writing (and maybe they should not), often use the environments without stars. As default, these users get all equations numbered, although there are \href{http://tex.stackexchange.com/q/29267/7323}{different opinions} about what should be numbered.
%
-% The other automatic possibility one can think of is to number only the referenced equations. The \href{http://www.ctan.org/tex-archive/macros/latex/contrib/mh/}{mathtools} package's option \texttt{showonlyrefs} seems to be the solution for those, who want to have the referenced equations numbered only. Unfortunately for \href{http://www.ctan.org/pkg/amsmath}{amsmath} users this also means a step backwards, as the numbering can overwrite parts of the equation according to its documentation. Generally, this options seems to be quite unreliable. The autonum package implements the numbering of referenced equations only without this deficiencies.
+% The other automatic possibility one can think of is to number only the referenced equations. The \href{http://www.ctan.org/tex-archive/macros/latex/contrib/mh/}{mathtools} package's option \texttt{showonlyrefs} seems to be the solution for those who want to have the referenced equations numbered only. Unfortunately for \href{http://www.ctan.org/pkg/amsmath}{amsmath} users this also means a step backwards, as the numbering can overwrite parts of the equation according to its documentation. Generally, this options seems to be quite unreliable as it is incompatible with the \href{http://www.ctan.org/tex-archive/macros/latex/contrib/cleveref/}{cleveref} package. The autonum package implements the numbering of referenced equations only without this deficiencies.
%^^A This currently does not work with cleveref, as equations without a number increment the equation counter, too. As placing the labels does not always work correctly, \cmd{\mathtoolsset{showonlyrefs}} is not an option (see mathtools documentation, bottom of page 10).
%
% \section{Usage and Examples}
@@ -136,28 +141,25 @@ and the derived files autonum.ins,
% \cmd{\usepackage\{autonum\}}
% \vspace{0.5em}
%
-% The recommended style is to add a label to each equation. Then, simply use the needed references. Never use the starred form.
+% The recommended style is to add a label to each logical equation. Then, simply use the references as needed. Never use the starred form. You can use the (redefined) \texttt{equation} or \cmd{\[}-\cmd{\]}-environment in most cases, if you add the appropriate \cmd{\begin\{split\}}-\cmd{\end\{split\}} pairs when needed. Sometimes, an \texttt{align}, \texttt{multline}, or \texttt{gather} environment is needed.^^A, rarely use the \texttt{aligned} or \texttt{gathered} environments.
%
%\begin{macro}{equation}
-%^^A With amsmath:
% The following examples show the results of the equation environment after loading the package. Now let's reference the third equation: \ref{alreadyReferenced}.
-% \makeatletter
-% \begin{equation*}
+% \begin{equation}
% 1 = 1\quad(\text{equation without label})
-% \end{equation*}
+% \end{equation}
%
-% \begin{equation*}\label{notReferenced}
+% \begin{equation}\label{notReferenced}
% 2 = 2\quad(\text{equation with label, not referenced})
-% \end{equation*}
+% \end{equation}
%
-% \autonum@equationOld\label{alreadyReferenced}
+% \begin{equation+}\label{alreadyReferenced}
% 3 = 3\quad(\text{equation with label, already referenced})
-% \autonum@endequationOld
+% \end{equation+}
%
-% \autonum@equationOld\label{referencedLater}
+% \begin{equation+}\label{referencedLater}
% 4 = 4\quad(\text{equation with label, referenced later})
-% \autonum@endequationOld
-% \makeatother
+% \end{equation+}
% Now let's reference the fourth equation: \ref{referencedLater}. The first and the second equations do not get a number, as they are not referenced.
%
% If you want to try an example yourself, you can take this one, where only the first equation gets a number:
@@ -176,52 +178,54 @@ and the derived files autonum.ins,
% \cmd{\end\{document\}}
% \vspace{0.5em}
%
-%^^A Ohne amsmath:
-%^^A \begin{equation}\label{a}
-%^^A 1+1
-%^^A \end{equation}
-%^^A \begin{equation}\label{b}
-%^^A 2+2
-%^^A \end{equation}
-%^^A \ref{a}
-%
-% To get the correct references up to three compiliation runs are necessary when the autonum package is used (as always in \TeX\ this cannot be guranteed, so in rare cases even that is not enough). This is one additional run compared to the default referencing mode, as one run is needed to check if an equation is used.
+% To get the correct references up to three compilation runs are necessary when the autonum package is used (as always in \TeX\ this cannot be guaranteed, so in rare cases even that is not enough). This is one additional run compared to the default referencing mode, as one run is needed to check if an equation is used. This is probably not avoidable, as the information which equation should have a number is simply not always available in the first run while layouting the equation. Thus, the equation's number can change in the second run. For the reference command, this information is only stable in the third run, as the equations influence each other.
%\end{macro}
%
+% \begin{macro}{\[ et al.}
+% Instead of equation you may also use one of the following environments: \texttt{align}, \texttt{gather}, \texttt{multline}, \texttt{equation}. ^^A, \texttt{aligned}, \texttt{gathered}. ^^A The \texttt{multlined} environment is patched, if it is available.
+% As the commands \cmd{\[} and \cmd{\]} are useless when using autonum, they are redefined to be an alias of \texttt{begin\{equation\}} and \texttt{end\{equation\}}, respectively. ^^A, instead either the \texttt{equation} or the \texttt{align} environments are used, depending on the number of lines in the equation.
+% Without adding labels or referencing added labels, the new \cmd{\[}-\cmd{\]}-environment behaves like the old one. ^^A if the equation is only one line. The new definition inherits all the multi-line and alignment capabilities from align and thus is recommended as default while writing. If the environment's content has only one line, the equation environment is used, which has a \href{http://tex.stackexchange.com/questions/321/align-vs-equation}{smaller vertical space} for short lines above the environment compared to align.
+% \end{macro}
+%
%\begin{macro}{equation+}
-% In the probably rare case, that an equation is very important and not references within the text, but some other person wants to reference to that equation, you can use \cmd{\begin\{equation+\}} and \cmd{\end\{equation+\}}. The equation is then numbered in all cases (having a label or not, being referenced or not). This feature has been added, so that all \href{http://tex.stackexchange.com/a/52287}{three referencing practices} are supported in \LaTeX\ with as much automation as possible.
+% In the rare case, that an equation is very important and not references within the text, but some other person wants to reference to that equation, you can use \cmd{\begin\{equation+\}} and \cmd{\end\{equation+\}}. The equation is then numbered in all cases (having a label or not, being referenced or not). This feature has been added, so that all \href{http://tex.stackexchange.com/a/52287}{three referencing practices} are supported in \LaTeX\ with as much automation as possible. The other math environments support a \texttt{+}-variant, too.
%\end{macro}
%
-%^^A \documentclass{article}
-%^^A \usepackage{amsmath}
-%^^A \usepackage{mathtools}
-%^^A \mathtoolsset{showonlyrefs,showmanualtags}
-%^^A \begin{document}
-%^^A \begin{equation}
-%^^A \sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\sum\label{myequation}
-%^^A \end{equation}
-%^^A Text \ref{myequation}
-%^^A \end{document}
+%\begin{macro}{equation*}
+% The starred versions have been deleted, to avoid accidental use. Use the versions without star and without adding a label, to avoid that an equation gets a number.
+%\end{macro}
%
+% \section{Compatibility}
+% \subsection{Load Order}
+% As other packages might break autonum, it should be loaded very late. Normally, amsmath is loaded automatically to make use of the more advanced math environments. Nevertheless, to use other packages, it might be necessary to load amsmath manually. To use autonum with amsmath, hyperref and cleveref for example, the order must be amsmath $\rightarrow$ hyperref $\rightarrow$ cleveref $\rightarrow$ autonum, as cleveref may not be loaded after amsmath. If a wrong order has been active, it might be necessary to delete the aux file manually to get rid of compile errors. The support of autonum without amsmath has been discontinued due to missing demand.
%
-% \section{Compatibility and Load Order}
-% The autonum package is very careful to not break other packages, but other packages might break autonum. Thus, autonum should be loaded very late. To use it with amsmath, hyperref and cleveref (\cmd{\cref}) for example, the order must be amsmath $\rightarrow$ hyperref $\rightarrow$ cleveref $\rightarrow$ autonum. If a wrong order has been active, the aux file must potentially be manually deleted to get rid of compile errors.
+% \subsection{Hyperref}
+% The hyperref package must be loaded with the option \texttt{hypertexnames=false} to work when autonum is used.\\
+% Problem description: Generally, the autonum package is careful not to break other packages, but with autonum there are non-starred mathematical environment which do not increase the equation counter. This leads to the following warning in hyperref when more than one equation is used: "destination with the same identifier (name{equation.0.1}) has been already used, duplicate ignored". Additionally, the link anchors do not work correctly (with a reference followed by a labeled equation, there is a warning in tracing mode, too. Hyperref seems to increase \LaTeX's equation's counter (called \texttt{equation}) only if a equation is references. This counter may not be modified to avoid problems with Hyperref, as this would change the displayed equation number (and it does not work anyway). This problem is independent of cleveref. The problem might be solvable by modifying \cmd{\begin\{equation\}} or \cmd{\end\{equation\}}. Using \texttt{gather} instead of \texttt{equation} removes the warning, but hyperlinks still do not work.
+%
+% \subsection{Reference commands}
+% As default the \cmd{\ref} and cleveref's \cmd{\cref} commands are supported. New reference commands can be supported by executing \cmd{\let}\cmd{\ref}\cmd{\NewReferenceCommand}. If the original \cmd{\ref} command should not be overwritten, you can instead use \cmd{\autonum@generatePatchedReference}\{\cmd{\NewReferenceCommand}\}. Mind the \cmd{\makeatletter} and \cmd{\makeatother} command before and after the aforementioned command, respectively.
%
% \section{Further Ideas}
% \begin{itemize}
-% \item A similar approch could be used for figure and table environments. There, a warning could be printed, if an environment is not referenced.
-% \item Instead of "+" a "*" could be used.
-% \item The starred forms of the environments could be undefined with \csundef{equation*} and \csundef{endequation*} (but change the code before).
+% \item A similar approach could be used for figure and table environments to print a warning, if such an environment is not referenced. With the subfig package, the solution would be harder, as a figure or table may be unreferenced, if all subfloats are referenced. Similarly, a subfloat may be unreferenced, if its parent environment is referenced. So the warning should only be printed if an unreferenced parent environment either does not have any child environment or if there is an unreferenced child environment.
+% \item A warning could be printed, if another compile is necessary.
+% \item A "*" could be used instead of a "+".
+% \item If a reference is used before the label is defined, the reference information is saved in a variable and can be used later in the current run when processing the label. It does not have to be saved to the aux file. If a reference is used after defining the label, the reference information is saved in the aux file and can be used in the next run when processing the label. The information does not have to be saved into a variable, as a label must only be defined once and the definition has already happened. Unfortunately, this would make it necessary to distinguish between definitions in the current and in the last run, as otherwise this leads either to oscillation or to defining everything in the end (depending if csdefaux or csdefall is used in the not-defined case), which is probably not worth the effort.
+% \item It would be good, if umlauts were possible in labels -- the \texttt{stringstrings} package might be handy
+% The command \cmd{\(} could be an alias for \cmd{begin\{split\}} and \cmd{\)} could be an alias for \cmd{end\{split\}}
% \end{itemize}
%
% \section{Implementation}
-% The basic idea is to write the usage of each label -- labeling and referencing -- into the aux file and save it as variable, so that the information is available in the current and in the next run. As the equation environment does not know anything about labels, a global linear equation counter is used to identify each equation. After the label information is connected to the counter information, each equation can check, if it is referenced or not.
+% The basic idea is to write into the aux file and save a variable whenever a label is referenced, so that the information is available in the current and in the next run. The label information is passed from the label command to the newline command. The newline command uses the label and the reference information to possibly add a \cmd{\notag} command, deciding if it is referenced or not.
+%
+% ^^A Calling the old newline command leads to a redefinition (overwriting, not patching) of the (current) newline command (proved by using \cmd{\show}\cmd{\\} before and after the call to amsmath's newline command) when using local definitions. As amsmath behaves like a virus then, the patched newline function is only called for the first newline. After the first call, amsmath's hijacked newline is used directly without giving a chance to do something useful. To correct this behavior, the newline command would need to be patched again. Unfortunately, \cmd{\let} cannot be used to save the current command, because the copy generated by \cmd{\let} is deleted by amsmath (WTF?). To avoid all this, some global definitions are used.
%\begin{macro}{\csxdefaux}
-% This command is similar to the \cmd{\csxdef} command from the package etoolbox, but instead of defining the command immediately, it is defined in the next run by writing it to the aux file. The name is given by the first argument (which may not have a leading backslash). The second argument is the replacement text. \cmd{\ifcsdef} can be used to check, if the command has been defined. See also http://tex.stackexchange.com/a/49035
-% Usage: \cmd{\csxdefaux{csname}{replacement}}
+% This command is similar to the \cmd{\csxdef} command from the package etoolbox, but instead of defining the command immediately, it is defined in the next run by writing it to the aux file. The name is given by the first argument (which may not have a leading backslash). The second argument is the replacement text. This command would be a candidate for inclusion into etoolbox. \cmd{\ifcsdef} can be used to check, if the command has been defined. See also http://tex.stackexchange.com/a/49035
+% Usage: \cmd{\csxdefaux\{csname\}\{replacement\}}
% \begin{macrocode}
-\newcommand{\csxdefaux}[2]{%
+\def\csxdefaux#1#2{%
% \end{macrocode}
% The expandafter commands are used to first expand the \cmd{\csname}-\cmd{\endcsname}. Then there is a command definition left, where the command and its replacement (which can also be a command) are protected by \cmd{\string} to create the command in the next run (when the aux file is read) and not in the current run (when the aux file is written).
% \begin{macrocode}
@@ -233,38 +237,117 @@ and the derived files autonum.ins,
%\end{macro}
%\begin{macro}{\csxdefall}
-% This command simply combines the commands \cmd{\csxdef} and \cmd{\csxdefaux}.
+% This command simply combines the commands \cmd{\csxdef} and \cmd{\csxdefaux}. This command would be a candidate for inclusion into etoolbox.
% \begin{macrocode}
-\newcommand{\csxdefall}[2]{%
+\def\csxdefall#1#2{%
\csxdefaux{#1}{#2}%
\csxdef{#1}{#2}%
}
% \end{macrocode}
%\end{macro}
-%\begin{macro}{autonum@counter}
-% Use a counter to numerate all displayed equation environments linearly.
+%\begin{macro}{\ifcsedef}
+% This command is similar to the \cmd{\ifcsdef} test from the package etoolbox, but the command sequence gets fully expanded before it is evaluated.. This command would be a candidate for inclusion into etoolbox.
% \begin{macrocode}
-\newcounter{autonum@counter}
+\def\ifcsedef#1#2#3{%
+ \edef\autonum@ifcsedefTemp{#1}%
+ \expandafter\ifcsdef\expandafter{\autonum@ifcsedefTemp}{#2}{#3}%
+ \undef{\autonum@ifcsedefTemp}%
+}
% \end{macrocode}
%\end{macro}
+% This is needed to not get overwritten by other packages. The package autonum only overwrites some commands whose name start with \cmd{\autonum}. Other commands are only patched, so the currently valid command gets called, too. So although not very polite, this behavior seems reasonable.
+% \begin{macrocode}
+\AtBeginDocument{%
+% \end{macrocode}
+ % Most of amsmath's environments are redefined. The environments alignat and alignedat are not redefined, as their syntax differs and they do not seem so useful to be worth the effort. The environment flalign is also not redefined, as it's behavior is not described in amsmath's documentation. The environments aligned and gathered are not redefined, as it is unclear, how the numbering should work.
+% \begin{macrocode}
+ %^^A\forcsvlist{\autonum@patchBlockEnvironment}{gathered,aligned}%
+%^^A \ifdef{\multlined}{%
+%^^A \autonum@patchBlockEnvironment{multlined}%
+%^^A }{}%
+ \forcsvlist{\autonum@patchFullEnvironment}{equation,gather,multline,align}%
+% \end{macrocode}
+ %
+ % Patch the environment delimited by \cmd{\[} and \cmd{\]}.
+% \begin{macrocode}
+% \autonum@patchShortcutEnvironment
+ \def\[#1\]{%
+ \begin{equation}#1\end{equation}%
+ }%
+% \end{macrocode}
+ %
+ % Support the normal \cmd{\ref} command and, if available, the \cmd{\cref} command from cleveref.
+% \begin{macrocode}
+ \autonum@generatePatchedReference{ref}%
+ \ifdef{\cref}{%
+ \autonum@generatePatchedReference{cref}%
+ }{}%
+% \end{macrocode}
+% \begin{macrocode}
+}
+% \end{macrocode}
+
%\begin{macro}{\autonum@patchEnvironment}
-% Patch a mathematical environment to automatically show an equation's number, if it is referenced and do not use a number otherwise.
+% Patch a mathematical environment to automatically show an equation's number, if a part is referenced and do not use a number otherwise. For completeness, the original definition (numbering every part of an displayed equation structure) is made available using a different name. Do not redefine environment before getting the original label and newline commands. Use center as the default parameter, as a center environment is a neutral element regarding the subcommands' definitions.
% \begin{macrocode}
-\newcommand{\autonum@patchEnvironment}[1]{
- \csletcs{autonum@#1Old}{#1}%
- \csletcs{autonum@end#1Old}{end#1}%
- \autonum@saveOldEnvironment{#1}%
+% \def\autonum@patchEnvironment#1{
+\def\autonum@patchFullEnvironment#1{%
+ \autonum@saveEnvironmentSubcommands{#1}{center}%
+ \autonum@patchEnvironmentHelper{#1}%
+}
+\def\autonum@patchBlockEnvironment#1{%
+ \autonum@saveEnvironmentSubcommands{#1}{equation*}%
+ \autonum@patchEnvironmentHelper{#1}%
+}
+\def\autonum@patchEnvironmentHelper#1{%
+ \autonum@renameEnvironment{#1}%
\autonum@changeEnvironment{#1}%
+ \autonum@generatePatchedLabel{#1}%
+ \autonum@generatePatchedNewline{#1}%
}
% \end{macrocode}
%\end{macro}
-%\begin{macro}{\autonum@saveOldEnvironment}
-% Save the old environment to be accessible with an appended +.
+%\begin{macro}{\autonum@saveEnvironmentSubcommands}
+% This code is to save the newline code used in a mathematic display environment so that it can be used later. This is necessary, as saving it in the instance of the environment, where it should be used, does not work. Allow for two arguments, to enable putting the alignment building blocks into an equation environment (see amsmath documentation).
+% \begin{macrocode}
+\def\autonum@saveEnvironmentSubcommands#1#2{%
+ \begin{textblock}{1}[1,1](0,0)%
+ \begin{#2}
+ \begin{#1}%
+% \end{macrocode}
+ % Using global here is necessary to get the information out of the environment.
+% \begin{macrocode}
+ \global\cslet{autonum@newline#1}\\%
+% \end{macrocode}
+ % Use \cmd{\notag} to not increase the equation counter (otherwise the first equation shown would not have number 1).
+% \begin{macrocode}
+ \notag%
+% \end{macrocode}
+ % For multline, check, that autonum's label command is undefined, because this means, that the first pass (measuring pass) of the environment is active. This is to avoid getting the \cmd{\label} command of the second pass (displaying pass), where the \cmd{\label} command is set to the \href{http://tex.stackexchange.com/a/59088/7323}{null definition}. The multline environment seems to need the first pass, whereas other environments seem to need the second pass, so adapt to the environments.
% \begin{macrocode}
-\newcommand{\autonum@saveOldEnvironment}[1]{
+ \ifboolexpr{not test {\ifstrequal{#1}{multline}} or test {\ifcsundef{autonum@label#1}}}{%
+ \global\cslet{autonum@label#1}{\label}%
+ }%
+% \end{macrocode}
+ % Use \cmd{\notag} again, to avoid an error with the gathered environment. WTF?
+% \begin{macrocode}
+ \notag%
+ \end{#1}%
+ \end{#2}
+ \end{textblock}%
+}
+% \end{macrocode}
+%\end{macro}
+
+%\begin{macro}{\autonum@renameEnvironment}
+% Rename the old environment to be accessible with an appended + by saving the original environment using a different name.
+% \begin{macrocode}
+\def\autonum@renameEnvironment#1{%
+ \csletcs{autonum@#1Old}{#1}%
+ \csletcs{autonum@end#1Old}{end#1}%
\newenvironment{#1+}{%
\csuse{autonum@#1Old}%
}{%
@@ -275,114 +358,258 @@ and the derived files autonum.ins,
%\end{macro}
%\begin{macro}{\autonum@changeEnvironment}
-% Now change the invoronment.
+% Now change the environment. This command only supports displayed equation structures and is not suited for other environments (as e.g. figures).
+% \begin{macrocode}
+\def\autonum@changeEnvironment#1{%
+% \end{macrocode}
+ % Although Amsmath's environment is executed twice (for measuring and for painting), the content here is executed only once.
% \begin{macrocode}
-\newcommand{\autonum@changeEnvironment}[1]{
\renewenvironment{#1}{%
- \autonum@IfReferenced{\Roman{autonum@counter}}{%
- \csuse{autonum@#1Old}%
- }{%
- \begin{#1*}%
- }%
+% \end{macrocode}
+ % Prepare the label and the newline commands and begin the displayed equation environment.
+% \begin{macrocode}
+ \autonum@saveSubcommands
+ \csuse{autonum@#1Old}%
+ \autonum@patchSubcommands{#1}%
}{%
- \autonum@IfReferenced{\Roman{autonum@counter}}{%
- \csuse{autonum@end#1Old}%
- }{%
- \end{#1*}%
% \end{macrocode}
- % The following command is also used in amsmath (see the amsmath code). It is needed to not change the spacing of the patched environment.
+ % Possibly hide the number of the last equation in the displayed equation environment, end the latter one and restore the subcommands.
% \begin{macrocode}
- \ignorespacesafterend
- }%
- \stepcounter{autonum@counter}%
+ \autonum@possiblyHideNumber
+ \csuse{autonum@end#1Old}%
+ \autonum@restoreSubcommands
}%
+% \end{macrocode}
+ % Delete the starred versions of the environment, as they sometimes lead to strange errors a long time after using the starred version. By deleting it, the error occurs at the right place.
+% \begin{macrocode}
+ \global\csundef{#1*}%
+ \global\csundef{end#1*}%
}
% \end{macrocode}
%\end{macro}
-% This is needed to not get overwritten by other packages and to use amsmath if it gets loaded after autonum. The package autonum only overwrites some commands whose name start with \cmd{\autonum}. Other commands are only patched, so the currently valid command gets called, too. So although not very polite, this behaviour seems reasonable.
+%\begin{macro}{\autonum@saveSubcommands}
+% Save the current newline and label commands.
% \begin{macrocode}
-\AfterPreamble{
- \autonum@patchEnvironment{equation}
+\def\autonum@saveSubcommands{%
+ \let\autonum@labelNormal\label%
+ \let\autonum@newlineNormal\\%
+}
% \end{macrocode}
- % These definitions do not work at the moment and the code has to be rethought anyway, because multiple labels are possible.
+%\end{macro}
+
+%\begin{macro}{\autonum@patchSubcommands}
+% Patch the label command, as some special data has to be saved with each usage. In order to support multi-line equations, the counter must be increased in every line, as every line is a possible reference target. Therefore, \\ has to be overwritten, too. This must be global, as amsmath is very annoying with overwriting local definitions, e.g. in align environments.
% \begin{macrocode}
-%% \forcsvlist{\autonum@patchEnvironment}{gather,multline,align.flalign,alignat}
+\def\autonum@patchSubcommands#1{%
+ \global\letcs{\label}{autonum@patched#1Label}%
% \end{macrocode}
-
- % As equation* is only available when using amsmath, define it otherwise, because it is needed in \cmd{\autonum@patchEnvironment}. If amsmath is used, is only known after the preamble.
+ % Do not patch the newline command in a multline environment, as only the last line may get a \cmd{\notag} command, because all lines basically build one equation (see also amsmath's documentation, section 3.3).
% \begin{macrocode}
- \ifcsundef{equation*}{%
- \newenvironment{equation*}{\[}{\]}%
- }{}
+ \ifstrequal{#1}{multline}{%
+ }{%
+ \global\letcs{\\}{autonum@patched#1Newline}%
+ }%
+}
% \end{macrocode}
+%\end{macro}
-%\begin{macro}{\autonum@IfReferenced}
- % Add a new if condition expecting a label. If the label is referenced, the second argument is expanded, otherwise the third one.
+%\begin{macro}{\autonum@restoreSubcommands}
+% Restore the newline and label commands. This must be global, as it had been overwritten globally in \cmd{\autonum@patchSubcommands}.
% \begin{macrocode}
- \newcommand{\autonum@IfReferenced}[3]{%
- \ifcsdef{autonum@#1Referenced}{#2}{#3}%
- }
+\def\autonum@restoreSubcommands{%
+ \global\let\label\autonum@labelNormal%
+ \global\let\\\autonum@newlineNormal%
+}
% \end{macrocode}
%\end{macro}
-%\begin{macro}{\autonum@patchLabel}
- % To patch a variably named label command, generate an extra command for this.
+%\begin{macro}{\autonum@generatePatchedLabel}
+% Use an extra command to patch the used label command for efficiency.
% \begin{macrocode}
- \newcommand{\autonum@patchLabel}[1]{%
- \letcs{\autonum@labelOld}{#1}%
- \csdef{#1}##1{%
+\def\autonum@generatePatchedLabel#1{%
+ \csdef{autonum@patched#1Label}##1{%
+ % \end{macrocode}
+ % The labeling information is needed in the newline command. Therefore, the following variable is used to store it until the next newline command. As the definition is local and every line in an multi-line displayed math environment has its own group, the variable does not have to be deleted explicitly.
+ % \begin{macrocode}
+ \ifdef{\autonum@currentLabel}{%
+ \PackageError{autonum}{Two succeeding \string\label's detected}{Did you forget a \string\\?}%
+ }{%
+ \def\autonum@currentLabel{##1}%
+ }%
% \end{macrocode}
- % Provide a way to get the current counter value when a reference is used, as then only the label is available. Also defining the command saves one compilation run, although still up to three are needed to get everything right.
+ % Only call the original label command if the label gets referenced. This obviously is identical if the reference is located before the label. It is also identical if the reference is located after the label, as the \cmd{\\} or \cmd{\endenvironment} commands which follow the \cmd{\label} would suppress the numbering anyway in the first pass. In the second pass, the information about referencing is the same as if only the content of the following if-command were available.
% \begin{macrocode}
- \csxdefall{autonum@##1Labeled}{\Roman{autonum@counter}}%
+ \ifcsedef{autonum@##1Referenced}{%
% \end{macrocode}
- % Call the old label command.
+ % The environment's original label command is called to do the real labeling. As it checks for erroneous succeeding labels using \cmd{df@label}, this variable has to be emptied before every call.
% \begin{macrocode}
- \autonum@labelOld{##1}%
- }%
+ \let\df@label\@empty%
+ \csuse{autonum@label#1}{##1}%
+ }{}
+ }%
+}
+% \end{macrocode}
+%\end{macro}
+
+%\begin{macro}{\autonum@generatePatchedNewline}
+% This command generates patched newline commands for displayed math environments, so that they can simply be activated when needed.
+% \begin{macrocode}
+\def\autonum@generatePatchedNewline#1{%
+ \csdef{autonum@patched#1Newline}{%
+ \autonum@possiblyHideNumber
+ \csuse{autonum@newline#1}%
}
+}
% \end{macrocode}
- % Amsmath regularly overwrites the \cmd{\label} command, so use \cmd{\label@in@display} instead of label, if \href{http://newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2005-11/msg00074.html}{amsmath is loaded}.
+%\end{macro}
+
+%\begin{macro}{\autonum@possiblyHideNumber}
+% Define this command, which can hide the current line's number if the label is not referenced.
+% \begin{macrocode}
+\def\autonum@possiblyHideNumber{
+ \ifdef{\autonum@currentLabel}{%
+ \ifcsedef{autonum@\csuse{autonum@currentLabel}Referenced}{%
+ }{%
+ \notag%
+ }
+% \end{macrocode}
+ % The current label does not have to be cleaned, as every line is a separate cell \href{http://tex.stackexchange.com/q/58190}{defining a local group} in an displayed math environment.
% \begin{macrocode}
- \ifdef{\label@in@display}{%
- \autonum@patchLabel{label@in@display}%
}{%
- \autonum@patchLabel{label}%
- }
+ \notag%
+ }%
+}
% \end{macrocode}
%\end{macro}
-%\begin{macro}{\autonum@patchReference}
- % To patch a variably named reference command, generate an extra command.
+%\begin{macro}{\autonum@generatePatchedReference}
+% This command can patch arbitrary reference commands. The patch logic can be different to patching the label command, as the references have to be patched only once, so optimizing for speed is counter-productive.
% \begin{macrocode}
- \newcommand{\autonum@patchReference}[1]{%
- \letcs{\autonum@referenceOld}{#1}%
- \csdef{#1}##1{%
+\def\autonum@generatePatchedReference#1{%
+ \csletcs{autonum@reference#1Old}{#1}%
+ \csdef{#1}##1{%
% \end{macrocode}
- % Mark label as used. As we can have a reference before and after the \cmd{\label} call, save it as an variable (for the current run) and in the aux file (for the next run). This saves one compilation run, although still up to three are needed to get everything right.
+ % Mark label as referenced. The reference information is stored into a variable (for the current run) and into the aux file (for the next run), so it does not matter if the reference is used before or after the definition of the label. Saving into a variable saves one compilation run, although still up to three are needed to get everything right.
% \begin{macrocode}
- \ifcsdef{autonum@##1Labeled}{%
- \csxdefall{autonum@\csuse{autonum@##1Labeled}Referenced}{}%
- }{}%
+ \csxdefall{autonum@##1Referenced}{}%
% \end{macrocode}
- % Call the old reference command.
+ % Call the old reference command.
% \begin{macrocode}
- \autonum@referenceOld{##1}%
+ \csuse{autonum@reference#1Old}{##1}%
+ }%
+}
+% \end{macrocode}
+%\end{macro}
+
+%\begin{macro}{\autonum@patchShortcutEnvironment}
+% Use a counter to numerate all \cmd{\[}-\cmd{\]}-environments linearly.
+% \begin{macrocode}
+\newcounter{autonum@counter}
+% \end{macrocode}
+% \cmd{\[} and \cmd{\]} are redefined as the correct one of \texttt{equation} and \texttt{align}. Due to the improved numbering, the old environment's capabilities are basically a subset of the new capabilities.
+% \begin{macrocode}
+\def\autonum@patchShortcutEnvironment{%
+ \def\[##1\]{%
+% \end{macrocode}
+ % This command checks if the current environment only consists of one line without counting lines in sub-environments. The default will result in an align environment, as incorrectly using an equation instead of an correct align results in a compile error.
+% \begin{macrocode}
+ \ifcsedef{autonum@\Roman{autonum@counter}HasExactlyOneLine}{%
+ \autonum@useWithMultipleLineDetection{equation}{##1}%
+ }{%
+ \autonum@useWithMultipleLineDetection{align}{##1}%
}%
- }
+ \stepcounter{autonum@counter}%
+ }%
+}
% \end{macrocode}
- % Support the normal \cmd{\ref} command and, if available, the \cmd{\cref} command from cleveref.
+%\end{macro}
+
+%\begin{macro}{\autonum@useWithMultipleLineDetection}
+% This function uses an environment defined by the first argument to display the content given in the second argument. A multiple-line detection is activated, to set a variable if more than one line is used.
% \begin{macrocode}
- \autonum@patchReference{ref}%
- \ifdef{\cref}{%
- \autonum@patchReference{cref}%
+\def\autonum@useWithMultipleLineDetection#1#2{%
+ \begin{#1}%
+ \autonum@patchParentheses
+% \end{macrocode}
+ % Use global as this is in the middle of the first local group of a math environment.
+% \begin{macrocode}
+ \global\let\autonum@patchedNewline\\%
+% \end{macrocode}
+ % Set the multipleLines variable if a newline is used. Do not use the newline for equations, as this results in "There's no line here to end" errors. This is ok, as if there is a newline in an environment, which is currently an equation, it is wrong anyway and should be set as an align environment. For that it is enough to set the multipleLines variable.
+% \begin{macrocode}
+ \ifstrequal{#1}{align}{%
+ \gdef\\{%
+ \autonum@patchedNewline
+ \gdef\autonum@multipleLines{}%
+ }%
+ }{%
+ \gdef\\{%
+ \gdef\autonum@multipleLines{}%
+ }%
+ }
+% \end{macrocode}
+ % Set the environment's content and reset the newline command.
+% \begin{macrocode}
+ #2%
+ \global\let\\\autonum@patchedNewline%
+% \end{macrocode}
+ % Store information if the current math environment. The roman number is used, as there might be no label and if there is one, it might not be available at the beginning of the environment. Delete the multipleLines variable, to avoid influencing the next \cmd{\[}-\cmd{\]}-environment, as the variable must be global.
+% \begin{macrocode}
+ \ifdef{\autonum@multipleLines}{%
+ \global\undef{\autonum@multipleLines}%
+ }{%
+ \csxdefaux{autonum@\Roman{autonum@counter}HasExactlyOneLine}{\Roman{autonum@counter}}%
+ }%
+ \autonum@restoreParentheses
+ \end{#1}%
+}
+% \end{macrocode}
+%\end{macro}
+
+%\begin{macro}{\autonum@patchParentheses}
+% This function patches the left and the right parentheses.
+% \begin{macrocode}
+\global\def\autonum@patchParentheses{%
+ \autonum@patchParenthesis{(}{Left}{}%
+ \autonum@patchParenthesis{)}{Right}{end}%
+}
+% \end{macrocode}
+%\end{macro}
+
+%\begin{macro}{\autonum@patchParenthesis}
+% This function patches a parenthesis given in the first argument with a name partly given in the second argument by using the third argument.
+% \begin{macrocode}
+\global\def\autonum@patchParenthesis#1#2#3{%
+ \ifcsdef{#1}{%
+ \global\csletcs{autonum@old#2Parenthesis}{#1}%
}{}%
+ \global\csletcs{#1}{#3split}%
+}
% \end{macrocode}
%\end{macro}
+
+%\begin{macro}{\autonum@restoreParentheses}
+% This function restores the left and the right parentheses.
+% \begin{macrocode}
+\global\def\autonum@restoreParentheses{%
+ \autonum@restoreParenthesis{(}{Left}%
+ \autonum@restoreParenthesis{)}{Right}%
+}
+% \end{macrocode}
+%\end{macro}
+
+%\begin{macro}{\autonum@restoreParenthesis}
+% This function restores a parenthesis given in the first argument with the name given in the second argument.
% \begin{macrocode}
+\global\def\autonum@restoreParenthesis#1#2{%
+ \ifcsdef{autonum@old#2Parenthesis}{%
+ \global\csletcs{#1}{autonum@old#2Parenthesis}%
+ \global\csundef{autonum@old#2Parenthesis}%
+ }{}%
}
% \end{macrocode}
+%\end{macro}
%\StopEventually{^^A
% \PrintChanges
diff --git a/Master/texmf-dist/source/latex/autonum/autonum.ins b/Master/texmf-dist/source/latex/autonum/autonum.ins
index 623c434f7cb..fbac37d109c 100644
--- a/Master/texmf-dist/source/latex/autonum/autonum.ins
+++ b/Master/texmf-dist/source/latex/autonum/autonum.ins
@@ -50,27 +50,67 @@ and the derived files autonum.ins,
}
\endbatchfile
-\newcommand{\csxdefaux}[2]{%
+\def\csxdefaux#1#2{%
\protected@write\@mainaux{}{%
\expandafter\string\expandafter\xdef\expandafter\string\csname #1\endcsname{#2}%
}%
}
-\newcommand{\csxdefall}[2]{%
+\def\csxdefall#1#2{%
\csxdefaux{#1}{#2}%
\csxdef{#1}{#2}%
}
-\newcounter{autonum@counter}
+\def\ifcsedef#1#2#3{%
+\edef\autonum@ifcsedefTemp{#1}%
+\expandafter\ifcsdef\expandafter{\autonum@ifcsedefTemp}{#2}{#3}%
+\undef{\autonum@ifcsedefTemp}%
+}
-\newcommand{\autonum@patchEnvironment}[1]{
-\csletcs{autonum@#1Old}{#1}%
-\csletcs{autonum@end#1Old}{end#1}%
-\autonum@saveOldEnvironment{#1}%
+\AtBeginDocument{%
+\forcsvlist{\autonum@patchFullEnvironment}{equation,gather,multline,align}%
+\def\[#1\]{%
+\begin{equation}#1\end{equation}%
+}%
+\autonum@generatePatchedReference{ref}%
+\ifdef{\cref}{%
+\autonum@generatePatchedReference{cref}%
+}{}%
+}
+
+\def\autonum@patchFullEnvironment#1{%
+\autonum@saveEnvironmentSubcommands{#1}{center}%
+\autonum@patchEnvironmentHelper{#1}%
+}
+\def\autonum@patchBlockEnvironment#1{%
+\autonum@saveEnvironmentSubcommands{#1}{equation*}%
+\autonum@patchEnvironmentHelper{#1}%
+}
+\def\autonum@patchEnvironmentHelper#1{%
+\autonum@renameEnvironment{#1}%
\autonum@changeEnvironment{#1}%
+\autonum@generatePatchedLabel{#1}%
+\autonum@generatePatchedNewline{#1}%
}
-\newcommand{\autonum@saveOldEnvironment}[1]{
+\def\autonum@saveEnvironmentSubcommands#1#2{%
+\begin{textblock}{1}[1,1](0,0)%
+\begin{#2}
+\begin{#1}%
+\global\cslet{autonum@newline#1}\\%
+\notag%
+\ifboolexpr{not test {\ifstrequal{#1}{multline}} or test {\ifcsundef{autonum@label#1}}}{%
+\global\cslet{autonum@label#1}{\label}%
+}%
+\notag%
+\end{#1}%
+\end{#2}
+\end{textblock}%
+}
+
+\def\autonum@renameEnvironment#1{%
+\csletcs{autonum@#1Old}{#1}%
+\csletcs{autonum@end#1Old}{end#1}%
\newenvironment{#1+}{%
\csuse{autonum@#1Old}%
}{%
@@ -78,61 +118,136 @@ and the derived files autonum.ins,
}%
}
-\newcommand{\autonum@changeEnvironment}[1]{
+\def\autonum@changeEnvironment#1{%
\renewenvironment{#1}{%
-\autonum@IfReferenced{\Roman{autonum@counter}}{%
+\autonum@saveSubcommands
\csuse{autonum@#1Old}%
+\autonum@patchSubcommands{#1}%
}{%
-\begin{#1*}%
-}%
-}{%
-\autonum@IfReferenced{\Roman{autonum@counter}}{%
+\autonum@possiblyHideNumber
\csuse{autonum@end#1Old}%
-}{%
-\end{#1*}%
-\ignorespacesafterend
+\autonum@restoreSubcommands
}%
-\stepcounter{autonum@counter}%
+\global\csundef{#1*}%
+\global\csundef{end#1*}%
+}
+
+\def\autonum@saveSubcommands{%
+\let\autonum@labelNormal\label%
+\let\autonum@newlineNormal\\%
+}
+
+\def\autonum@patchSubcommands#1{%
+\global\letcs{\label}{autonum@patched#1Label}%
+\ifstrequal{#1}{multline}{%
+}{%
+ \global\letcs{\\}{autonum@patched#1Newline}%
}%
}
-\AfterPreamble{
-\autonum@patchEnvironment{equation}
-%% \forcsvlist{\autonum@patchEnvironment}{gather,multline,align.flalign,alignat}
+\def\autonum@restoreSubcommands{%
+\global\let\label\autonum@labelNormal%
+\global\let\\\autonum@newlineNormal%
+}
-\ifcsundef{equation*}{%
-\newenvironment{equation*}{\[}{\]}%
+\def\autonum@generatePatchedLabel#1{%
+\csdef{autonum@patched#1Label}##1{%
+\ifdef{\autonum@currentLabel}{%
+\PackageError{autonum}{Two succeeding \string\label's detected}{Did you forget a \string\\?}%
+}{%
+\def\autonum@currentLabel{##1}%
+}%
+\ifcsedef{autonum@##1Referenced}{%
+\let\df@label\@empty%
+\csuse{autonum@label#1}{##1}%
}{}
+}%
+}
+
+\def\autonum@generatePatchedNewline#1{%
+\csdef{autonum@patched#1Newline}{%
+\autonum@possiblyHideNumber
+\csuse{autonum@newline#1}%
+}
+}
-\newcommand{\autonum@IfReferenced}[3]{%
-\ifcsdef{autonum@#1Referenced}{#2}{#3}%
+\def\autonum@possiblyHideNumber{
+\ifdef{\autonum@currentLabel}{%
+\ifcsedef{autonum@\csuse{autonum@currentLabel}Referenced}{%
+}{%
+\notag%
+}
+}{%
+\notag%
+}%
}
-\newcommand{\autonum@patchLabel}[1]{%
-\letcs{\autonum@labelOld}{#1}%
+\def\autonum@generatePatchedReference#1{%
+\csletcs{autonum@reference#1Old}{#1}%
\csdef{#1}##1{%
-\csxdefall{autonum@##1Labeled}{\Roman{autonum@counter}}%
-\autonum@labelOld{##1}%
+\csxdefall{autonum@##1Referenced}{}%
+\csuse{autonum@reference#1Old}{##1}%
}%
}
-\ifdef{\label@in@display}{%
-\autonum@patchLabel{label@in@display}%
+
+\newcounter{autonum@counter}
+\def\autonum@patchShortcutEnvironment{%
+\def\[##1\]{%
+\ifcsedef{autonum@\Roman{autonum@counter}HasExactlyOneLine}{%
+\autonum@useWithMultipleLineDetection{equation}{##1}%
}{%
-\autonum@patchLabel{label}%
+\autonum@useWithMultipleLineDetection{align}{##1}%
+}%
+\stepcounter{autonum@counter}%
+}%
}
-\newcommand{\autonum@patchReference}[1]{%
-\letcs{\autonum@referenceOld}{#1}%
-\csdef{#1}##1{%
-\ifcsdef{autonum@##1Labeled}{%
-\csxdefall{autonum@\csuse{autonum@##1Labeled}Referenced}{}%
-}{}%
-\autonum@referenceOld{##1}%
+\def\autonum@useWithMultipleLineDetection#1#2{%
+\begin{#1}%
+\autonum@patchParentheses
+\global\let\autonum@patchedNewline\\%
+\ifstrequal{#1}{align}{%
+\gdef\\{%
+\autonum@patchedNewline
+\gdef\autonum@multipleLines{}%
+}%
+}{%
+\gdef\\{%
+\gdef\autonum@multipleLines{}%
}%
}
-\autonum@patchReference{ref}%
-\ifdef{\cref}{%
-\autonum@patchReference{cref}%
+#2%
+\global\let\\\autonum@patchedNewline%
+\ifdef{\autonum@multipleLines}{%
+\global\undef{\autonum@multipleLines}%
+}{%
+\csxdefaux{autonum@\Roman{autonum@counter}HasExactlyOneLine}{\Roman{autonum@counter}}%
+}%
+\autonum@restoreParentheses
+\end{#1}%
+}
+
+\global\def\autonum@patchParentheses{%
+\autonum@patchParenthesis{(}{Left}{}%
+\autonum@patchParenthesis{)}{Right}{end}%
+}
+
+\global\def\autonum@patchParenthesis#1#2#3{%
+\ifcsdef{#1}{%
+\global\csletcs{autonum@old#2Parenthesis}{#1}%
+}{}%
+\global\csletcs{#1}{#3split}%
+}
+
+\global\def\autonum@restoreParentheses{%
+\autonum@restoreParenthesis{(}{Left}%
+\autonum@restoreParenthesis{)}{Right}%
+}
+
+\global\def\autonum@restoreParenthesis#1#2{%
+\ifcsdef{autonum@old#2Parenthesis}{%
+\global\csletcs{#1}{autonum@old#2Parenthesis}%
+\global\csundef{autonum@old#2Parenthesis}%
}{}%
}
diff --git a/Master/texmf-dist/tex/latex/autonum/autonum.sty b/Master/texmf-dist/tex/latex/autonum/autonum.sty
index 303aa4f89c7..cb9833848fe 100644
--- a/Master/texmf-dist/tex/latex/autonum/autonum.sty
+++ b/Master/texmf-dist/tex/latex/autonum/autonum.sty
@@ -13,30 +13,73 @@
%% ----------------------------------------------------------------
%%
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{autonum}[2012/05/26 v0.1 autonum LaTeX package]
+\ProvidesPackage{autonum}[2012/06/21 v0.2 autonum LaTeX package]
+\PassOptionsToPackage{hypertexnames=false}{hyperref}
\RequirePackage{etoolbox}
+\RequirePackage{amsmath}
+\RequirePackage[absolute]{textpos}
-\newcommand{\csxdefaux}[2]{%
+\def\csxdefaux#1#2{%
\protected@write\@mainaux{}{%
\expandafter\string\expandafter\xdef\expandafter\string\csname #1\endcsname{#2}%
}%
}
-\newcommand{\csxdefall}[2]{%
+\def\csxdefall#1#2{%
\csxdefaux{#1}{#2}%
\csxdef{#1}{#2}%
}
-\newcounter{autonum@counter}
+\def\ifcsedef#1#2#3{%
+\edef\autonum@ifcsedefTemp{#1}%
+\expandafter\ifcsdef\expandafter{\autonum@ifcsedefTemp}{#2}{#3}%
+\undef{\autonum@ifcsedefTemp}%
+}
-\newcommand{\autonum@patchEnvironment}[1]{
-\csletcs{autonum@#1Old}{#1}%
-\csletcs{autonum@end#1Old}{end#1}%
-\autonum@saveOldEnvironment{#1}%
+\AtBeginDocument{%
+\forcsvlist{\autonum@patchFullEnvironment}{equation,gather,multline,align}%
+\def\[#1\]{%
+\begin{equation}#1\end{equation}%
+}%
+\autonum@generatePatchedReference{ref}%
+\ifdef{\cref}{%
+\autonum@generatePatchedReference{cref}%
+}{}%
+}
+
+\def\autonum@patchFullEnvironment#1{%
+\autonum@saveEnvironmentSubcommands{#1}{center}%
+\autonum@patchEnvironmentHelper{#1}%
+}
+\def\autonum@patchBlockEnvironment#1{%
+\autonum@saveEnvironmentSubcommands{#1}{equation*}%
+\autonum@patchEnvironmentHelper{#1}%
+}
+\def\autonum@patchEnvironmentHelper#1{%
+\autonum@renameEnvironment{#1}%
\autonum@changeEnvironment{#1}%
+\autonum@generatePatchedLabel{#1}%
+\autonum@generatePatchedNewline{#1}%
}
-\newcommand{\autonum@saveOldEnvironment}[1]{
+\def\autonum@saveEnvironmentSubcommands#1#2{%
+\begin{textblock}{1}[1,1](0,0)%
+\begin{#2}
+\begin{#1}%
+\global\cslet{autonum@newline#1}\\%
+\notag%
+\ifboolexpr{not test {\ifstrequal{#1}{multline}} or test {\ifcsundef{autonum@label#1}}}{%
+\global\cslet{autonum@label#1}{\label}%
+}%
+\notag%
+\end{#1}%
+\end{#2}
+\end{textblock}%
+}
+
+\def\autonum@renameEnvironment#1{%
+\csletcs{autonum@#1Old}{#1}%
+\csletcs{autonum@end#1Old}{end#1}%
\newenvironment{#1+}{%
\csuse{autonum@#1Old}%
}{%
@@ -44,61 +87,136 @@
}%
}
-\newcommand{\autonum@changeEnvironment}[1]{
+\def\autonum@changeEnvironment#1{%
\renewenvironment{#1}{%
-\autonum@IfReferenced{\Roman{autonum@counter}}{%
+\autonum@saveSubcommands
\csuse{autonum@#1Old}%
+\autonum@patchSubcommands{#1}%
}{%
-\begin{#1*}%
-}%
-}{%
-\autonum@IfReferenced{\Roman{autonum@counter}}{%
+\autonum@possiblyHideNumber
\csuse{autonum@end#1Old}%
-}{%
-\end{#1*}%
-\ignorespacesafterend
+\autonum@restoreSubcommands
}%
-\stepcounter{autonum@counter}%
+\global\csundef{#1*}%
+\global\csundef{end#1*}%
+}
+
+\def\autonum@saveSubcommands{%
+\let\autonum@labelNormal\label%
+\let\autonum@newlineNormal\\%
+}
+
+\def\autonum@patchSubcommands#1{%
+\global\letcs{\label}{autonum@patched#1Label}%
+\ifstrequal{#1}{multline}{%
+}{%
+ \global\letcs{\\}{autonum@patched#1Newline}%
}%
}
-\AfterPreamble{
-\autonum@patchEnvironment{equation}
-%% \forcsvlist{\autonum@patchEnvironment}{gather,multline,align.flalign,alignat}
+\def\autonum@restoreSubcommands{%
+\global\let\label\autonum@labelNormal%
+\global\let\\\autonum@newlineNormal%
+}
-\ifcsundef{equation*}{%
-\newenvironment{equation*}{\[}{\]}%
+\def\autonum@generatePatchedLabel#1{%
+\csdef{autonum@patched#1Label}##1{%
+\ifdef{\autonum@currentLabel}{%
+\PackageError{autonum}{Two succeeding \string\label's detected}{Did you forget a \string\\?}%
+}{%
+\def\autonum@currentLabel{##1}%
+}%
+\ifcsedef{autonum@##1Referenced}{%
+\let\df@label\@empty%
+\csuse{autonum@label#1}{##1}%
}{}
+}%
+}
+
+\def\autonum@generatePatchedNewline#1{%
+\csdef{autonum@patched#1Newline}{%
+\autonum@possiblyHideNumber
+\csuse{autonum@newline#1}%
+}
+}
-\newcommand{\autonum@IfReferenced}[3]{%
-\ifcsdef{autonum@#1Referenced}{#2}{#3}%
+\def\autonum@possiblyHideNumber{
+\ifdef{\autonum@currentLabel}{%
+\ifcsedef{autonum@\csuse{autonum@currentLabel}Referenced}{%
+}{%
+\notag%
+}
+}{%
+\notag%
+}%
}
-\newcommand{\autonum@patchLabel}[1]{%
-\letcs{\autonum@labelOld}{#1}%
+\def\autonum@generatePatchedReference#1{%
+\csletcs{autonum@reference#1Old}{#1}%
\csdef{#1}##1{%
-\csxdefall{autonum@##1Labeled}{\Roman{autonum@counter}}%
-\autonum@labelOld{##1}%
+\csxdefall{autonum@##1Referenced}{}%
+\csuse{autonum@reference#1Old}{##1}%
}%
}
-\ifdef{\label@in@display}{%
-\autonum@patchLabel{label@in@display}%
+
+\newcounter{autonum@counter}
+\def\autonum@patchShortcutEnvironment{%
+\def\[##1\]{%
+\ifcsedef{autonum@\Roman{autonum@counter}HasExactlyOneLine}{%
+\autonum@useWithMultipleLineDetection{equation}{##1}%
}{%
-\autonum@patchLabel{label}%
+\autonum@useWithMultipleLineDetection{align}{##1}%
+}%
+\stepcounter{autonum@counter}%
+}%
}
-\newcommand{\autonum@patchReference}[1]{%
-\letcs{\autonum@referenceOld}{#1}%
-\csdef{#1}##1{%
-\ifcsdef{autonum@##1Labeled}{%
-\csxdefall{autonum@\csuse{autonum@##1Labeled}Referenced}{}%
-}{}%
-\autonum@referenceOld{##1}%
+\def\autonum@useWithMultipleLineDetection#1#2{%
+\begin{#1}%
+\autonum@patchParentheses
+\global\let\autonum@patchedNewline\\%
+\ifstrequal{#1}{align}{%
+\gdef\\{%
+\autonum@patchedNewline
+\gdef\autonum@multipleLines{}%
+}%
+}{%
+\gdef\\{%
+\gdef\autonum@multipleLines{}%
}%
}
-\autonum@patchReference{ref}%
-\ifdef{\cref}{%
-\autonum@patchReference{cref}%
+#2%
+\global\let\\\autonum@patchedNewline%
+\ifdef{\autonum@multipleLines}{%
+\global\undef{\autonum@multipleLines}%
+}{%
+\csxdefaux{autonum@\Roman{autonum@counter}HasExactlyOneLine}{\Roman{autonum@counter}}%
+}%
+\autonum@restoreParentheses
+\end{#1}%
+}
+
+\global\def\autonum@patchParentheses{%
+\autonum@patchParenthesis{(}{Left}{}%
+\autonum@patchParenthesis{)}{Right}{end}%
+}
+
+\global\def\autonum@patchParenthesis#1#2#3{%
+\ifcsdef{#1}{%
+\global\csletcs{autonum@old#2Parenthesis}{#1}%
+}{}%
+\global\csletcs{#1}{#3split}%
+}
+
+\global\def\autonum@restoreParentheses{%
+\autonum@restoreParenthesis{(}{Left}%
+\autonum@restoreParenthesis{)}{Right}%
+}
+
+\global\def\autonum@restoreParenthesis#1#2{%
+\ifcsdef{autonum@old#2Parenthesis}{%
+\global\csletcs{#1}{autonum@old#2Parenthesis}%
+\global\csundef{autonum@old#2Parenthesis}%
}{}%
}