% \iffalse meta-comment
%
%% File: latex-lab-mathtools.dtx
%
% Copyright (C) 2022-2024 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.
%
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-amsmath.dtx}
\end{document}
%</driver>
%
% \fi
%
%
% \title{The \texttt{latex-lab-mathtools} code\thanks{}}
% \author{\LaTeX{} Project}
%
% \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}
        [2023-01-05 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
\AddToHook{package/mathtools/after}
 {%
   \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:}
     } 
   \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:}
     }    
   \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_stop:\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_stop:\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%
      }%
    }
    
   \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_stop:\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}
% end hook
%    \begin{macrocode}   
  \l_@@_mathtools_init_tl
}  
\ExplSyntaxOff
%    \end{macrocode}
%
%    \begin{macrocode}
%</kernel>
%    \end{macrocode}
%
% \Finale
%
