% \iffalse meta-comment
%
%% File: latex-lab-mathtools.dtx
%
% Copyright (C) 2022-2026 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
%
% The latex-lab bundle is developed in the LaTeX2e GitHub.
% Issues may be reported at
%
%    https://github.com/latex3/latex2e/issues
%
\def\ltlabmathtoolsdate{2026-04-25}
\def\ltlabmathtoolsversion{0.80e}
%<*driver>
\DocumentMetadata{tagging=on,pdfstandard=ua-2}
\documentclass[kernel]{l3in2edoc}

\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-mathtools.dtx}
\end{document}
%</driver>
%
% \fi
%
%
% \title{The \texttt{latex-lab-mathtools} code\thanks{}}
% \author{\LaTeX{} Project}
% \date{v\ltlabmathtoolsversion\ \ltlabmathtoolsdate}
% \maketitle
%
% \newcommand\fmi[1]{\begin{quote} TODO: \itshape #1\end{quote}}
% \newcommand\NEW[1]{\marginpar{\mbox{}\hfill\fbox{New: #1}}}
% \providecommand\class[1]{\texttt{#1.cls}}
% \providecommand\pkg[1]{\texttt{#1}}
% \providecommand\hook[1]{\texttt{#1}}
%
% \begin{abstract}
% \end{abstract}
%
% \tableofcontents
%
% \section{Introduction}
%
% This file implements adaptions to the \pkg{mathtools} package needed for 
% the tagging project. 
%
% \MaybeStop{\setlength\IndexMin{200pt}  \PrintIndex  }
%
% \section{The Implementation}
%
%    \begin{macrocode}
%<@@=math>
%    \end{macrocode}
%
%    \begin{macrocode}
%<*kernel>
%    \end{macrocode}
%
% \subsection{File declaration}
%    \begin{macrocode}
\ProvidesFile{latex-lab-mathtools.ltx}
        [\ltlabmathtoolsdate\space \ltlabmathtoolsversion\space mathtools adaptions]
%    \end{macrocode}
% \changes{0.80d}{2025-12-13}{correct @@ handling, remove tagpdf-base}
% \subsection{\cs{shortintertext}}
% Similar to the \cs{intertext} command from amsmath,
% \cs{shortintertext} errors with active tagging as it is processed twice
% which leads to duplicated structures. 
% The fix is similar but is complicated as \pkg{mathtools} defines two version (and
% an additional \cs{intertext} version) and package options to switch between the variants.
% 
% At first we redefine all the internal commands
%    \begin{macrocode}
\ExplSyntaxOn
\tl_new:N\l_@@_mathtools_init_tl
\cs_if_eq:NNTF\intertext@ \MT_intertext:
  {
    \tl_set:Nn \l_@@_mathtools_init_tl {\MT_orig_intertext_false:}
  }
  {
    \tl_set:Nn \l_@@_mathtools_init_tl {\MT_orig_intertext_true:}
  } 
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_if_eq:NNTF\shortintertext@ \MT_shortintertext:n
  {
    \tl_put_right:Nn \l_@@_mathtools_init_tl 
      {\MT_orig_shortintertext_false:}
  }    
  {
    \tl_put_right:Nn \l_@@_mathtools_init_tl 
      {\MT_orig_shortintertext_true:}
  }    
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=>
%    \end{macrocode}
%    \begin{macrocode}
\def\MT_intertext: {%
  \def\intertext##1{%
    \ifvmode\else\\\@empty\fi
    \noalign{%
      \penalty\postdisplaypenalty\vskip\belowdisplayskip
      \vskip-\lineskiplimit      % CCS
      \vskip\normallineskiplimit % CCS
      \vskip\l_MT_above_intertext_sep
       \vbox{%
%    \end{macrocode}
% Stop tagging when measuring:
%    \begin{macrocode}
      \ifmeasuring@\tag_suspend:n{\measuring}\fi          
      \normalbaselines
        \ifdim
          \ifdim\@totalleftmargin=\z@
            \linewidth
          \else
            -\maxdimen
          \fi
        =\columnwidth
       \else \parshape\@ne \@totalleftmargin \linewidth
       \fi
%    \end{macrocode}
% End the previous mc:
% \changes{0.80e}{2026-04-25}{Use tagging socket}
%    \begin{macrocode}
       \UseTaggingSocket{math/intertext}{}
        {\noindent\ignorespaces##1\par}
       }%                  
      \penalty\predisplaypenalty\vskip\abovedisplayskip%
      \vskip-\lineskiplimit      % CCS
      \vskip\normallineskiplimit % CCS
      \vskip\l_MT_below_intertext_sep
   }%
 }%
 \MH_let:NwN \shortintertext \shortintertext@
}
%    \end{macrocode}
%    \begin{macrocode}
\def\MT_orig_shortintertext:n #1{%
  \ifvmode\else\\\@empty\fi
  \noalign{%
    \penalty\postdisplaypenalty\vskip\abovedisplayshortskip
    \vbox{%
      \ifmeasuring@\tag_suspend:n{\measuring}\fi    
      \normalbaselines
      \MH_if_dim:w
        \MH_if_dim:w \@totalleftmargin=\z@
          \linewidth
        \MH_else:
          -\maxdimen
        \MH_fi:
        =\columnwidth
      \MH_else:
        \parshape\@ne \@totalleftmargin \linewidth
      \MH_fi:
%    \end{macrocode}
% \changes{0.80e}{2026-04-25}{Use tagging socket}
%    \begin{macrocode}
      \UseTaggingSocket{math/intertext}{}
       {\noindent\ignorespaces#1\par}
    }
    \penalty\predisplaypenalty\vskip\abovedisplayshortskip%
  }%
}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\MT_shortintertext:n #1{%
  \ifvmode\else\\\@empty\fi
  \noalign{%
    \penalty\postdisplaypenalty\vskip\abovedisplayshortskip
    \vskip-\lineskiplimit
    \vskip\normallineskiplimit
    \vskip\l_MT_above_shortintertext_sep
    \vbox{%
    \ifmeasuring@\tag_suspend:n{\measuring}\fi    
    \normalbaselines
      \MH_if_dim:w
        \MH_if_dim:w \@totalleftmargin=\z@
          \linewidth
        \MH_else:
          -\maxdimen
        \MH_fi:
        =\columnwidth
      \MH_else:
        \parshape\@ne \@totalleftmargin \linewidth
      \MH_fi:
%    \end{macrocode}
% \changes{0.80e}{2026-04-25}{Use tagging socket}
%    \begin{macrocode}
      \UseTaggingSocket{math/intertext}{}
       {\noindent\ignorespaces#1\par}
    }%
    \penalty\predisplaypenalty\vskip\abovedisplayshortskip%
    \vskip-\lineskiplimit
    \vskip\normallineskiplimit
    \vskip\l_MT_below_shortintertext_sep
  }%
}    
%    \end{macrocode}
% see https://github.com/latex3/tagging-project/issues/734. The multlined environment
% still creates a few unneeded structure, perhaps triggered by empty tags.
%    \begin{macrocode}
    \renewcommand*\MT_mult_internal:n [1]{
     \MH_if_boolean:nF {outer_mult}{\alignedspace@left} %<-- requires amsmath 2016/11/05
      \MT_next:
      \bgroup
        \Let@
        \def\l_MT_multline_lastline_fint{0 }
        \chardef\dspbrk@context\@ne \restore@math@cr
        \MH_let:NwN \math@cr@@\MT_mult_mathcr_atat:w
        \MH_let:NwN \shoveleft\MT_shoveleft:wn
        \MH_let:NwN \shoveright\MT_shoveright:wn
        \spread@equation
        \MH_set_boolean_F:n {mult_firstline}
        \MT_measure_mult:n {#1}
        \MH_if_dim:w \l_MT_multwidth_dim<\l_MT_multline_measure_fdim
          \MH_setlength:dn \l_MT_multwidth_dim{\l_MT_multline_measure_fdim}
        \fi
        \MH_set_boolean_T:n {mult_firstline}
        \MH_if_num:w \l_MT_multline_lastline_fint=\@ne
          \MH_let:NwN \math@cr@@\MT_mult_firstandlast_mathcr:w
        \MH_fi:
        \ialign\bgroup
          \hfil\strut@$\m@th\displaystyle{}##
          \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd}}
          $
          \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
          \hfil
          \crcr
          \hfilneg
          #1
    }
%    \end{macrocode}
% end hook
%    \begin{macrocode}
%<@@=math>
%    \end{macrocode}
%    \begin{macrocode}   
  \l_@@_mathtools_init_tl  
\ExplSyntaxOff
%    \end{macrocode}
%
%    \begin{macrocode}
%</kernel>
%    \end{macrocode}
%
% \Finale
%
