%%
%% This is file `latex-lab-footnotes.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latex-lab-footnotes.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-footnotes.dtx
\ProvidesFile{latex-lab-footnotes.ltx}
        [2023-02-04 v0.7 changes to the footnote interfaces]
%% alterations not covered:

\ExplSyntaxOn
\prop_new:N \l__fnote_tmpa_prop
\tl_new:N   \l__fnote_tmpa_tl
\tl_new:N  \l_fnote_type_tl
\tl_set:Nn \l_fnote_type_tl {default}
\bool_new:N       \l_fnote_link_bool
\bool_set_true:N  \l_fnote_link_bool
\tl_new:N  \l_fnote_link_type_tl
\tl_set:Nn \l_fnote_link_type_tl {link}
\tl_new:N \l__fnote_linktarget_tl
\tl_new:N \l__fnote_currentlabel_tl

\seq_new:N  \l__fnote_currentrefs_seq
\bool_new:N       \l__fnote_autodetect_bool
\bool_set_true:N  \l__fnote_autodetect_bool
\tl_new:N  \l__fnote_currentstruct_tl
\tl_set:Nn \l__fnote_currentstruct_tl {1}
\cs_generate_variant:Nn \ref_label:nn { Vn }
\cs_generate_variant:Nn \ref_value:nn { Vn }
\cs_generate_variant:Nn \prop_gput:Nnn {cxn}
\cs_generate_variant:Nn \hook_gput_code:nnn{nne}
\cs_generate_variant:Nn \tag_struct_use:n {e}
\cs_new_protected:Npn \fnote_step_fnmark:nn #1#2 {
  \tl_if_novalue:nTF {#1}
    {
      \stepcounter {#2}
      \protected@xdef \@thefnmark { \use:c { the#2 } }
    }
    {
     \group_begin:
        \int_set:cn { c@#2 }{ #1 }
        \unrestored@protected@xdef \@thefnmark { \use:c { the#2 } }
     \group_end:
    }
}
\cs_new_protected:Npn \fnote_set_fnmark:nn #1#2 {
  \tl_if_novalue:nTF {#1}
    {
      \protected@xdef \@thefnmark { \use:c { the#2 } }
    }
    {
     \group_begin:
        \int_set:cn { c@#2 }{ #1 }
        \unrestored@protected@xdef \@thefnmark { \use:c { the#2 } }
     \group_end:
    }
}
\NewMirroredHookPair{fnmark/before}{fnmark/after}
\NewHook{fnmark}
\NewHook{fnmark/begin}
\NewHook{fnmark/end}
\NewMirroredHookPair{fntext/before}{fntext/after}
\NewHook{fntext}
\NewHook{fntext/para}
\NewHook{fntext/begin}
\NewHook{fntext/end}
\bool_new:N        \g_fnote_debug_bool
\bool_gset_true:N  \g_fnote_debug_bool
\cs_new_protected:Npn \__fnote_debug_footnotemark:
  {
    \bool_if:NT \g_fnote_debug_bool
       {
         \LogHook{fnmark/before}
         \LogHook{fnmark}
         \LogHook{fnmark/begin}
         \LogHook{fnmark/end}
         \LogHook{fnmark/after}
         \cs_gset_eq:NN \__fnote_debug_footnotemark: \prg_do_nothing:
       }
  }
\cs_new_protected:Npn \__fnote_debug_footnotetext:
  {
    \bool_if:NT \g_fnote_debug_bool
       {
         \cs_log:N\@footnotetext@cfgpoint
         \cs_log:N\@footnotetext@cfgpointii
         \cs_log:N\@footnotetext@cfgpointiii
         \cs_log:N\@footnotetext@cfgpointiv
         \cs_log:N\@makefntext@cfgpoint
         \cs_log:N\@makefntext@cfgpointii
         \LogHook{fntext/before}
         \LogHook{fntext}
         \LogHook{fntext/para}
         \LogHook{fntext/begin}
         \LogHook{fntext/end}
         \LogHook{fntext/after}
         \cs_gset_eq:NN \__fnote_debug_footnotetext: \prg_do_nothing:
       }
  }
\cs_new_protected:Npn \fnote_footnotemark: {
  \__fnote_debug_footnotemark:
  \UseHook{fnmark/before}
  \leavevmode
  \ifhmode
    \edef\@x@sf{\the\spacefactor}
    \UseHook{fnmark}
    \nobreak
  \fi
  \UseHook{fnmark/begin}
  \@kernel@process@makefnmark
     \@makefnmark
  \__fnote_footnotemark_finish:
}
\cs_new_protected:Npn \__fnote_finish: {
  \UseHook{fnmark/end}
  \ifhmode
    \spacefactor \@x@sf \relax
  \fi
  \UseHook{fnmark/after}
}

\cs_new_eq:NN \__fnote_footnotemark_finish: \__fnote_finish:
\def \@kernel@process@makefnmark { }
\cs_set_eq:NN  \@footnotemark \fnote_footnotemark:
\cs_new_protected:Npn \fnote_footnotetext:n #1 {
  \__fnote_debug_footnotetext:
  \UseHook{fntext/before}
  \@kernel@process@footnotetext
  \@footnotetext@cfgpoint {  % config point
    \UseHook{fntext}
    \reset@font
    \footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox
    \floatingpenalty \@MM
    \hsize\columnwidth
    \@parboxrestore
    \parindent 1em     % typical default used in \@makefntext moved up here
    \def\@currentcounter{footnote}
    \protected@edef \@currentlabel { \p@footnote \@thefnmark }
    \UseHook{fntext/para}
    \color@begingroup
      \@footnotetext@cfgpointii       % config point
        {
          \@footnotetext@cfgpointiii  % config point
          \UseHook{fntext/begin}
          \ignorespaces
          #1
          \UseHook{fntext/end}
          \@footnotetext@cfgpointiv   % config point
        }
      \par
    \color@endgroup
  }
  \UseHook{fntext/after}
}
\cs_new_protected:Npn \@footnotetext@cfgpoint    { \insert\footins }
\cs_new_protected:Npn \@footnotetext@cfgpointii  { \@makefntext }
\cs_new_protected:Npn \@footnotetext@cfgpointiii { \rule\z@\footnotesep }
\cs_new_protected:Npn \@footnotetext@cfgpointiv  { \@finalstrut\strutbox }
\cs_new_protected:Npn \@kernel@process@footnotetext {}
\AddToHook{begindocument}
  {
    \cs_set_eq:NN \@footnotetext \fnote_footnotetext:n
  }
\newdimen\footnotemargin
\footnotemargin\maxdimen         % no value given

\AtBeginDocument
  {
    \ifdim \footnotemargin=\maxdimen
      \setlength\footnotemargin{1.8em}
    \fi
  }

\cs_new_protected:Npn \fnote_makefntext:n #1 {
  \@makefntext@cfgpoint
      {
        \ifdim\footnotemargin>\z@
          \hb@xt@ \footnotemargin{\hss\@makefnmark}
        \else
          \ifdim\footnotemargin=\z@
            \llap{\@makefnmark}
          \else
          \ifdim\footnotemargin=-\maxdimen
               \@makefnmark
            \else
               \llap{\hb@xt@ -\footnotemargin{\@makefnmark\hss}}
            \fi
          \fi
        \fi
      }
  \@makefntext@cfgpointii
      { #1 }
}
\cs_new_protected:Npn \@makefntext@cfgpoint { \noindent }
\cs_new_protected:Npn \@makefntext@cfgpointii #1 { #1 }
\newcommand\old@std@class@makefntext[1]{%
    \parindent 1em%
    \noindent
    \hb@xt@1.8em{\hss\@makefnmark}#1}
\tl_new:N \l__fnote_patch_tl
\cs_new_eq:NN \__fnote_tmp:w \ERROR

\cs_new_protected:Npn \__fnote_patch:
  {
    \tl_set:No \l__fnote_patch_tl { \@makefntext { \@makefntext@cfgpointii{##1} } }
    \tl_if_in:NnTF \l__fnote_patch_tl { \hbox }
      { \cs_set_eq:NN \__fnote_tmp:w \__fnote_patch_hbox:w }
      {
        \tl_if_in:NnTF \l__fnote_patch_tl { \hb@xt@ }
          { \cs_set_eq:NN \__fnote_tmp:w \__fnote_patch_hb@xt@:w }
          {
            \tl_if_in:NnTF \l__fnote_patch_tl { \@makefnmark }
              { \cs_set_eq:NN \__fnote_tmp:w \__fnote_patch_@makefnmark:w }
              { \ERROR
                \cs_set_eq:NN \__fnote_tmp:w \exp_stop_f: }
          }
      }
    \tl_set:Nf \l__fnote_patch_tl
      { \exp_after:wN \__fnote_tmp:w \l__fnote_patch_tl }
    \cs_set:Npn \__fnote_tmp:w { \long \def \@makefntext ####1 }
    \exp_after:wN \__fnote_tmp:w \exp_after:wN { \l__fnote_patch_tl }
  }
\cs_new:Npn \__fnote_patch_hbox:w #1 \hbox #2 #
  { \exp_stop_f: #1 \@makefntext@processX { \hbox #2 } }
\cs_new:Npn \__fnote_patch_hb@xt@:w #1 \hb@xt@ #2 #
  { \exp_stop_f: #1 \@makefntext@processX { \hb@xt@ #2 } }
\cs_new:Npn \__fnote_patch_@makefnmark:w #1 \@makefnmark
  { \exp_stop_f: #1 \@makefntext@processX { \use:n } { \@makefnmark } }
\cs_new:Npn \@makefntext@processX #1#2{\@makefntext@cfgpoint{#1{#2}}}

\AddToHook{begindocument}
  {
    \cs_if_eq:NNTF \@makefntext \old@std@class@makefntext
      {
        \cs_set_eq:NN \@makefntext \fnote_makefntext:n
      }
      {
        \__fnote_patch:
      }
  }

\DeclareDocumentCommand\footnotetext {om}
  {
    \fnote_set_fnmark:nn {#1} \@mpfn
    \@footnotetext {#2}
  }
\DeclareDocumentCommand\footnote {om}
  {
    \fnote_step_fnmark:nn {#1} \@mpfn
    \cs_set_eq:NN \__fnote_footnotemark_finish: \prg_do_nothing:
    \@footnotemark
    \cs_set_eq:NN \__fnote_footnotemark_finish: \__fnote_finish:
    \@footnotetext {#2}
    \__fnote_footnotemark_finish:
  }

\DeclareDocumentCommand\footnotemark {o}
  {
    \fnote_step_fnmark:nn {#1} { footnote }
    \@footnotemark
  }

\DeclareDocumentCommand\footref {m}
  {
    \begingroup
      \unrestored@protected@xdef\@thefnmark{\ref*{#1}}%
    \endgroup
    \bool_set_false:N  \l__fnote_autodetect_bool
    \tl_set:Nn \l__fnote_currentlabel_tl {#1}
    \@footnotemark
    \bool_set_true:N  \l__fnote_autodetect_bool
  }
\cs_new_protected:Npn \@mpfootnotetext@cfgpoint #1
 {
   \global\setbox\@mpfootins\vbox{%
    \unvbox\@mpfootins
    #1
    }
 }
\RequirePackage{etoolbox}
\ExplSyntaxOff
\patchcmd\@iiiminipage
 {\let\@footnotetext\@mpfootnotetext}
 {\let\@footnotetext@cfgpoint\@mpfootnotetext@cfgpoint}
 {}{\ERROR}
\ExplSyntaxOn
\AddToHook{class/memoir/before}
  { \let\new@std@class@makecol\@makecol }
\AddToHook{class/memoir/after}
  {
    \cs_set_eq:NN  \@footnotemark \fnote_footnotemark:
    \cs_set_eq:NN  \@makefntext\old@std@class@makefntext
    \cs_set_eq:NN  \@makecol\new@std@class@makecol
  }
\AddToHook{package/setspace/after}
   {\let \@footnotetext \fnote_footnotetext:n
    \AddToHook{fntext}[setspace]{\let\baselinestretch\setspace@singlespace}}
\def\hyper@nopatch@footnote{}
\AtBeginDocument
  {
   \providecommand\hyper@linkstart{\@gobbletwo}
   \providecommand\hyper@linkend{\@empty}
  }
\AtBeginDocument
 {
   \@ifpackageloaded{hyperref}
     {
       \legacy_if:nF{Hy@hyperfootnotes}{\bool_set_false:N \l_fnote_link_bool}
     }
     {
       \bool_set_false:N \l_fnote_link_bool
     }
 }
\ref_attribute_gset:nnnn {fnote/struct}{1}{now}{\l__fnote_currentstruct_tl}
\cs_new_protected:Npn \__fnote_label_hook: {}
\AddToHook{begindocument/before}
  {
    \RequirePackage{nameref}
    \g@addto@macro\label@hook
      {
        \__fnote_label_hook:
      }
  }
\AddToHook{fntext/begin}
 {
   \cs_set_protected:Npn \__fnote_label_hook:
    {
      \ref_label:Vn \label@name {fnote/struct}
    }
 }
\cs_new_protected:Npn \fnote_new:nn #1 #2 % #1 name, #2 options
 {
   \prop_new:c { g__fnote_currentmarks_ #1 _prop }
 }

\fnote_new:nn {default}{}
\cs_new_protected:Npn \fnote_mark_gput:nn #1 #2 % #1 the representation of the mark, #2 type
 {
   \prop_gput:cxn { g__fnote_currentmarks_ #2 _prop }
     { \tag_get:n{struct_num} }
     { #1 }
 }

\cs_generate_variant:Nn \fnote_mark_gput:nn {no,oo}
\cs_new_protected:Npn \fnote_mark_gpop:nnN #1 #2 #3
  {
    \seq_clear:N #3
    \prop_set_eq:Nc     \l__fnote_tmpa_prop { g__fnote_currentmarks_ #2 _prop }
    \prop_map_inline:Nn \l__fnote_tmpa_prop
      {
        \tl_if_eq:nnT {#1} { ##2 }
          {
            \seq_put_right:Nn #3 { ##1 }
            \prop_gremove:cn { g__fnote_currentmarks_ #2 _prop } {##1}
          }
      }
  }
\cs_generate_variant:Nn\fnote_mark_gpop:nnN  {ooN}

\cs_new_protected:Npn \tag@FEMark #1  %#1 content
  {
    \tag_mc_end_push:
    \tag_struct_begin:n { tag=footnotemark }
     \bool_if:NTF \l__fnote_autodetect_bool
      {
        \fnote_mark_gput:oo {\@thefnmark}{\l_fnote_type_tl}
        \tl_set:Nx \l__fnote_linktarget_tl {footnote*.\tag_get:n{struct_num}}
      }
      {
        \hook_gput_code:nne {tagpdf/finish/before} {tagpdf/footnote}
         {
           \exp_not:N\fnote_gput_refs:ee
            { \tag_get:n{struct_num} }
            { \ref_value:Vn\l__fnote_currentlabel_tl {fnote/struct} }
         }
        \tl_set:Nx \l__fnote_linktarget_tl {footnote*.\ref_value:Vn\l__fnote_currentlabel_tl {fnote/struct}}
      }
     \tag_mc_begin:n{tag=Lbl}
     %
     \bool_if:NTF \l_fnote_link_bool
      {
        \exp_args:No
           \hyper@linkstart
           { \l_fnote_link_type_tl }
           { \l__fnote_linktarget_tl }
           #1
           \hyper@linkend
      }
      { #1 }
    \tag_mc_end:
    \tag_struct_end:
    \tag_mc_begin_pop:n{}
   }
\cs_set_eq:NN \@kernel@process@makefnmark \tag@FEMark


\cs_new_protected:Npn \__fnote_gput_ref:nn #1 #2 %#1 the structure number receiving the ref #2
  {
    \tag_if_active:T
      {
        \tag_struct_gput:nne {#1}{ref}{\tag_struct_object_ref:e { #2 }}
      }
  }
\cs_new_protected:Npn \fnote_gput_refs:nn #1 #2 % pair of numbers
  {
    \__fnote_gput_ref:nn {#1}{#2}
    \__fnote_gput_ref:nn {#2}{#1}
  }
\cs_generate_variant:Nn \fnote_gput_refs:nn {ee}
\tl_new:N \l__fnote_dflt_struct_tl
\tl_set:Nn \l__fnote_dflt_struct_tl {1}
\cs_new_protected:Npn \tag@FENote #1#2 % #1 config point, \insert\footins  #2 content
  {
    \tag_mc_end_push:
    \tag_check_child:nnTF {FENote}{pdf2}
      {
        \tag_struct_begin:n { tag=footnote }
      }
      {
        \tag_struct_begin:n { tag=footnote,parent=\l__fnote_dflt_struct_tl }
      }
    \tl_set:Nx \l__fnote_currentstruct_tl { \tag_get:n{struct_num} }
    \tag_struct_begin:n { tag=footnotelabel }\tag_struct_end:
    \bool_if:NTF \l__fnote_autodetect_bool
      {
        \fnote_mark_gpop:ooN { \@thefnmark }{ \l_fnote_type_tl } \l__fnote_currentrefs_seq
       \seq_map_inline:Nn \l__fnote_currentrefs_seq
         {
           \fnote_gput_refs:ee {##1}{ \l__fnote_currentstruct_tl }
         }
      }
      {%no auto

      }
      #1 {#2}
    \tag_struct_end:
    \tag_mc_begin_pop:n{}
  }
\cs_set_eq:NN \@kernel@process@footnotetext \tag@FENote
\cs_set_protected:Npn \@makefntext@cfgpoint #1 %#1 code that typesets the mark.
  {
    \noindent
    \tag_mc_end_push:
    \tag@FENoteLbl { #1 }
    \tag_mc_begin_pop:n{}
  }
\cs_new_protected:Npn \tag@FENoteLbl #1
  {
     %\seq_show:N\l__fnote_currentrefs_seq
     \seq_map_inline:Nn\l__fnote_currentrefs_seq {\MakeLinkTarget*{footnote*.##1}}
     \MakeLinkTarget*{footnote*.\l__fnote_currentstruct_tl}
     \tag_struct_begin:n { tag=NonStruct,parent=\l__fnote_currentstruct_tl +1 }
      \tag_mc_begin:n { tag=Lbl }
       #1
      \tag_mc_end:
     \tag_struct_end:
  }
\cs_set_protected:Npn \@makefntext@cfgpointii #1
  {
    \tag_mc_end_push:
    \tag@FENotetext { #1 }
    \tag_mc_begin_pop:n{}
  }
\cs_new_protected:Npn \tag@FENotetext #1
  {
    \tag_mc_begin:n{}
    #1
    \tag_mc_end:
  }

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