%%
%% This is file `latex-lab-math.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latex-lab-math.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-math.dtx
\def\ltlabmathdate{2024-10-25}
\def\ltlabmathversion{0.6i}
\ProvidesFile{latex-lab-math.ltx}
             [\ltlabmathdate\space
              v\ltlabmathversion\space
              Grab all the math(s) and tag it (experiments)]
\AddToHook{begindocument/before}{\RequirePackage{latex-lab-testphase-block}}
\ExplSyntaxOn
\AddToHook{begindocument/before}{ \RequirePackage { amsmath } }
\bool_new:N \l__math_collected_bool
\bool_new:N \l__math_fakemath_bool
\tl_new:N \g__math_grabbed_env_tl
\tl_new:N \g__math_grabbed_math_tl
\tl_new:N \l__math_tmpa_tl
\skip_new:N \l__math_tmpa_skip
\str_new:N \l__math_tmpa_str
\tl_new:N \l__math_content_alt_tl
\tl_new:N \l__math_content_actual_tl
\tl_new:N \l__math_content_AF_source_tl
\tl_new:N \l__math_content_AF_source_tmpa_tl
\tl_new:N \l__math_content_AF_mathml_tl
\cs_new_protected:Npn \__tag_tool_close_P:
  {
    \tag_if_active:T
     {
       \tag_mc_end: %end P-chunk, should perhaps be \tag_mc_end_push: ...
         \__tag_gincr_para_end_int:
         \__tag_check_para_end_show:nn{red}{} %debug: show para
         \tag_struct_end:
     }
  }
\tl_new:N\l__math_attribute_class_tl
\tagpdfsetup
      {role/new-attribute = {inline}    {/O /Layout /Placement/Inline},
       role/new-attribute = {display}   {/O /Layout /Placement/Block},
      }
\bool_new:N\l__tag_math_texsource_AF_bool
\bool_new:N\l__tag_math_texsource_pane_bool
\bool_new:N\l__tag_math_mathml_AF_bool
\bool_new:N\g__tag_math_mathml_AF_bool
\bool_new:N\l__tag_math_mathml_pane_bool
\bool_new:N\l__tag_math_alt_bool
\tl_new:N\g__tag_math_luamml_tl
\tl_gset:Nn\g__tag_math_luamml_tl {0}
\int_new:N\g__math_mathml_total_int
\int_new:N\g__math_mathml_int
\int_new:N\g__math_math_total_int
\int_new:N\g__math_mathml_AF_found_int
\int_new:N\g__math_mathml_AF_attached_int
\clist_new:N\l__tag_math_mathml_files_clist
\clist_put_right:Ne\l__tag_math_mathml_files_clist
  {\c_sys_jobname_str-mathml,\c_sys_jobname_str-luamml-mathml}
\cs_new_protected:Npn \__math_AF_mml:nnnn #1 #2 #3 #4
  {
    \int_gincr:N \g__math_mathml_total_int
    \tl_if_exist:cF { g__math_mathml_#3_tl }
     {
       \int_gincr:N \g__math_mathml_int
       \pdfdict_put:nne {l_pdffile/Filespec} {Desc}{(mathml-#1)}
       \pdffile_embed_stream:nnN {#4}{mathml-#1.xml}\l__math_tmpa_tl
       \bool_if:NT \l__tag_math_mathml_pane_bool
        {\pdfmanagement_add:nne {Catalog/Names}{EmbeddedFiles}{\l__math_tmpa_tl}}
       \tl_new:c{g__math_mathml_#3_tl}
       \tl_gset_eq:cN{g__math_mathml_#3_tl}\l__math_tmpa_tl
     }
  }
\cs_new_protected:Npn \__math_AF_html_reader:w#1</h2>#2<p>#3</p>#4<p>#5</p>#6<math{
  \begingroup
   \char_set_catcode_other:N\{
   \char_set_catcode_other:N\}
   \char_set_catcode_other:N\#
   \char_set_catcode_other:N\%
   \__math_AF_html_reader_verb:w{#1}{#3}{#5}<math
}
\cs_new_protected:Npn\__math_AF_html_reader_verb:w#1#2#3#4~</div>{
  \endgroup
   \__math_AF_mml:nnnn{#1}{#2}{#3}{#4}
   }
\tl_const:Nn \c__math_mathml_write_init_tl
  {
    <!DOCTYPE~html>
    \iow_newline:
    <html>
    \iow_newline:
  }
\tl_new:N \l__math_mathml_write_before_tl
\tl_const:Nn \c__math_mathml_write_after_tl
  {
    \iow_newline:
    </div>
    \iow_newline:
  }
\tl_const:Nn \c__math_mathml_write_final_tl
  {
    </html>
  }

\socket_new:nn {tagsupport/math/mathml/write/prepare}{0}
\socket_new_plug:nnn{tagsupport/math/mathml/write/prepare}{On}
  {
    \str_set:NV\l__math_tmpa_str\l__math_content_AF_source_tl
    \str_replace_all:Nnn\l__math_tmpa_str{&}{&amp;}
    \str_replace_all:Nnn\l__math_tmpa_str{<}{&lt;}
    \tl_set:Nn \l__math_mathml_write_before_tl
      {
        <div>
        \iow_newline:
        <h2>\c_backslash_str mml\c_space_tl \int_use:N \g__math_math_total_int </h2>
        \iow_newline:
        <p>\l__math_tmpa_str</p>
        \iow_newline:
        <p>\l__math_content_hash_tl </p>
        \iow_newline:
      }
  }
\cs_new_protected:Npn\__math_provide_luamml_commands:
  {
    \providecommand\luamml_flag_structelem:{}
    \cs_if_free:NT \luamml_structelem:
     {
       \cs_set_eq:NN\luamml_structelem:\luamml_flag_structelem:
     }
    \providecommand\luamml_flag_process:{}
    \cs_if_free:NT \luamml_process:
     {
       \cs_set_eq:NN\luamml_process:\luamml_flag_process:
     }
    \providecommand\luamml_flag_ignore:{}
    \cs_if_free:NT \luamml_ignore:
     {
       \cs_set_eq:NN\luamml_ignore:\luamml_flag_ignore:
     }
  }
\sys_if_engine_luatex:TF
 {
   \cs_new_protected:Npn \__math_correct_luamml_array_patches:
     {
       \AddToHook{package/array/after}
        {
          \cs_set:Npn \endarray
           {
             \tbl_crcr:n{endarray}
             \__luamml_array_save_array:
             \egroup
             \UseTaggingSocket{tbl/finalize}
             \tbl_restore_outer_cell_data:
             \egroup
             \mode_if_math:T { \__luamml_array_finalize_array: }
             \@arrayright
             \gdef \@preamble {}
           }
         \cs_set:Npn \@classz
           {
            \@classx
            \@tempcnta \count@
            \prepnext@tok
            \@addtopreamble {
              \ifcase \@chnum
                \hfil
                \hskip 1sp
                \d@llarbegin
                \cs_if_eq:NNTF \d@llarbegin \begingroup {
                  \insert@column
                  \d@llarend
                } {
                  \__luamml_array_init_col:
                  \insert@column
                  \luamml_flag_save:nn {} {mtd}
                  \d@llarend
                  \__luamml_array_finalize_col:w 0~
                }
                \do@row@strut
                \hfil
              \or
                \hskip 1sp
                \d@llarbegin
                \cs_if_eq:NNTF \d@llarbegin \begingroup {
                  \insert@column
                  \d@llarend
                } {
                  \__luamml_array_init_col:
                  \insert@column
                  \luamml_flag_save:nn {} {mtd}
                  \d@llarend
                  \__luamml_array_finalize_col:w 1~
                }
                \do@row@strut
                \hfil
              \or
                \hfil
                \hskip 1sp
                \d@llarbegin
                \cs_if_eq:NNTF \d@llarbegin \begingroup {
                  \insert@column
                  \d@llarend
                } {
                  \__luamml_array_init_col:
                  \insert@column
                  \luamml_flag_save:nn {} {mtd}
                  \d@llarend
                  \__luamml_array_finalize_col:w 2~
                }
                \do@row@strut
              \or
                \setbox \ar@mcellbox \vbox \@startpbox { \@nextchar }
                  \insert@pcolumn
                \@endpbox
                \ar@align@mcell
                \do@row@strut
              \or
                \vtop \@startpbox { \@nextchar }
                  \insert@pcolumn
                \@endpbox
                \do@row@strut
              \or
                \vbox \@startpbox { \@nextchar }
                  \insert@pcolumn
                \@endpbox
                \do@row@strut
              \fi
            }
            \prepnext@tok
          }
        }
     }
   \AddToHook{begindocument/before}
     {
       \str_case:on \g__math_luamml_load_tl
         {
           { 1 } {
                   \RequirePackage  { luamml }
                   \__math_correct_luamml_array_patches:
                   \AddToHook{begindocument/end}
                    {
                      \__math_luamml_activate_write:
                    }
                 }
           {-1 } {
                   \AddToHook{begindocument/end}
                    {
                     \msg_note:nnnn { tag }
                     { luamml-status }{ disabled }{ not~create }
                    }
                 }
           { 0 }
           {
             \@ifpackageloaded { unicode-math }
              {
                \RequirePackage  { luamml }
                \__math_correct_luamml_array_patches:
                \AddToHook{begindocument/end}
                  {
                    \__math_luamml_activate_write:
                  }
              }
              { \msg_warning:nn { tag }{ unicode-math-missing } }
           }
         }
         \__math_provide_luamml_commands:
     }
 }
 {
   \__math_provide_luamml_commands:
 }
\msg_new:nnn { tag }{ luamml-status }
  {
    luamml~has~been~#1~and~will~#2~an~MathML~file.
  }

\msg_new:nnn { tag }{ unicode-math-missing }
  {
    The~package~unicode-math~is~missing\\
    luamml~will~not~create~an~MathML~file.\\
    To~avoid~this~warning~load~unicode-math~\\
    or~disable~luamml~with~\\
    \tl_to_str:n{\tagpdfsetup{math/mathml/luamml/load=false}}\\
    or~force~luamml~with~\\
    \tl_to_str:n{\tagpdfsetup{math/mathml/luamml/load=true}}
  }
\cs_new_protected:Npn \__math_luamml_activate_write:
 {
   \bool_if:NT \g__math_luamml_write_bool
     {
       \bool_gset_true:N\g__tag_math_mathml_AF_bool
       \AssignSocketPlug{tagsupport/math/struct/begin}{mathml-AF}
       \AssignSocketPlug{tagsupport/math/struct/end}{mathml-AF}
       \AssignSocketPlug{tagsupport/math/substruct/begin}{single}
       \AssignSocketPlug{tagsupport/math/substruct/end}{single}
       \int_set:Nn \l__luamml_pretty_int { 7 }
       \RegisterFamilyMapping\symsymbols{oms}
       \RegisterFamilyMapping\symletters{oml}
       \AssignSocketPlug{tagsupport/math/mathml/write/prepare}{On}
       \iow_new:N   \g__math_luamml_iow
       \iow_open:Nn \g__math_luamml_iow {\c_sys_jobname_str-luamml-mathml.html}
       \iow_now:Ne  \g__math_luamml_iow { \c__math_mathml_write_init_tl  }
       \cs_new:Npn  \__math_luamml_output_hook:n  ##1
         {
           \tl_if_empty:NF \l__math_mathml_write_before_tl
             {
              \iow_now:Ne \g__math_luamml_iow
               {
                 \l__math_mathml_write_before_tl
                 ##1
                 \c__math_mathml_write_after_tl
               }
             }
         }
       \__luamml_register_output_hook:N \__math_luamml_output_hook:n
      \AddToHook{enddocument/afterlastpage}
        {
          \iow_now:Ne \g__math_luamml_iow
            { \c__math_mathml_write_final_tl }
          \iow_close:N \g__math_luamml_iow
        }
      \msg_note:nnnn { tag }
        { luamml-status }{ enabled }{ create }
     }
 }
\tl_new:N  \g__math_luamml_load_tl
\tl_gset:Nn \g__math_luamml_load_tl {0}
\bool_new:N  \g__math_luamml_write_bool
\bool_gset_true:N \g__math_luamml_write_bool
\msg_new:nnn { tag }{ PDF-2.0-recommended }
  {
    The~key~#1~will~not~work~properly~with~PDF~#2.\\
    Switching~to~PDF~2.0~is~recommended.
  }
\keys_define:nn { __tag / setup }
   {
     math/mathml/luamml/load .choice: ,
     math/mathml/luamml/load/true  .code:n = {\tl_gset:Nn \g__math_luamml_load_tl{1}},
     math/mathml/luamml/load/false .code:n = {\tl_gset:Nn \g__math_luamml_load_tl{-1}},
     math/mathml/luamml/load .default:n = true,
     math/mathml/luamml/load .usage:n=preamble,
     math/mathml/structelem .code:n =
      {
        \pdf_version_compare:NnT < {2.0}
         {
          \msg_warning:nnne { tag }{ PDF-2.0-recommended }
           { math/mathml/structelem }{ \pdf_version: }
         }
        \AddToHook{begindocument/end}{\luamml_structelem:}
      },
     math/mathml/ignore .code:n = {\luamml_ignore:},
     math/mathml/luamml/write .choice:,
     math/mathml/luamml/write/true .code:n =
      {
        \tl_gset:Nn \g__math_luamml_load_tl{1}
        \bool_gset_true:N \g__math_luamml_write_bool
      },
     math/mathml/luamml/write/false .code:n =
      {
        \bool_gset_false:N \g__math_luamml_write_bool
      },
     math/mathml/luamml/write .default:n = true,
     math/mathml/luamml/write .usage:n=preamble,
     math/mathml/luamml .bool_gset:N = \g__math_luamml_write_bool,
     math/mathml/luamml .usage:n=preamble
   }
\socket_new:nn {tagsupport/math/mathml/write}{0}
\socket_new_plug:nnn{tagsupport/math/mathml/write}{On}
 {
    \iow_now:Ne \g__math_writedummy_iow
     {
      \l__math_mathml_write_before_tl
      <math></math>
      \c__math_mathml_write_after_tl
      }
 }

\keys_define:nn { __tag / setup }
   {
     math/mathml/write-dummy .code:n =
       {
         \bool_gset_true:N \g__tag_math_mathml_AF_bool
         \tl_if_exist:NF\g__math_writedummy_iow
          {
            \iow_new:N  \g__math_writedummy_iow
            \iow_open:Nn \g__math_writedummy_iow
             {
               \c_sys_jobname_str-mathml-dummy.html
             }
            \iow_now:Ne \g__math_writedummy_iow
             {
               \c__math_mathml_write_init_tl
             }
            \AssignSocketPlug {tagsupport/math/mathml/write/prepare}{On}
            \AssignSocketPlug {tagsupport/math/mathml/write}{On}
            \AddToHook{enddocument/afterlastpage}
             {
               \iow_now:Ne \g__math_writedummy_iow
                 { \c__math_mathml_write_final_tl }
               \iow_close:N \g__math_writedummy_iow
             }
          }
       },
     math/mathml/write-dummy .usage:n=preamble
   }
\box_new:N\l__math_tmpa_box
\cs_new_protected:Npn \__math_AF_process_mathml_files:
 {
   \hbox_set:Nn \l__math_tmpa_box
    {
      \pdfdict_put:nnn { l_pdffile/Filespec }{AFRelationship} { /Supplement }
      \pdfdict_put:nne
       { l_pdffile }{Subtype}
       { \pdf_name_from_unicode_e:n{application/mathml+xml} }
      \char_set_catcode_other:N \#
      \cs_set_eq:NN\mml \__math_AF_html_reader:w
      \clist_map_inline:Nn \l__tag_math_mathml_files_clist
        {
          \file_if_exist:nTF {##1.html}
            {
              \typeout{Info:~reading~mathml~file~##1}
              \file_input:n {##1.html}
              \bool_gset_true:N\g__tag_math_mathml_AF_bool
            }
            {
              \typeout{Info:~mathml~file~##1~does~not~exist}%info message
            }
        }
    }
    \bool_if:NT\g__tag_math_mathml_AF_bool
      {
        \typeout{Info:~Activating~mathml~support}
        \AssignSocketPlug{tagsupport/math/struct/begin}{mathml-AF}
        \AssignSocketPlug{tagsupport/math/struct/end}{mathml-AF}
        \AssignSocketPlug{tagsupport/math/substruct/begin}{single}
        \AssignSocketPlug{tagsupport/math/substruct/end}{single}
        \AddToHook{enddocument/info}
         {
           \iow_term:n{MathML~statistic}
           \iow_term:n{================}
           \iow_term:e{==>~\int_use:N\g__math_mathml_total_int\c_space_tl
           MathML~fragments~read}
           \iow_term:e{==>~\int_use:N\g__math_mathml_int\c_space_tl
           different~MathML~fragments}
           \iow_term:e{==>~\int_use:N\g__math_math_total_int\c_space_tl
           math~fragments~found}
           \iow_term:e{==>~\int_use:N\g__math_mathml_AF_found_int\c_space_tl
           fitting~MathML~AF~found}
           \iow_term:e{==>~\int_use:N\g__math_mathml_AF_attached_int\c_space_tl
           MathML~AF~attached}
         }
      }
 }
\AddToHook{begindocument}{\__math_AF_process_mathml_files:}
\int_new:N \l__math_mathstyle_int
\int_new:N \g__math_mathchoice_int
\property_new:nnnn{mathstyle}{now}{-1}{\int_use:N \l__math_mathstyle_int }
\cs_new_protected:Npn \__math_tag_if_mathstyle:nn #1 #2
 %#1 refers to label
 %#2 is a number for the mathstyle (typically 0,2,4,6)
 {
   \int_set:Nn \l__math_mathstyle_int {#2}
   \property_record:nn {#1} { mathstyle }
   \int_compare:nNnTF { \property_ref:nn {#1}{ mathstyle} } = { #2 }
    { \tag_resume:n{\mathchoice} }{ \tag_suspend:n{\mathchoice} }
 }
\cs_generate_variant:Nn \__math_tag_if_mathstyle:nn {en}
\keys_define:nn { __tag / setup }
  {
   math/mathml/sources .clist_set:N = \l__tag_math_mathml_files_clist,
   math/alt/use        .bool_set:N  = \l__tag_math_alt_bool,
   viewer/pane/mathml      .bool_set:N = \l__tag_math_mathml_pane_bool,
   viewer/pane/mathml      .initial:n = true,
   viewer/pane/mathsource  .bool_set:N = \l__tag_math_texsource_pane_bool,
   math/mathml/AF .bool_set:N = \l__tag_math_mathml_AF_bool,
   math/mathml/AF  .initial:n = true,
   math/tex/AF    .bool_set:N = \l__tag_math_texsource_AF_bool,
   math/tex/AF    .initial:n = true
  }
\AddToHook{begindocument/end}
 {
   \str_if_eq:eeT
    {1}
    {
        \exp_last_unbraced:Ne\use_i:nn
         {\GetDocumentProperties{document/pdfstandard-UA}}
         \c_empty_tl\c_empty_tl
    }
    {
      \bool_if:NF \l__tag_math_alt_bool
       {
         \typeout{PDF/UA-1~detected.~Enabling~alt~text~on~Formula}
       }
      \bool_set_true:N\l__tag_math_alt_bool
    }
 }
\socket_new:nn {tagsupport/math/inline/begin}{0}
\socket_new:nn {tagsupport/math/inline/end}{0}
\socket_new:nn {tagsupport/math/inline/formula/begin}{1} %
\socket_new:nn {tagsupport/math/inline/formula/end}{0}
\socket_new_plug:nnn
  {tagsupport/math/inline/begin}
  {MC}
  {\tag_mc_end_push:}
\socket_new_plug:nnn
  {tagsupport/math/inline/end}
  {MC}
  {\tag_mc_begin_pop:n{}}
\socket_new_plug:nnn
  {tagsupport/math/inline/formula/begin}
  {default}
  { \tagpdfparaOff
    \tag_socket_use:n{math/content}
    \tag_socket_use:n{math/struct/begin}
    % inner formula if multiple parts (not really implemented yet)
    \tag_socket_use:n{math/substruct/begin}
    #1
    \tag_socket_use:n{math/end}
  }
\socket_new_plug:nnn
  {tagsupport/math/inline/formula/end}
  {default}
  {
    \socket_use:n{tagsupport/math/substruct/end}
    \socket_use:n{tagsupport/math/struct/end}
  }
\socket_new:nn {tagsupport/math/display/begin}{0}
\socket_new:nn {tagsupport/math/display/end}{0}
\socket_new:nn {tagsupport/math/display/formula/begin}{1} %
\socket_new:nn {tagsupport/math/display/formula/end}{0}

\socket_new_plug:nnn
  {tagsupport/math/display/begin}
  {default}
  { \__tag_tool_close_P:  }
\socket_new_plug:nnn
  {tagsupport/math/display/end}
  {default}
  {
  }

\socket_new_plug:nnn
  {tagsupport/math/display/formula/begin}
  {default}
  {
    \tag_socket_use:n{math/content}
    \tag_socket_use:n{math/struct/begin}
    \tag_socket_use:n{math/substruct/begin}
    #1
    \tag_socket_use:n{math/end}
  }
\socket_new_plug:nnn
  {tagsupport/math/display/formula/end}
  {default}
  {
    \socket_use:n{tagsupport/math/substruct/end}
    \socket_use:n{tagsupport/math/struct/end}
  }
\tl_new:N\l__math_content_template_tl
\tl_set:Nn \l__math_content_template_tl
   {
       LaTeX~ formula~ starts~
       \exp_not:N\begin{\g__math_grabbed_env_tl}
       \c_space_tl
       \exp_not:V\g__math_grabbed_math_tl
       \c_space_tl
       \exp_not:N\end{\g__math_grabbed_env_tl}
       \c_space_tl LaTeX~ formula~ ends~
   }

\tl_new:N\l__math_texsource_template_tl
\tl_const:Nn\c__math_inline_env_tl {math}
\tl_set:Nn \l__math_texsource_template_tl
   {
     \tl_if_eq:NNTF\g__math_grabbed_env_tl\c__math_inline_env_tl
      {
       $
         \exp_not:V\g__math_grabbed_math_tl
       $
      }
      {
       \exp_not:N\begin{\g__math_grabbed_env_tl}
       \exp_not:V\g__math_grabbed_math_tl
       \exp_not:N\end{\g__math_grabbed_env_tl}
      }
   }

\socket_new:nn {tagsupport/math/content}{0}
\socket_new_plug:nnn
  {tagsupport/math/content}
  {actual+source}
  {
   \tl_set:Ne\l__math_content_actual_tl
    {
       \l__math_content_template_tl
    }
   \tl_set:Ne \l__math_content_AF_source_tl
    {
      \l__math_texsource_template_tl
    }
   \tl_set:Nn    \l__math_content_AF_mathml_tl {}
   \tl_set:Nn    \l__math_content_alt_tl    {}
  }
\socket_new_plug:nnn
  {tagsupport/math/content}
  {alt+source}
  {
   \tl_set:Ne\l__math_content_alt_tl
    {
       \l__math_content_template_tl
    }
   \tl_set:Ne \l__math_content_AF_source_tl
    {
       \l__math_texsource_template_tl
    }
   \tl_set:Nn    \l__math_content_AF_mathml_tl {}
   \tl_set:Nn    \l__math_content_actual_tl    {}
  }
\socket_assign_plug:nn {tagsupport/math/content}{alt+source}
\socket_new:nn {tagsupport/math/struct/begin}{0}
\socket_new:nn {tagsupport/math/struct/end}{0}
\socket_new_plug:nnn
  {tagsupport/math/struct/begin}
  {default}
  {
    \bool_if:NTF\l__tag_math_texsource_AF_bool
     { \tl_set_eq:NN \l__math_content_AF_source_tmpa_tl \l__math_content_AF_source_tl }
     { \tl_clear:N \l__math_content_AF_source_tmpa_tl }
    \tl_if_eq:NnTF\g__math_grabbed_env_tl {math}
          {
            \tl_set:Nn\l__math_attribute_class_tl{inline}
          }
          {
            \tl_set:Nn\l__math_attribute_class_tl{display}
          }
    \bool_if:NF\l__tag_math_alt_bool
      { \tl_set:Nn \l__math_content_alt_tl{} }
    \tag_struct_begin:n
     {
       tag=Formula,
       attribute-class=\l__math_attribute_class_tl,
       texsource   = \l__math_content_AF_source_tmpa_tl,
       title-o     = \g__math_grabbed_env_tl,
       actualtext  = \l__math_content_actual_tl,
       alt         = \l__math_content_alt_tl
     }
  }
\socket_new_plug:nnn
  {tagsupport/math/struct/end}
  {default}
  { \tag_struct_end: }

\socket_assign_plug:nn {tagsupport/math/struct/begin}{default}
\socket_assign_plug:nn {tagsupport/math/struct/end}{default}
\cs_generate_variant:Nn \str_mdfive_hash:n {o}
\tl_new:N\l__math_content_hash_tl
\tl_new:N\l__math_grabbed_math_tl
\socket_new_plug:nnn
  {tagsupport/math/struct/begin}
  {mathml-AF}
  {
   \int_gincr:N\g__math_math_total_int
   \tl_set:Ne\l__math_content_hash_tl
    {\str_mdfive_hash:o { \l__math_content_AF_source_tl }}
   \tl_set_eq:NN\l__math_grabbed_math_tl\g__math_grabbed_math_tl
   \tl_if_eq:NnTF\g__math_grabbed_env_tl {math}
     {
       \tl_set:Nn\l__math_attribute_class_tl{inline}
     }
     {
       \tl_set:Nn\l__math_attribute_class_tl{display}
     }
   \bool_if:NF\l__tag_math_alt_bool
     { \tl_set:Nn \l__math_content_alt_tl{} }
   \tl_if_exist:cTF { g__math_mathml_ \l__math_content_hash_tl _tl }
     {
       \int_gincr:N\g__math_mathml_AF_found_int
       \bool_if:NTF \l__tag_math_mathml_AF_bool
        {
          \int_gincr:N\g__math_mathml_AF_attached_int
          \typeout {Inserting~mathml~with~Hash~\l__math_content_hash_tl}
        }
        {
          \typeout {Ignoring~mathml~with~Hash~\l__math_content_hash_tl}
        }
     }
     {
       \bool_if:NT \l__tag_math_mathml_AF_bool
        {
          \typeout {WARNING:~mathml~missing~for~hash~\l__math_content_hash_tl}
        }
     }
   \socket_use:n {tagsupport/math/mathml/write/prepare}
   \socket_use:n {tagsupport/math/mathml/write} % write hash if request
    \bool_if:NTF\l__tag_math_texsource_AF_bool
     { \tl_set_eq:NN \l__math_content_AF_source_tmpa_tl \l__math_content_AF_source_tl }
     { \tl_clear:N \l__math_content_AF_source_tmpa_tl }
   \tag_struct_begin:n
     {
       tag=Formula,
       attribute-class=\l__math_attribute_class_tl, %
       AFref       =
        \bool_if:NT\l__tag_math_mathml_AF_bool
         {
           \cs_if_exist_use:c {g__math_mathml_ \l__math_content_hash_tl _tl}
         },
       texsource   = \l__math_content_AF_source_tmpa_tl, % should be after mathml AF!
       title-o     = \g__math_grabbed_env_tl,    %
       alt         = \l__math_content_alt_tl
     }
   }
\socket_new_plug:nnn
  {tagsupport/math/struct/end}
  {mathml-AF}
  {
    \tag_struct_end:
  }
\socket_new:nn {tagsupport/math/substruct/begin}{0}
\socket_new:nn {tagsupport/math/substruct/end}{0}
\socket_new_plug:nnn
  {tagsupport/math/substruct/begin}
  {default}
  { \grabaformulapartandstart }
\socket_new_plug:nnn
  {tagsupport/math/substruct/end}
  {default}
  {
    \tagmcend
    \if@subformulas
      \tagstructend
    \fi
 }
\socket_assign_plug:nn {tagsupport/math/substruct/begin}{default}
\socket_assign_plug:nn {tagsupport/math/substruct/end}{default}

\socket_new_plug:nnn
  {tagsupport/math/substruct/begin}
  {single}
  {
    \typeout{====>subpart~splitting~deactivated}
    \typeout{====>grabbed~math=\meaning\g__math_grabbed_math_tl}
    \tag_mc_begin:n{}
  }
\socket_new_plug:nnn
  {tagsupport/math/substruct/end}
  {single}
  { \tag_mc_end:}
\socket_new:nn {tagsupport/math/end}{0}

\cs_new_protected:Npn \__tag_math_disable:
  {
    \socket_assign_plug:nn {tagsupport/math/inline/begin}{noop}
    \socket_assign_plug:nn {tagsupport/math/inline/end}{noop}
    \socket_assign_plug:nn {tagsupport/math/inline/formula/begin}{identity}
    \socket_assign_plug:nn {tagsupport/math/inline/formula/end}{noop}
    \socket_assign_plug:nn {tagsupport/math/display/begin}{noop}
    \socket_assign_plug:nn {tagsupport/math/display/end}{noop}
    \socket_assign_plug:nn {tagsupport/math/display/formula/begin}{identity}
    \socket_assign_plug:nn {tagsupport/math/display/formula/end}{noop}
  }
\cs_new_protected:Npn \__tag_math_enable:
  {
    \socket_assign_plug:nn {tagsupport/math/inline/begin}{MC}
    \socket_assign_plug:nn {tagsupport/math/inline/end}{MC}
    \socket_assign_plug:nn {tagsupport/math/inline/formula/begin}{default}
    \socket_assign_plug:nn {tagsupport/math/inline/formula/end}{default}
    \socket_assign_plug:nn {tagsupport/math/display/begin}{default}
    \socket_assign_plug:nn {tagsupport/math/display/end}{default}
    \socket_assign_plug:nn {tagsupport/math/display/formula/begin}{default}
    \socket_assign_plug:nn {tagsupport/math/display/formula/end}{default}
  }
\AtBeginDocument{\tag_if_active:T{\__tag_math_enable: }}
\cs_new_protected:Npn \__math_process:nn #1#2
  {
    \legacy_if:nF { measuring@ }
      {
        \tl_if_in:nnTF {#2} { \m@th }
          { \bool_set_true:N\l__math_fakemath_bool }
          { \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 { \__math_grab_dollar_delim:w }
\cs_new_protected:Npn \__math_grab_dollar:n #1
  {
    \__math_process:nn { math } {#1}
      \bool_lazy_any:nTF
        {
          {\legacy_if_p:n { measuring@ }}
          { \l__math_fakemath_bool }
          { \tl_if_blank_p:n {#1} }
        }
        {
          #1 $ % $
        }
        {
            \tag_socket_use:n  {math/inline/begin} %end P-MC
            \socket_use:nn {tagsupport/math/inline/formula/begin}{#1}
            $ % $
            \tag_socket_use:n  {math/inline/formula/end}
            \tag_socket_use:n  {math/inline/end} % restart P-MC
        }
  }
\cs_new_protected:Npn \__math_grab_dollar_delim:w #1 $ % $
  { \__math_grab_dollar:n {#1} }
\cs_new_protected:Npn \__math_grab_dollardollar:w % $$
  #1 $$
  {
    \tl_if_blank:nF {#1}
      {
        \__math_process:nn { equation* } {#1}
        \tag_socket_use:n {math/display/begin}
        \socket_use:nn{tagsupport/math/display/formula/begin}{#1}
      }
    $$
  }
\cs_new_protected:Npn \__math_tag_dollardollar_display_end:
  {
    %  \typeout{== tag dollarldollar display end}
    %  \ShowTagging{struct-stack}
    \para_raw_end:
    \tagpdfparaOn
    \l__math_tmpa_skip \lastskip
    \tag_socket_use:n{math/display/formula/end}
    \nobreak
    \skip_vertical:n { -\l__math_tmpa_skip  } % remove the negative belowdisplayskip
    \penalty \postdisplaypenalty
    \skip_vertical:n { -\l__math_tmpa_skip  } % insert the correct 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}
      {
        $ #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_grabbed_tl
\int_new:N \l__math_grab_env_int
\cs_new_protected:Npn \__math_grab_dollar_loop:
  {
    \group_begin:
      \tl_clear:N \l__math_grabbed_tl
      \__math_grab_loop:
  }
\cs_new_protected:Npn \__math_grab_loop:
  {
    \peek_remove_spaces:n
      {
        \peek_meaning:NTF \c_group_begin_token
          { \__math_grab_loop_group:n }
          { \__math_grab_loop_token:N }
      }
  }
\cs_new_protected:Npn \__math_grab_loop_group:n #1
  { \__math_grab_loop_store:n { {#1} } }
\cs_new_protected:Npn \__math_grab_loop_store:n #1
  {
    \tl_put_right:Nn \l__math_grabbed_tl {#1}
    \__math_grab_loop:
  }
\cs_new_protected:Npn \__math_grab_loop_token:N #1
  {
    \cs_if_exist_use:cF
      { __math_grab_loop_ \token_to_str:N #1 : }
      { \__math_grab_loop_store:n {#1} }
  }
\cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N $ : }
  { \__math_grab_loop_end: }
\cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \\ : }
  {
    \int_compare:nNnTF \l__math_grab_env_int = 0
       { \__math_grab_loop_newline: }
       { \__math_grab_loop_store:n { \\ } }
  }
\cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \begin : }
  {
    \int_incr:N \l__math_grab_env_int
    \__math_grab_loop_store:n { \begin }
  }
\cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \end : }
  {
    \int_compare:nNnTF \l__math_grab_env_int = 0
       {
         \__math_grab_loop_newline:
         \end
       }
       {
         \int_decr:N \l__math_grab_env_int
         \__math_grab_loop_store:n { \end }
       }
  }
\tl_map_inline:nn { \ignorespaces \unskip \textonly@unskip }
  {
    \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N #1 : }
      { \__math_grab_loop: }
  }
\cs_new_protected:Npn \__math_grab_loop_newline:
  {
    \if_false: { \fi:
    \cs_set_protected:Npn \cr
      {
        \__math_grab_loop:
        \tex_cr:D
      }
    \if_false: } \fi:
    \\
  }
\cs_new_protected:Npn \__math_grab_loop_end:
  {
    \exp_args:NNV \group_end:
    \__math_grab_dollar:n \l__math_grabbed_tl
  }
\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 { nne } \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_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 { nnee } \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 { nnee } \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
       {
        \tag_if_active:T
          {
            \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!
          }
       }
       {
         \__tag_tool_close_P:
       }
  \socket_use:nn{tagsupport/math/display/formula/begin}{}
  \tagpdfparaOff
}

\cs_new:Npn \@kernel@math@registered@end {
  \para_raw_end:
  \tagpdfparaOn
  \socket_use:n{tagsupport/math/display/formula/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 }
          { $ }
      }
      {
        \__math_grab_inline:w
      }
  } % \(
\cs_gset_protected:Npn \)
  {
    \mode_if_math:TF
      { $ }
      { \@badmath }
  }
\tl_gput_right:Nn \@kernel@before@begindocument
  {
    \cs_gset_protected:Npn \[ % \]
       {
         \__math_grab_eqn:w
 %       \bool_if:NTF \l__math_collected_bool
      } % \[
    \cs_gset_protected:Npn \]
      {
        \@badmath
      }
  }

\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* }
\RequirePackage{array}
\tl_if_in:NnT\@tabular{$}
 {
   \def\@tabular{%
   \leavevmode
   \UseTaggingSocket{tbl/hmode/begin}%
   \hbox \bgroup
   \bool_set_true:N \l__math_collected_bool
   $
   \bool_set_false:N \l__math_collected_bool
   \col@sep\tabcolsep \let\d@llarbegin\begingroup
                                    \let\d@llarend\endgroup
   \cs_set_protected:Npn \__math_grab_dollar:w { \__math_grab_dollar_loop: }
   \@tabarray}
 }
\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
  {
    \bool_set_true:N\l__math_collected_bool
  }
\tl_gput_right:Nn\@kernel@after@begindocument
 {
   \bool_set_false:N\l__math_collected_bool
 }

\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_suspend:n{\measuring}}
  \math_register_halign_env:nn {align}{}
  \math_register_halign_env:nn {align*}{}
  \math_register_halign_env:nn {alignat}{}
  \math_register_halign_env:nn {alignat*}{}
  \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
  }
\@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:NN #1#2 {
  \tl_set:Nf \l__math_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__math_tmpa_tl #1 #2
}
\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_protected:Npn \__math_split_at_nl_aux:nnNN #1 #2 #3 #4
  {
    \tl_gset:Nn #4 {#1}
    \tl_gset:Nn #3 {#2}
  }

\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
          { \__math_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 } }
  }

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


\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,
         alt = subformula,
         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-math.ltx'.
