\catcode`\@=11
\def\alignedat#1{\null\,\vcenter\bgroup\doat@{#1}\vspace@\Let@
 \ifinany@\else\openup\jot\fi\ialign\bgroup\span\preamble@@\crcr}
\def\doat@#1{\toks@{\hfil\strut@$\m@th
 \displaystyle{\the\hashtoks@}$&$\m@th\displaystyle
 {{}\the\hashtoks@}$\hfil}%
 \atcount@#1\relax\advance\atcount@\m@ne
 \loop\ifnum\atcount@>\z@\toks@=\expandafter{\the\toks@&\hfil$\m@th
 \displaystyle{\the\hashtoks@}$&$\m@th
 \displaystyle{{}\the\hashtoks@}$\hfil}\advance\atcount@\m@ne\repeat
 \edef\preamble@{\the\toks@}\edef\preamble@@{\preamble@}}
\let\endalignedat\endaligned
\def\alignat{\RIfMIfI@\DN@{\onlydmatherr@\alignat}\else
 \DN@{\csname alignat \endcsname}\fi\else
 \DN@{\onlydmatherr@\alignat}\fi\next@}
\expandafter\def\csname alignat \endcsname#1#2\endalignat
 {\inany@true\xat@false
 \def\tag{\global\tag@true
  \count@#1\relax\multiply\count@\tw@\advance\count@\m@ne
  \gdef\tag@{&}%
  \loop\ifnum\count@>\and@\xdef\tag@{&\omit\tag@}%
  \advance\count@\m@ne\repeat
  \tag@\relax}%
 \vspace@\allowdisplaybreak@\displaybreak@\intertext@
 \displ@y@\measuring@true\tag@CC\tag@C
 \setbox\savealignat@\hbox{\noset@\unlabel@$\m@th\displaystyle\Let@
  \attag@{#1}\vbox{\halign{\span\preamble@@\crcr#2\crcr}}$}%
 \measuring@false
 \Let@\attag@{#1}\tag@C\tag@CC
 \tabskip\centering@\halign to\displaywidth
  {\span\preamble@@\crcr#2\crcr\black@{\wd\savealignat@}}}
\Invalid@\endalignat
\def\xalignat{\RIfMIfI@
 \DN@{\onlydmatherr@\xalignat}\else
 \DN@{\csname xalignat \endcsname}\fi\else
 \DN@{\onlydmatherr@\xalignat}\fi\next@}
\expandafter\def\csname xalignat \endcsname#1#2\endxalignat
 {\inany@true\xat@true
 \def\tag{\global\tag@true
  \count@#1\relax\multiply\count@\tw@\advance\count@\m@ne
  \gdef\tag@{&}%
  \loop\ifnum\count@>\and@\xdef\tag@{&\omit\tag@}%
  \advance\count@\m@ne\repeat
  \tag@\relax}%
 \vspace@\allowdisplaybreak@\displaybreak@\intertext@
 \displ@y@\measuring@true\tag@CC\tag@C
 \setbox\savealignat@\hbox{\noset@\unlabel@$\m@th\displaystyle\Let@
  \attag@{#1}\vbox{\halign{\span\preamble@@\crcr#2\crcr}}$}%
 \measuring@false\Let@\attag@{#1}\tag@C\tag@CC
 \tabskip\centering@\halign to\displaywidth
 {\span\preamble@@\crcr#2\crcr\black@{\wd\savealignat@}}}
\def\attag@#1{\let\Maketag@\maketag@\let\TAG@\Tag@
 \let\Prepost@\prepost@\let\Reprepost@\reprepost@
 \let\Tag@\relax\let\maketag@\relax
 \let\prepost@\relax\let\reprepost@\relax
 \ifmeasuring@
  \def\llap@##1{\setboxz@h{##1}\hbox to\tw@\wdz@{}}%
  \def\rlap@##1{\setboxz@h{##1}\hbox to\tw@\wdz@{}}%
 \else\let\llap@\llap\let\rlap@\rlap\fi
 \toks@{\hfil\strut@
  $\m@th\displaystyle{\@lign\the\hashtoks@\prepost@}$%
  \tabskip\z@skip\global\advance\and@\@ne&
  $\m@th\displaystyle{{}\@lign\the\hashtoks@\prepost@}$\hfil
  \ifxat@\tabskip\centering@\fi\global\advance\and@\@ne}%
 \iftagsleft@
  \toks@@{\tabskip\centering@&\Tag@\kern-\displaywidth
   \rlap@{\@lign\reprepost@\maketag@\the\hashtoks@\maketag@}%
   \global\advance\and@\@ne\tabskip\displaywidth}\else
  \toks@@{\tabskip\centering@&\Tag@\llap@{\@lign\reprepost@\maketag@
   \the\hashtoks@\maketag@}\global\advance\and@\@ne\tabskip\z@skip}\fi
 \atcount@#1\relax\advance\atcount@\m@ne
 \loop\ifnum\atcount@>\z@
  \toks@\expandafter{\the\toks@&\hfil$\m@th\displaystyle{\@lign
  \the\hashtoks@\prepost@}$\global\advance\and@\@ne
  \tabskip\z@skip
  &$\m@th\displaystyle{{}\@lign\the\hashtoks@\prepost@}$\hfil\ifxat@
  \tabskip\centering@\fi\global\advance\and@\@ne}\advance\atcount@\m@ne
 \repeat
 \edef\preamble@{\the\toks@\the\toks@@}%
 \edef\preamble@@{\preamble@}%
 \let\maketag@\Maketag@\let\Tag@\TAG@
 \let\prepost@\Prepost@\let\reprepost@\Reprepost@}
\Invalid@\endxalignat
\def\xxalignat{\RIfMIfI@
 \DN@{\onlydmatherr@\xxalignat}\else\DN@{\csname xxalignat
  \endcsname}\fi\else
 \DN@{\onlydmatherr@\xxalignat}\fi\next@}
\expandafter\def\csname xxalignat \endcsname#1#2\endxxalignat{\inany@true
 \vspace@\allowdisplaybreak@\displaybreak@\intertext@
 \displ@y\setbox\savealignat@\hbox{$\m@th\displaystyle\Let@
 \xxattag@{#1}\vbox{\halign{\span\preamble@@\crcr#2\crcr}}$}%
 \Let@\xxattag@{#1}\tabskip\z@skip\halign to\displaywidth
 {\span\preamble@@\crcr#2\crcr\black@{\wd\savealignat@}}}
\def\xxattag@#1{\toks@{\tabskip\z@skip\hfil\strut@
 $\m@th\displaystyle{\the\hashtoks@}$&%
 $\m@th\displaystyle{{}\the\hashtoks@}$\hfil\tabskip\centering@&}%
 \atcount@#1\relax\advance\atcount@\m@ne\loop\ifnum\atcount@>\z@
 \toks@=\expandafter{\the\toks@&\hfil$\m@th\displaystyle{\the\hashtoks@}$%
  \tabskip\z@skip&$\m@th\displaystyle{{}\the\hashtoks@}$\hfil
  \tabskip\centering@}\advance\atcount@\m@ne\repeat
 \edef\preamble@{\the\toks@\tabskip\z@skip}\edef\preamble@@{\preamble@}}
\Invalid@\endxxalignat
\catcode`\@=\active
