%%
%% This is file `latex-lab-mathtagging.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latex-lab-mathtagging.dtx  (with options: `kernel')
%% 
%% This is a generated file.
%% 
%% Copyright 2021-2023 LaTeX Project
%% 
%% This file was generated from file(s) of the  `LaTeX-lab Bundle'.
%% ------------------------------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% `LaTeX-lab Bundle'. You may however distribute the `LaTeX-lab Bundle'
%% without such generated files.
%% 
%% The newest sources can be found below
%% 
%%    https://github.com/latex3/latex2e/required/latex-lab
%% 
%% where one can also log issues in case there are any.
%% 
%% 
%% File: latex-lab-mathtagging.dtx
\def\ltmathtaggingdate{2023-03-08}
\def\ltmathtaggingversion{v0.5a}
\ProvidesFile{latex-lab-mathtagging.ltx}
             [\ltmathtaggingdate\space
              \ltmathtaggingversion\space
              Grab all the math(s) and tag it (experiments)]
\AddToHook{begindocument/before}{\RequirePackage{latex-lab-testphase-block-tagging}}
\ExplSyntaxOn
\tl_gput_right:Nn \@kernel@before@begindocument
  { \RequirePackage { amsmath } }
\bool_new:N \l__math_collected_bool
\tl_new:N \g__math_grabbed_env_tl
\tl_new:N \g__math_grabbed_math_tl
\cs_new_protected:Npn \__math_process:nn #1#2
  {
    \legacy_if:nF { measuring@ }
      {
        \tl_if_in:nnF {#2} { \m@th }
          { \tl_trim_spaces_apply:nN {#2} \__math_process_auxi:nn {#1} }
      }
  }
\cs_generate_variant:Nn \__math_process:nn { V }
\cs_new_protected:Npn \__math_process_auxi:nn #1#2
  {
    \tl_gset:Nn \g__math_grabbed_env_tl {#2}
    \tl_gset:Nn \g__math_grabbed_math_tl {#1}
    \__math_process_auxii:nn {#2} {#1}
  }
\cs_new_protected:Npn \__math_process_auxii:nn #1#2 { }
\cs_new_protected:Npn \math_processor:n #1
  { \cs_set_protected:Npn \__math_process_auxii:nn ##1##2 {#1} }
\cs_new_protected:Npn \__math_grab_dollar:w % $
  #1 $
  {
    \tl_if_blank:nF {#1}
      {
        \__math_process:nn { math } {#1} % $
        \tagmcend %end P-chunk, in code: \tag_mc_end_push:
        \@kernel@math@begin
        #1 $
        \@kernel@math@end
        \tagmcbegin{}  % restart P-chunk (whatsits in pdftex)
      }
  }

\skip_new:N \l__math_tmpa_skip

\cs_new_protected:Npn \__math_grab_dollardollar:w % $$
  #1 $$
  {
    \tl_if_blank:nF {#1}
      {
        \__math_tag_dollardollar_display:nn { equation* }{#1}
        #1
        $$
      }
  }

\cs_new_protected:Npn \@kernel@close@P {
  \tagmcend %end P-chunk, in code: \tag_mc_end_push:
        \int_gincr:N \g__tag_para_end_int
        \bool_if:NT \l__tag_para_show_bool
          { \tag_mc_begin:n{artifact}
            \rlap{\color_select:n{red}\tiny\ \int_use:N\g__tag_para_end_int}
            \tag_mc_end:
          }
        \tag_struct_end:
}

\cs_new_protected:Npn \__math_tag_dollardollar_display:nn #1#2 {
  \__math_process:nn {#1} {#2}
  \@kernel@close@P
  \@kernel@math@begin
%%
}

\cs_new_protected:Npn \__math_tag_dollardollar_display_end: {
  \tagpdfparaOff
  \para_raw_end:
  \tagpdfparaOn
  \l__math_tmpa_skip \lastskip
  \@kernel@math@end
  \penalty \postdisplaypenalty
  \skip_vertical:n { -\l__math_tmpa_skip }
  \@doendpe             % this has no \end{...} to take care of it
}

\cs_new_protected:Npn \__math_grab_inline:w % \(
  #1 \)
  {
    \tl_if_blank:nF {#1}
      {
        \__math_process:nn { math } {#1}
        $ #1 $
      }
    \bool_set_false:N \l__math_collected_bool
  }
\cs_new_protected:Npn \__math_grab_eqn:w % \[
  #1 \]
   {
     \begin { equation* } #1 \end { equation* }
   }
\tl_new:N \l__math_env_name_tl
\keys_define:nn { __math }
  {
     arg-spec .code:n =
       {
         \ExpandArgs { c } \DeclareDocumentCommand
           { __math_env \l__math_env_name_tl _aux: }
           {#1}
           { \__math_env_forward:w }
       }
  }
\cs_new_protected:Npn \math_register_env:nn #1#2
  {
    \tl_set:Nn \l__math_env_name_tl {#1}
    \keys_set:nn { __math } {#2}
    \cs_gset_eq:cc { __math_env_ #1 _begin: } {#1}
    \cs_gset_eq:cc { __math_env_ #1 _end: } { end #1 }
    \ExpandArgs { nnx } \RenewDocumentEnvironment {#1} { b }
      {
        \exp_not:N \bool_if:NTF \exp_not:N \l__math_collected_bool
          {
          }
          {
            \cs_if_exist:cTF { __math_env #1 _aux: }
              {
                \exp_not:c { __math_env #1 _aux: }
                  ####1 \exp_not:N \__math_env_end: {#1}
              }
              { \exp_not:N \__math_process:nn {#1} {####1} }
            \exp_not:n { \@kernel@math@registered@begin }
            \bool_set_true:N \exp_not:N \l__math_collected_bool
          }
        \exp_not:c { __math_env_ #1 _begin: }
        ####1
        \exp_not:c { __math_env_ #1 _end: }
     }
      {
      }
  }

\cs_set_protected:Npn \__cs_tmp:w #1
  {
    \group_begin:
      \exp_args:No \__cs_generate_internal_variant:n
        { \tl_to_str:n {#1} }
    \group_end:
  }
\__cs_tmp:w { nnxx }

\cs_new_protected:Npn \math_register_halign_env:nn #1#2
  {
    \tl_set:Nn \l__math_env_name_tl {#1}
    \keys_set:nn { __math } {#2}
    \cs_gset_eq:cc { __math_env_ #1 _begin: } {#1}
    \cs_gset_eq:cc { __math_env_ #1 _end: } { end #1 }
    \ExpandArgs { nnxx } \RenewDocumentEnvironment {#1} { b }
      {
        \exp_not:N \bool_if:NTF \exp_not:N \l__math_collected_bool
          {
          }
          {
            \cs_if_exist:cTF { __math_env #1 _aux: }
              {
                \exp_not:c { __math_env #1 _aux: }
                  ####1 \exp_not:N \__math_env_end: {#1}
              }
              { \exp_not:N \__math_process:nn {#1} {####1} }
            \exp_not:n { \@kernel@math@registered@begin }
            \bool_set_true:N \exp_not:N \l__math_collected_bool
          }
        \exp_not:c { __math_env_ #1 _begin: }
        ####1
     }
      {
        \exp_not:c { __math_env_ #1 _end: }
      }
  }

\cs_new_protected:Npn \math_register_odd_env:nn #1#2
  {
    \tl_set:Nn \l__math_env_name_tl {#1}
    \keys_set:nn { __math } {#2}
    \cs_gset_eq:cc { __math_env_ #1 _begin: } {#1}
    \cs_gset_eq:cc { __math_env_ #1 _end: } { end #1 }
    \ExpandArgs { nnxx } \RenewDocumentEnvironment {#1} { b }
      {
        \exp_not:N \bool_if:NTF \exp_not:N \l__math_collected_bool
          {
          }
          {
            \cs_if_exist:cTF { __math_env #1 _aux: }
              {
                \exp_not:c { __math_env #1 _aux: }
                  ####1 \exp_not:N \__math_env_end: {#1}
              }
              { \exp_not:N \__math_process:nn {#1} {####1} }
            \exp_not:n { \@kernel@math@registered@begin }
            \bool_set_true:N \exp_not:N \l__math_collected_bool
          }
        \exp_not:c { __math_env_ #1 _begin: }
        ####1
     }
      {
        \exp_not:c { __math_env_ #1 _end: }
        \exp_not:n { \@kernel@math@registered@end }
      }
  }


\cs_new:Npn \@kernel@math@registered@begin {
  \mode_if_vertical:TF
       {
         \exp_args:Noo\str_if_eq:nnF \g__tag_struct_tag_tl { \l__tag_para_main_tag_tl }    % needs correction!
             {
               \__block_beginpar_vmode:
             }              % needs correction!
       }
       {
         \@kernel@close@P
       }
  \@kernel@math@begin
  \tagpdfparaOff
}

\cs_new:Npn \@kernel@math@registered@end {
  \para_raw_end:
  \tagpdfparaOn
  \@kernel@math@end
  \@endpetrue
}

\cs_new_protected:Npn \math_register_env:n #1
  { \math_register_env:nn {#1} { } }
\NewDocumentCommand \RegisterMathEnvironment { O{} m }
  { \math_register_env:nn {#2} {#1} }
\cs_new_protected:Npn \__math_env_forward:w #1 \__math_env_end: #2
  { \__math_process:nn {#2} {#1} }
\tl_gput_right:Nn \@kernel@before@begindocument
  {
    \math_register_env:n { equation }
    \math_register_env:n { equation* }
    \RenewDocumentEnvironment{math} {b}{$#1$}{}
    \RenewDocumentEnvironment{displaymath} {b}{\[#1\]}{}
  }
\cs_gset_protected:Npn \( % \)
  {
    \bool_if:NTF \l__math_collected_bool
      {
        \mode_if_math:TF
          { \@badmath }
          { $ }
      }
      {
        \bool_set_true:N \l__math_collected_bool
        \__math_grab_inline:w
      }
  } % \(
\cs_gset_protected:Npn \)
  {
    \mode_if_math:TF
      { $ }
      { \@badmath }
  }
\tl_gput_right:Nn \@kernel@before@begindocument
  {
    \cs_gset_protected:Npn \[ % \]
       {
        \bool_if:NTF \l__math_collected_bool
          { \begin { equation* } }
          { \__math_grab_eqn:w }
      } % \[
    \cs_gset_protected:Npn \]
      {
        \bool_if:NTF \l__math_collected_bool
          { \end{ equation* } }
          { \@badmath }
      }
  }

\tl_new:N\tmpmathcontent

\def\@kernel@math@begin {
  \tl_gset:Nx\tmpmathcontent
     {\exp_not:N\begin{\g__math_grabbed_env_tl}
       \space
       \exp_not:V\g__math_grabbed_math_tl
       \space
       \exp_not:N\end{\g__math_grabbed_env_tl}
    }
  \tagstructbegin{tag=Formula,
    AFinline-o=\tmpmathcontent,
    title-o=\g__math_grabbed_env_tl,
    actualtext=\tmpmathcontent
  }
  \grabaformulapartandstart
}
\def\@kernel@math@end {
  \tagmcend
  \if@subformulas
    \tagstructend
  \else
  \fi
  \tagstructend
}

\exp_args:No \tex_everymath:D
  {
    \tex_the:D \tex_everymath:D
    \bool_if:NF \l__math_collected_bool
      {
        \bool_set_true:N \l__math_collected_bool
        \__math_grab_dollar:w
      }
  }
\exp_args:No \tex_everydisplay:D
  {
    \tex_the:D \tex_everydisplay:D
    \iftrue  % this may have to be a settable flag!
        \skip_set:Nn \belowdisplayskip      {-\belowdisplayskip}
        \skip_set:Nn \belowdisplayshortskip {-\belowdisplayshortskip}
        \int_set:Nn \postdisplaypenalty {10000}
        \group_insert_after:N \__math_tag_dollardollar_display_end:
    \fi
    \bool_if:NF \l__math_collected_bool
      {
        \bool_set_true:N \l__math_collected_bool
        \__math_grab_dollardollar:w
      }
  }
\math_register_env:n { eqnarray }
\math_register_env:n { eqnarray* }
\clist_map_inline:nn
  { tabular }
  {
    \AddToHook{ env / #1 / begin }
      { \bool_set_true:N \l__math_collected_bool }
  }
\cs_new_eq:NN \__math_m@th: \m@th
\cs_gset_protected:Npn \m@th
  {
    \bool_set_true:N \l__math_collected_bool
    \__math_m@th:
  }

\tl_gput_right:Nn \@kernel@before@begindocument {
\renewenvironment{gather*}{%
  \start@gather\st@rredtrue
}
{%
  %  \endgather
  \math@cr \black@\totwidth@ \egroup
  $$\ignorespacesafterend
}
\def\common@align@ending {
  \math@cr \black@\totwidth@
  \egroup
  \ifingather@
    \restorealignstate@
    \egroup
    \nonumber
    \ifnum0=`{\fi\iffalse}\fi
  \else
    $$%
  \fi
  \ignorespacesafterend
}
\renewenvironment{alignat}{%
  \start@align\z@\st@rredfalse
}{%
  \common@align@ending
}
\renewenvironment{alignat*}{%
  \start@align\z@\st@rredtrue
}{%
  \common@align@ending
}
\renewenvironment{xalignat}{%
  \start@align\@ne\st@rredfalse
}{%
  \common@align@ending
}
\renewenvironment{xalignat*}{%
  \start@align\@ne\st@rredtrue
}{%
  \common@align@ending
}
\renewenvironment{xxalignat}{%
  \start@align\tw@\st@rredtrue
}{%
  \common@align@ending
}
\renewenvironment{align}{%
  \start@align\@ne\st@rredfalse\m@ne
}{%
  \common@align@ending
}
\renewenvironment{align*}{%
  \start@align\@ne\st@rredtrue\m@ne
}{%
  \common@align@ending
}
\renewenvironment{flalign}{%
  \start@align\tw@\st@rredfalse\m@ne
}{%
  \common@align@ending
}
\renewenvironment{flalign*}{%
  \start@align\tw@\st@rredtrue\m@ne
}{%
  \common@align@ending
}
\renewenvironment{multline*}{\start@multline\st@rredtrue}
{%
  \iftagsleft@ \@xp\lendmultline@ \else \@xp\rendmultline@ \fi
  \ignorespacesafterend
}
\def\measuring@true{\let\ifmeasuring@\iftrue\tag_stop:}
  \math_register_halign_env:nn {align}{}
  \math_register_halign_env:nn {align*}{}
  \math_register_halign_env:nn {flalign}{}
  \math_register_halign_env:nn {flalign*}{}
  \math_register_halign_env:nn {gather}{}
  \math_register_halign_env:nn {gather*}{}
  \math_register_halign_env:nn {multline}{}
  \math_register_halign_env:nn {multline*}{}
  \math_register_halign_env:nn {xalignat}{}
  \math_register_halign_env:nn {xalignat*}{}
  \math_register_halign_env:nn {xxalignat}{}
  %
  \@namedef{maketag @ @ @} #1{%
    \ifmeasuring@
      \hbox{\m@th\normalfont#1}%
    \else
      \tagmcend \tagstructbegin{tag=Lbl}%
      \tagmcbegin{tag=Lbl}%
      \hbox{\m@th\normalfont#1}%
      \tagmcend \tagstructend \tagmcbegin{}%
    \fi
  }
    \def\intertext@{%
      \def\intertext##1{%
        \ifvmode\else\\\@empty\fi
        \noalign{%
          \penalty\postdisplaypenalty\vskip-\belowdisplayskip
          \vbox{
           \ifmeasuring@\tag_stop:\fi
           \normalbaselines
            \ifdim\linewidth=\columnwidth
            \else \parshape\@ne \@totalleftmargin \linewidth
            \fi
            \tag_mc_end_push:
            \tagpdfsetup{paratag=P}%
            \tagpdfparaOn
            \noindent\ignorespaces##1\par
            \tag_mc_begin_pop:n{}}%
          \penalty\predisplaypenalty\vskip\abovedisplayskip%
        }%
      }
    }
\@namedef{math@cr @ @ @ gather}{%
    \ifst@rred\nonumber\fi
   &\relax
    \make@display@tag
    \maybestartnewformulatag
    \ifst@rred\else\global\@eqnswtrue\fi
    \global\advance\row@\@ne
    \cr
}
\@namedef{math@cr @ @ @ align}{%
  \ifst@rred\nonumber\fi
  \if@eqnsw \global\tag@true \fi
  \global\advance\row@\@ne
  \add@amps\maxfields@
  \omit
  \kern-\alignsep@
  \iftag@
    \setboxz@h{\@lign\strut@{\make@display@tag}}%
    \place@tag
  \fi
    \maybestartnewformulatag
  \ifst@rred\else\global\@eqnswtrue\fi
  \global\lineht@\z@
  \cr
}
\def\restore@math@cr{\@namedef{math@cr @ @ @}{
    \maybestartnewformulatag
    \cr}}
\restore@math@cr
}

\cs_new:Npn \__math_split_at_nl_first:w #1 \\ #2 \\ #3 \s_stop
  {
    \quark_if_nil:nTF {#2}
      { {#1} {  } }
      {
        \__math_split_chk_if_begin:ww #1 \begin \q_nil \s_mark
          #2 \\ #3 \s_stop
      }
  }
\cs_new:Npn \__math_split_chk_if_begin:ww #1 \begin #2 #3 \s_mark
    #4 \\ \q_nil \\ \s_stop
  {
    \quark_if_nil:nTF {#2}
      { {#1} {#4} }
      {
        \exp_after:wN \__math_split_collect_one_end:w
          \__math_split_cleanup_begin_q_nil:w #1 \begin{#2} #3 \\ #4 \s_stop
            { } { 1 }
      }
  }
\cs_new:Npn \__math_split_cleanup_begin_q_nil:w #1 \begin \q_nil {#1}
\cs_new:Npn \__math_split_collect_one_end:w #1 \end #2 #3 \s_stop #4 #5
  {
    \exp_args:Nf \__math_split_check_count_begins:nnnn
      { \__math_split_count_begins:n { #4 #1 } } {#5}
      { #4 #1 \end{#2} } {#3}
  }
\cs_new:Npn \__math_split_count_begins:n #1
  { \int_eval:n { 0 \__math_split_count_begins:w #1 \begin \q_nil } }
\cs_new:Npn \__math_split_count_begins:w #1 \begin #2
  { \quark_if_nil:nF {#2} { +1 \__math_split_count_begins:w } }
\cs_new:Npn \__math_split_check_count_begins:nnnn #1 #2 #3 #4
  {
    \int_compare:nNnTF {#1} = {#2}
      {
        \exp_last_unbraced:Nf \__math_split_final_cleanup:nn
          { \split:n { \__math_split_guard:n {#3} #4 } }
      }
      {
        \exp_args:No \use_ii_i:nn
          { \exp_after:wN { \int_value:w \int_eval:n { #2 + 1 } } }
          { \__math_split_collect_one_end:w #4 \s_stop {#3} }
      }
  }
\cs_new:Npn \__math_split_final_cleanup:nn #1 #2
  {
      \exp:w \__math_split_final_cleanup:w #1
        \__math_split_guard:n \q_nil \s_mark { }
      {#2}
  }
\cs_new:Npn \__math_split_final_cleanup:w #1 \__math_split_guard:n #2 #3 \s_mark #4
  {
    \quark_if_nil:nTF {#2}
      { \exp_end: { #4 #1 } }
      { \__math_split_final_cleanup:w #3 \s_mark { #4 #1 #2 } }
  }
\NewDocumentCommand \splitnl { mm +m }
  {
    \tl_set:Nf \l_tmpa_tl { \split:n {#3} }
    \show \l_tmpa_tl
    \exp_after:wN \__splitnl_aux:nnNN \l_tmpa_tl #1 #2
  }

\cs_new:Npn \split:n #1 {
    \__math_split_at_nl_first:w #1 \\ \q_nil \\ \s_stop }

\cs_new:Npn \__math_split_at_nl:NN #1#2 {
  \tl_set:Nf \l_tmpa_tl {
      \exp_after:wN \__math_split_at_nl_first:w #1 \\ \q_nil \\ \s_stop }
  \exp_after:wN \__math_split_at_nl_aux:nnNN \l_tmpa_tl #1 #2
}

\cs_new_protected:Npn \__math_split_at_nl_aux:nnNN #1 #2 #3 #4
  {
    \tl_gset:Nn #4 {#1}
    \tl_gset:Nn #3 {#2}
}


\newif\if@subformulas
\tl_new:N \result

\cs_new_protected:Npn\grabaformulapartandstart {
  \__math_split_at_nl:NN  \g__math_grabbed_math_tl \result
  \typeout{====>first-result=\meaning\result}
  \typeout{====>first-tmpmathcontent=\meaning\g__math_grabbed_math_tl}
  \tl_if_empty:NTF \g__math_grabbed_math_tl
     {
       \typeout{====>formula~ has~ no~ subparts}
       \global\@subformulasfalse
     }
     {
       \typeout{====>formula~ has~ subparts}
       \global\@subformulastrue
       \edef\resulttitle{\g__math_grabbed_env_tl\space (part)}
       \tagstructbegin{tag=Formula,
         title-o=\resulttitle
       }
    }
    \tagmcbegin{}
}

\cs_new_protected:Npn\grabaformulapartandmayberestart {
  \__math_split_at_nl:NN  \g__math_grabbed_math_tl \result
  \typeout{====>result=\meaning\result}
  \typeout{====>tmpmathcontent=\meaning\g__math_grabbed_math_tl}
       \edef\resulttitle{\g__math_grabbed_env_tl\space (part)}
       \tagstructbegin{tag=Formula,
         alt=\result,
         title-o=\resulttitle
       }
    \tagmcbegin{}
}
\def\maybestartnewformulatag {
\if@subformulas
 \ifmeasuring@\else
  \tl_if_empty:NF \g__math_grabbed_math_tl
     {
       \tagmcend
       \tagstructend
       \grabaformulapartandmayberestart
     }
 \fi
\fi
}
\ExplSyntaxOff

\endinput
%%
%% End of file `latex-lab-mathtagging.ltx'.
