# Misplaced \noalign: mathtools's SwapAboveDisplaySkip does not work with autonum package

I would like to use \SwapAboveDisplaySkip from the mathtools package to fix the vertical space above align environments. Unfortunately, this does not seem to work when loading the autonum package:

! Misplaced \noalign. \SwapAboveDisplaySkip ->\noalign

autonum needs to be loaded because I would like to label equations only if they are referenced (not shown in the MWE, but required in the document) and the mathtools option does not work when loading the cleveref package.

Here is an MWE that produces the described error. Removing either \usepackage{autonum} or \SwapAboveDisplaySkip makes the error go away.

\documentclass{scrreprt}

\usepackage{mathtools}
\usepackage{cleveref}
\usepackage{autonum}

\begin{document}

\chapter{Chapter}
\section{Section}

Text
\begin{align}
\SwapAboveDisplaySkip
A &= B
\end{align}

\end{document}

• The autonum package inserts some assignments just after the original code executed for align, which makes \noalign appear in a wrong place, because those assignments are executed in the first cell. – egreg Dec 11 '17 at 18:09

The autonum package patches align and the other environments to do something (dangerous) just after the alignment is started, so the trick used by mathtools becomes impossible.

Technically, the code inserted by autonum is performed in the first cell of the alignment, but \SwapAboveDisplayskip must be the first item, because it uses \noalign. Actually it has not to be the first item, provided it is only preceded by other \noalign items.

\documentclass{scrreprt}

\usepackage{mathtools}
\usepackage{cleveref}
\usepackage{autonum}

\makeatletter
% fix autonum code
\newtoggle{autonum@equation@toggle}
\def\autonum@startChangeEnvironment#1#2{%
\ifstrequal{#1}{split}{%
\GlobalLetLtxMacro\autonum@outerMathEnvironmentLabel\label
}{%
\autonum@saveSubcommands
}%
\ifstrequal{#1}{equation}{%
\global\toggletrue{autonum@equation@toggle}%
}{%
\global\togglefalse{autonum@equation@toggle}%
}%
\csuse{autonum@#1Old}#2%
\iftoggle{autonum@equation@toggle}{}{\noalign\bgroup}%
\ifstrequal{#1}{split}{%
\GlobalLetLtxMacro\label\autonum@outerMathEnvironmentLabel
}{%
\autonum@patchSubcommands{#1}%
}%
\iftoggle{autonum@equation@toggle}{}{\egroup}%
}
\makeatother

\begin{document}

\chapter{Chapter}
\section{Section}

Text Text Text Text Text
$$A = B$$
Text Text Text Text Text
\begin{align}
\SwapAboveDisplaySkip
A &= B
\end{align}
Text Text Text Text Text
\begin{align}
A &= B
\end{align}

\end{document}


• Thank you very much. Unfortunately, this breaks when adding Text Text Text Text Text $$A = B$$ right before the end of the document (! Misplaced \noalign. \autonum@startChangeEnvironment ...Old}#2\noalign). – user61658 Dec 12 '17 at 9:37
• @jpmath The new version works also with equation (the \noalign is inserted conditionally). Please, do other tests and feel free to ask again if something goes wrong. – egreg Dec 12 '17 at 9:50
• I have not done thorough testing, but using it in a very large document it seemed to work fine. You probably don't know a way to automate the short skips before align either (tex.stackexchange.com/a/385787/61658) do you? – user61658 Dec 12 '17 at 9:57
• @Patrick Häcker Do you think it would be possible to implement that into your package? – user61658 Dec 12 '17 at 10:41
• @jpmath I sent mail to the author of autonum about this. – egreg Dec 12 '17 at 10:48

No problem, of course, using the showonlyrefs boolean from mathtools:

\documentclass{scrreprt}

\usepackage{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage{cleveref}

\begin{document}

\chapter{Chapter}
\section{Section}

Text text text text text text%\useshortskip
\begin{align}
\SwapAboveDisplaySkip
A &= B
\end{align}

More text More text
\begin{align}
A &= B
\end{align}
Still more text

\end{document}


If you really need to use autonum, you can use the \useshortskip from nccmath in the place of \SwapAboveDisplayskip, like this:

\documentclass{scrreprt}

\usepackage{mathtools, nccmath}
\usepackage{cleveref}
\usepackage{autonum}

\begin{document}

\chapter{Chapter}
\section{Section}

Text text text text text text \useshortskip
\begin{align}
A &= B
\end{align}

More text More text
\begin{align}
A &= B
\end{align}
Still more text

\end{document}


• The reason for autonum is to straighten up some difficulties with showonlyrefs. – egreg Dec 11 '17 at 19:20
• I didn't know that. In this case, \useshortskip from nccmath seems to be compatible with autonum. – Bernard Dec 11 '17 at 19:31
• \useshortskip works very well, despite the need of an additional package - which is why I tend to prefer egregs solution if it can be fixed. Too bad that the short skips before align environments can not be automated (tex.stackexchange.com/a/385787/61658). – user61658 Dec 12 '17 at 9:46
• @jpmath: One may ask whether it's better to have a longer preamble (or a personal macros file), or to load one more package. On the other hand, nccmathdefines some other goodies, like very useful medium size math macros (\mfrac, \mint, ...) and environments, an optional argument for \intertextto control the vertical spacing of this command. – Bernard Dec 12 '17 at 10:25
• I totally agree, this decision should be made on a case-by-case basis. I had a look into nccmath and noticed that I currently would have no usage for it besides from \useshortskip. Hence I opted for the macro. – user61658 Dec 12 '17 at 10:45