latex3 / mathtools

Mathematical tools to use with amsmath
LaTeX Project Public License v1.3c
57 stars 7 forks source link

Breaks code using "aligned" #15

Closed nilsfleischhacker closed 3 years ago

nilsfleischhacker commented 3 years ago

(I originally filed this as an issue with the cryptocode package, but it turns out, mathtools is at fault.)

The following document will not compile

\documentclass{article}
\usepackage{mathtools}
\usepackage{amsmath}
\begin{document}
\begin{align*}
\begin{aligned}&\end{aligned}
\end{align*}
\end{document}

however, it compiles fine if the mathtools package is not loaded as in

\documentclass{article}
%\usepackage{mathtools}
\usepackage{amsmath}
\begin{document}
\begin{align*}
\begin{aligned}&\end{aligned}
\end{align*}
\end{document}

The issue is also fixed by adding a single whitespace as in

\documentclass{article}
\usepackage{mathtools}
\usepackage{amsmath}
\begin{document}
\begin{align*}
\begin{aligned} &\end{aligned}
\end{align*}
\end{document}

This seems like an unintended side effect of something, but I have no idea what.

PhelypeOleinik commented 3 years ago

amsmath's aligned does \let\@testopt\alignsafe@testopt, and \alignsafe@testopt is the same as the usual \@testopt except that it does (in expl3 name) \group_align_safe_begin: and \group_align_safe_end:. However mathtools's redefinition does not.

If I got it right, this should work:

\documentclass{article}
\usepackage{mathtools}
\usepackage{amsmath}
\ExplSyntaxOn
\makeatletter
\long\def\MH_nospace_ifnextchar:Nnn #1#2#3{
  \MH_group_align_safe_begin:
  \MH_let:NwN\reserved@d=~#1
  \def\reserved@a{\MH_group_align_safe_end: #2}
  \def\reserved@b{\MH_group_align_safe_end: #3}
  \futurelet\@let@token\MH_nospace_nextchar:
}
\makeatother
\ExplSyntaxOn
\begin{document}
\begin{align*}
\begin{aligned}&\end{aligned}
\end{align*}
\end{document}
nilsfleischhacker commented 3 years ago

I have to admit, I'm unsure if you're saying that the current behavior is intended or not.

PhelypeOleinik commented 3 years ago

Sorry, my sloppy writing. I mean that it looks like mathtools overlooked this case from amsmath, so it seems like a mathtools bug to me (with the fix proposed in my previous comment).

daleif commented 3 years ago

Should now be fixed in mhsetup.dtx using @PhelypeOleinik's suggestion.