%%
%% This is file `secnum.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% secnum.dtx  (with options: `package')
%% 
%% Copyright (C) 2019--
%% Gau, Syu ( GauSyu@Gmail.com )
%% 
%% ----------------------------------------------------------------------
%% 
%% This work 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. This version of this license is in
%%    http://www.latex-project.org/lppl/lppl-1-3c.txt
%% and the latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is
%%   Gau, Syu ( GauSyu@Gmail.com ).
%% 
%% ----------------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,xparse,l3keys2e}
\ProvidesExplPackage{secnum}{2022/01/30}{}
  { An intuitive way to format section numbering }
\int_new:N \g__syu_secnum_depth
\int_new:N \g__syu_ifchapter_int
\if_cs_exist:N \thechapter
  \int_gset:Nn \g__syu_ifchapter_int 1
\else:
  \int_gset:Nn \g__syu_ifchapter_int 0
\fi:
\tl_gset:Nx \g__syu_secnum_bkmr {,}
\cs_generate_variant:Nn \tl_if_in:NnTF { NV }
\cs_generate_variant:Nn \tl_remove_all:Nn { NV }
\tl_new:N \g__syu_chapter_tl
\tl_new:N \g__syu_chapter_section_tl
\tl_new:N \g__syu_section_tl
\tl_new:N \g__syu_section_subsection_tl
\tl_new:N \g__syu_subsection_tl
\tl_new:N \g__syu_subsection_subsubsection_tl
\tl_new:N \g__syu_subsubsection_tl
\tl_new:N \g__syu_subsubsection_paragraph_tl
\tl_new:N \g__syu_paragraph_tl
\tl_new:N \g__syu_paragraph_subparagraph_tl
\tl_new:N \g__syu_subparagraph_tl
\keys_define:nn { syu / options }{
  tocdep .code:n = {
    \int_const:Nn \g__syu_tocdep {#1}
    \setcounter{tocdepth}{ \g__syu_tocdep }
  },
  breaking .code:n = {
    \tl_gset:Nx \g__syu_secnum_bkmr {#1}
  },
}
\ProcessKeysOptions{ syu / options }
\DeclareDocumentCommand{\setsecnum}{m}{
  \tl_set:Nn \l__syu_secnum_tl {#1}
  \__syu_secnum_unabbr:N \l__syu_secnum_tl
  \__syu_split_by_macros:NNN
    \l__syu_secnum_tl \l__syu_secnum_seq \g__syu_secnum_depth
  \__syu_secnum_from_seq:N \l__syu_secnum_seq
  \setcounter{secnumdepth}{
    \int_eval:n { \g__syu_secnum_depth - \g__syu_ifchapter_int }
  }
  \int_if_exist:NTF \g__syu_tocdep {
    \setcounter{tocdepth}{ \g__syu_tocdep }
  }{
    \setcounter{tocdepth}{
      \int_eval:n { \g__syu_secnum_depth - \g__syu_ifchapter_int }
    }
  }
  \__syu_secnum:
}
\cs_new_protected:Npn \__syu_secnum_unabbr:N #1 {
  \regex_replace_all:nnN {A} {\c{Alph}} #1
  \regex_replace_all:nnN {a} {\c{alph}} #1
  \regex_replace_all:nnN {I} {\c{Roman}} #1
  \regex_replace_all:nnN {i} {\c{roman}} #1
  \regex_replace_all:nnN {1} {\c{arabic}} #1
}
\cs_new_protected:Npn \__syu_split_by_macros:NNN #1 #2 #3 {
  \tl_set:Nn \l_tmpa_tl {S}
  \seq_clear:N #2
  \int_set:Nn #3 {0}
  \tl_map_inline:Nn #1 {
    \__syu_if_macro:nTF ##1 {
      \seq_put_right:NV #2 \l_tmpa_tl
      \tl_clear:N \l_tmpa_tl
      \tl_put_right:Nn \l_tmpa_tl ##1
      \seq_put_right:NV #2 \l_tmpa_tl
      \tl_clear:N \l_tmpa_tl
      \int_incr:N #3
    }{
      \tl_put_right:Nn \l_tmpa_tl ##1
    }
  }
}
\tl_new:N \g__syu_macro_tl
\tl_set:Nx \g__syu_macro_tl { \meaning \g__syu_macro_tl }
\tl_gset:Nx \g__syu_macro_tl { \tl_range:Nnn \g__syu_macro_tl {1}{5} }
\prg_new_protected_conditional:Npnn \__syu_if_macro:n #1 { T , F , TF }{
  \group_begin:
    \tl_set:Nx \l_tmpa_tl {\meaning #1}
    \tl_set:Nx \l_tmpa_tl {\tl_range:Nnn \l_tmpa_tl {1} {5}}
  \exp_after:wN
  \group_end:
    \if_meaning:w \l_tmpa_tl \g__syu_macro_tl
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
}
\cs_new_protected:Npn \__syu_secnum_from_seq:N #1 {
  \int_if_odd:nTF \g__syu_ifchapter_int {
    \tl_gset:Nx \g__syu_chapter_tl
      { \seq_item:Nn #1 { 2 } }
    \tl_gset:Nx \g__syu_chapter_section_tl
      { \seq_item:Nn #1 { 3 } }
    \tl_gset:Nx \g__syu_section_tl
      { \seq_item:Nn #1 { 4 } }
    \tl_gset:Nx \g__syu_section_subsection_tl
      { \seq_item:Nn #1 { 5 } }
    \tl_gset:Nx \g__syu_subsection_tl
      { \seq_item:Nn #1 { 6 } }
    \tl_gset:Nx \g__syu_subsection_subsubsection_tl
      { \seq_item:Nn #1 { 7 } }
    \tl_gset:Nx \g__syu_subsubsection_tl
      { \seq_item:Nn #1 { 8 } }
    \tl_gset:Nx \g__syu_subsubsection_paragraph_tl
      { \seq_item:Nn #1 { 9 } }
    \tl_gset:Nx \g__syu_paragraph_tl
      { \seq_item:Nn #1 { 10 } }
    \tl_gset:Nx \g__syu_paragraph_subparagrah_tl
      { \seq_item:Nn #1 { 11 } }
    \tl_gset:Nx \g__syu_subparagraph_tl
      { \seq_item:Nn #1 { 12 } }
  }{
    \tl_gset:Nx \g__syu_section_tl
      { \seq_item:Nn #1 { 2 } }
    \tl_gset:Nx \g__syu_section_subsection_tl
      { \seq_item:Nn #1 { 3 } }
    \tl_gset:Nx \g__syu_subsection_tl
      { \seq_item:Nn #1 { 4 } }
    \tl_gset:Nx \g__syu_subsection_subsubsection_tl
      { \seq_item:Nn #1 { 5 } }
    \tl_gset:Nx \g__syu_subsubsection_tl
      { \seq_item:Nn #1 { 6 } }
    \tl_gset:Nx \g__syu_subsubsection_paragraph_tl
      { \seq_item:Nn #1 { 7 } }
    \tl_gset:Nx \g__syu_paragraph_tl
      { \seq_item:Nn #1 { 8 } }
    \tl_gset:Nx \g__syu_paragraph_subparagrah_tl
      { \seq_item:Nn #1 { 9 } }
    \tl_gset:Nx \g__syu_subparagraph_tl
      { \seq_item:Nn #1 { 10 } }
  }
}
\cs_new:Nn \__syu_secnum: {
  \if_cs_exist:N \thechapter
    \renewcommand*{\thechapter}{ \g__syu_chapter_tl {chapter} }
    \tl_if_in:NVTF \g__syu_chapter_section_tl \g__syu_secnum_bkmr {
      \tl_remove_all:NV
        \g__syu_chapter_section_tl \g__syu_secnum_bkmr
      \renewcommand*{\thesection}{ \g__syu_section_tl {section} } 
      \makeatletter
        \renewcommand*{\p@section}{
          \thechapter\g__syu_chapter_section_tl
        }
        \renewcommand*{\p@subsection}{ \p@section}
        \renewcommand*{\p@subsubsection}{ \p@section }
        \renewcommand*{\p@paragraph}{ \p@section}
        \renewcommand*{\p@subparagraph}{ \p@section }
      \makeatother
    }{
      \renewcommand*{\thesection}{
        \thechapter\g__syu_chapter_section_tl
        \g__syu_section_tl {section}
      }
    }
  \else:
    \renewcommand*{\thesection}{ \g__syu_section_tl {section} }
  \fi:
  \tl_if_empty:NF \g__syu_subsection_tl {
    \tl_if_in:NVTF \g__syu_section_subsection_tl \g__syu_secnum_bkmr {
      \tl_remove_all:NV
        \g__syu_section_subsection_tl \g__syu_secnum_bkmr
      \renewcommand*{\thesubsection}{ \g__syu_subsection_tl {subsection} }
      \makeatletter
        \renewcommand*{\p@subsection}{
          \p@section\g__syu_section_tl{section}
          \g__syu_section_subsection_tl
        }
        \renewcommand*{\p@subsubsection}{ \p@subsection }
        \renewcommand*{\p@paragraph}{ \p@subsection}
        \renewcommand*{\p@subparagraph}{ \p@subsection }
      \makeatother
    }{
      \renewcommand*{\thesubsection}{
        \thesection\g__syu_section_subsection_tl
        \g__syu_subsection_tl {subsection}
      }
    }
}
  \tl_if_empty:NF \g__syu_subsubsection_tl {
    \tl_if_in:NVTF \g__syu_subsection_subsubsection_tl \g__syu_secnum_bkmr {
      \tl_remove_all:NV
        \g__syu_subsection_subsubsection_tl \g__syu_secnum_bkmr
      \renewcommand*{\thesubsubsection}
        { \g__syu_subsubsection_tl {subsubsection} }
      \makeatletter
        \renewcommand*{\p@subsubsection}{
          \p@subsection\g__syu_subsection_tl {subsection}
          \g__syu_subsection_subsubsection_tl
        }
        \renewcommand*{\p@paragraph}{ \p@subsubsection}
        \renewcommand*{\p@subparagraph}{ \p@subsubsection }
      \makeatother
    }{
      \renewcommand*{\thesubsubsection}{
        \thesubsection\g__syu_subsection_subsubsection_tl
        \g__syu_subsubsection_tl {subsubsection}
      }
    }
}
  \tl_if_empty:NF \g__syu_paragraph_tl {
    \tl_if_in:NVTF \g__syu_subsubsection_paragraph_tl \g__syu_secnum_bkmr {
      \tl_remove_all:NV
        \g__syu_subsubsection_paragraph_tl \g__syu_secnum_bkmr
      \renewcommand*{\theparagraph}{ \g__syu_paragraph_tl {paragraph} }
      \makeatletter
        \renewcommand*{\p@paragraph}{
          \p@subsubsection\g__syu_subsubsection_tl {subsubsection}
          \g__syu_subsubsection_paragraph_tl
        }
        \renewcommand*{\p@subparagraph}{ \p@paragraph }
      \makeatother
    }{
      \renewcommand*{\theparagraph}{
        \thesubsubsection\g__syu_subsubsection_paragraph_tl
        \g__syu_paragraph_tl {paragraph}
      }
    }
}
  \tl_if_empty:NF \g__syu_subparagraph_tl {
    \tl_if_in:NVTF \g__syu_paragraph_subparagrah_tl \g__syu_secnum_bkmr {
      \tl_remove_all:NV
        \g__syu_paragraph_subparagrah_tl \g__syu_secnum_bkmr
      \renewcommand*{\thesubparagraph}{ \g__syu_subparagraph_tl {subparagraph} }
      \makeatletter
        \renewcommand*{\p@subparagraph}{
          \p@paragraph\g__syu_paragraph_tl {paragraph}
          \g__syu_paragraph_subparagrah_tl
        }
      \makeatother
    }{
      \renewcommand*{\thesubparagraph}{
        \theparagraph\g__syu_paragraph_subparagrah_tl
        \g__syu_subparagraph_tl {subparagraph}
      }
    }
  }
}
\endinput
%%
%% End of file `secnum.sty'.
