% \iffalse meta-comment
%
%% File: latex-lab-mathpkg.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
%
%<*driver>
\DocumentMetadata{tagging=on,pdfstandard=ua-2}
\documentclass[kernel]{l3in2edoc}

\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-mathpkg.dtx}
\end{document}
%</driver>
%
% \fi
%
%
% \title{The \texttt{latex-lab-mathpkg} 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 various math packages 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-mathpkg.ltx}
        [2026-04-25 v0.1d mathpkg adaptions]
%    \end{macrocode}
%
% \subsection{breqn}
%    \begin{macrocode}
\AddToHook{package/breqn/after}{
   \RegisterMathEnvironment{dmath}
   \RegisterMathEnvironment{dgroup*}
   \AddToHook{env/dmath/begin}{\tagpdfparaOff}
   \AddToHook{env/dgroup*/begin}{\tagpdfparaOff}
}
%    \end{macrocode}
%
% \subsection{cases}
% Force loading of \pkg{amsmath} before \pkg{cases}.
%    \begin{macrocode}
\AddToHook{package/cases/before}{\RequirePackage{amsmath}}
\AddToHook{package/cases/after}{
  \RegisterMathEnvironment{subnumcases}
}
%    \end{macrocode}
%
% \subsection{bm}
% Similar to the amsbsy code in latex-lab-amsmath for \cs{pmb} we need to mark up
% copies as artifact (with mathml-SE this probably has the same problem as \cs{pmb}):
% \changes{v0.1d}{2026-04-25}{Use tagging sockets in pmb patch}
%    \begin{macrocode}
\ExplSyntaxOn
\cs_new:Npn\__math_patch_bm_pmb:
 {
   \NewTaggingSocket{bm/pmb}{2}
   \NewTaggingSocketPlug{bm/pmb}{default}
    {
      \tag_mc_end:\tag_mc_begin:n{artifact}
      \tag_mc_reset_box:N##1
      ##2
      \tag_mc_end:
      \tag_mc_begin:n{}
    }
   \AssignTaggingSocketPlug{amsbsy/pmb}{default}
   \def\bm@pmb@@@@##1##2##3{{%
     \hbox_set:Nn\l_@@_tmpa_box{$\m@th##1##3$}%
     \dimen@##2\wd\tw@
     \rlap{\box_use:N\l_@@_tmpa_box}%
     \UseTaggingSocket{bm/pmb}{\l_@@_tmpa_box}
      {
        \kern\dimen@
        \raise1.5\dimen@\rlap{\box_use:N\l_@@_tmpa_box}%
        \kern\dimen@
        \box_use_drop:N\l_@@_tmpa_box
      }}}
   }
\AddToHook{package/bm/after}
 { \__math_patch_bm_pmb: }
\ExplSyntaxOff
%    \end{macrocode}
% \subsection{amsmath timing}
% These packages error or lose their function if loaded before \pkg{amsmath}.
%    \begin{macrocode}
\AddToHook{package/accents/before}{\RequirePackage{amsmath}}
\AddToHook{package/cleveref/before}{\RequirePackage{amsmath}}
\AddToHook{package/esint/before}{\RequirePackage{amsmath}}
\AddToHook{package/grmath/before}{\RequirePackage{amsmath}}
\AddToHook{package/mathabx/before}{\RequirePackage{amsmath}}
\AddToHook{package/wasysym/before}{\RequirePackage{amsmath}}
%    \end{macrocode}
%    \begin{macrocode}
%</kernel>
%    \end{macrocode}
%
% \Finale
%
