% \iffalse meta-comment
%
%% File: latex-lab-mathtools.dtx
%
% Copyright (C) 2022-2025 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 development version of the bundle can be found below
%
%    https://github.com/latex3/latex2e/required/latex-lab
%
% for those people who are interested or want to report an issue.
%
\def\ltlabmathtoolsdate{2025-06-18}
\def\ltlabmathtoolsversion{0.80c}
%<*driver>
\DocumentMetadata{tagging=on,pdfstandard=ua-2}
\documentclass[kernel]{l3doc}
\usepackage{latex-lab-testphase-l3doc}
\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}
        [2024-07-13 v0.1a mathtools adaptions]
%    \end{macrocode}
% \subsection{Tagpdf support}
% To make the code independent from tagging being loaded and active
% we load the \pkg{tagpdf-base} package:
%
%    \begin{macrocode}
\RequirePackage{tagpdf-base}
%    \end{macrocode}
% 
% \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}
\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:
%    \begin{macrocode}
       \tag_mc_end_push:
%    \end{macrocode}
% We are already in a par so we change now to Span:
%    \begin{macrocode}
        \tagpdfsetup{para/tag=Span}
        \noindent\ignorespaces##1\par
%    \end{macrocode}
% Restart the MC
%    \begin{macrocode}
        \tag_mc_begin_pop:n{}}%                  
      \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:
      \tag_mc_end_push:
      \tagpdfsetup{para/tag=Span}
      \tagpdfparaOn
      \noindent\ignorespaces#1\par
      \tag_mc_begin_pop:n{}}
    \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:
      \tag_mc_end_push:
      \tagpdfsetup{para/tag=P}
%    \end{macrocode}
% Why is it needed to enable paratagging??
%    \begin{macrocode}
      \tagpdfparaOn
      \noindent\ignorespaces#1\par
      \tag_mc_begin_pop:n{}}%
    \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}   
  \l_@@_mathtools_init_tl  
\ExplSyntaxOff
%    \end{macrocode}
%
%    \begin{macrocode}
%</kernel>
%    \end{macrocode}
%
% \Finale
%
