%%
%% This is file `projlib-author.sty',
%% generated with the docstrip utility.
%%
%% Copyright (C) 2021-2024 by Jinwen XU
%% 
%% This is part of the ProjLib Toolkit.
%% 
%% 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. 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
%% 2005/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}[2022-06-01]
\ProvidesExplPackage
  {projlib-author}
  {2024/03/01} {}
  {Enhanced author information block}

\keys_define:nn { projlib-author }
  {
    , ams-fashion .bool_set:N = \l__projlib_author_amsfashion_bool
    , ams-fashion .initial:n  = { false }
    , ams~fashion .bool_set:N = \l__projlib_author_amsfashion_bool
    , ams fashion .bool_set:N = \l__projlib_author_amsfashion_bool
    , AMS-fashion .bool_set:N = \l__projlib_author_amsfashion_bool
    , AMS~fashion .bool_set:N = \l__projlib_author_amsfashion_bool
    , AMS fashion .bool_set:N = \l__projlib_author_amsfashion_bool

    , unknown     .code:n     = {}
  }
\ProcessKeyOptions [ projlib-author ]

\cs_if_exist:NT \subjclass { \endinput }

\prg_new_conditional:Nnn \projlib_author_if_content_empty:n { T, F, TF }
  {
    \hbox_set:Nn \l_tmpa_box { #1 }
    \dim_compare:nNnTF { \box_wd:N \l_tmpa_box } > \c_zero_dim
      { \prg_return_false: }
      { \prg_return_true: }
  }

\tl_new:N   \g__projlib_author_font_author_tl
\tl_new:N   \g__projlib_author_font_date_tl
\tl_new:N   \g__projlib_author_font_institute_tl
\tl_new:N   \g__projlib_author_font_address_tl
\tl_new:N   \g__projlib_author_font_curraddr_tl
\tl_new:N   \g__projlib_author_font_email_tl

\tl_gset:Nn \g__projlib_author_font_author_tl      { \normalsize \normalfont \scshape }
\tl_gset:Nn \g__projlib_author_font_date_tl      { \normalsize \normalfont }
\tl_gset:Nn \g__projlib_author_font_institute_tl   { \small \normalfont }
\tl_gset:Nn \g__projlib_author_font_address_tl     { \small \normalfont \itshape }
\tl_gset:Nn \g__projlib_author_font_curraddr_tl    { \small \normalfont \itshape }
\tl_gset:Nn \g__projlib_author_font_email_tl       { \small \normalfont \ttfamily }

\dim_new:N  \g__projlib_author_groupsep_dim
\dim_new:N  \g__projlib_author_innersep_dim
\dim_new:N  \g__projlib_author_affilsep_dim
\dim_new:N  \g__projlib_author_aftersep_dim
\dim_new:N  \g__projlib_author_emptysep_dim

\dim_set:Nn \g__projlib_author_groupsep_dim { 0.5 \baselineskip }
\dim_set:Nn \g__projlib_author_innersep_dim { .25 \baselineskip }
\dim_set:Nn \g__projlib_author_affilsep_dim {   0 \baselineskip }
\dim_set:Nn \g__projlib_author_aftersep_dim { .15 \baselineskip }
\dim_set:Nn \g__projlib_author_emptysep_dim { .15 \baselineskip }

\bool_new:N \g__projlib_author_affil_already_exist_bool

\NewCommandCopy \projlib_author_backup_author:w \author
\RenewDocumentCommand \author { m }
  {
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \bool_set_false:N \g__projlib_author_affil_already_exist_bool
        % If there is nothing before, the following line does nothing
        % If there is an affil before, this will backward one line
        \phantom {A} \\[-\baselineskip]
        \projlib_author_if_content_empty:nTF { #1 }
          {
            \skip_vertical:n { \g__projlib_author_emptysep_dim }
            \phantom {}
          }
          {
            \skip_vertical:n { \g__projlib_author_groupsep_dim }
            { \g__projlib_author_font_author_tl #1 }
            \\ \phantom {}
          }
      }
  }

\cs_new_protected:Nn \projlib_author_affil_generic:nn
  % #1 = the item
  % #2 = the text
  {
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \phantom {A} \\[-\baselineskip]
        \bool_if:NTF \g__projlib_author_affil_already_exist_bool
          {
            \skip_vertical:n { \g__projlib_author_affilsep_dim }
          }
          {
            \skip_vertical:n { \g__projlib_author_innersep_dim }
          }
        { \use:c { g__projlib_author_font_ #1 _tl } #2 }
        \bool_set_true:N \g__projlib_author_affil_already_exist_bool
        \\ \phantom {}
      }
  }

\ProvideDocumentCommand \institute { m }
  {
    \projlib_author_affil_generic:nn { institute } { #1 }
  }

\ProvideDocumentCommand \address { m }
  {
    \projlib_author_affil_generic:nn { address } { #1 }
  }

\ProvideDocumentCommand \curraddr { m }
  {
    \projlib_author_affil_generic:nn { curraddr } { #1 }
  }

\ProvideDocumentCommand \email { m }
  {
    \projlib_author_affil_generic:nn { email } { #1 }
  }

\RequirePackage { regexpatch }

\xpatchcmd { \@maketitle } { \begin{tabular}[t]{c} } {}
  {
    \xpatchcmd { \@maketitle } { \end{tabular} } {} {} {}
  } {}

\tl_set:cn {@author} {}

\hook_gput_code:nnn { cmd/@maketitle/before } { projlib-author }
  {
    \hook_gput_code:nnn { cmd/@author/before } { projlib-author }
      {
        \skip_vertical:n { - \g__projlib_author_groupsep_dim }
      }
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \skip_vertical:n { - \baselineskip }
        \bool_if:NT \g__projlib_author_affil_already_exist_bool
          {
            \skip_vertical:n { \g__projlib_author_aftersep_dim }
          }
      }
  }

\bool_if:NT \l__projlib_author_amsfashion_bool
  {
    \RequirePackage { projlib-language }

    \tl_const:Nn \c_projlib_author_dot_tl { . }

    \tl_new:N   \g_projlib_author_keywordsname_style_tl
    \tl_gset:Nn \g_projlib_author_keywordsname_style_tl { \itshape }

    \projlib_langauge_define_multilingual_text:Nn \keywordsname
      {
        EN = Key~words~and~phrases                  ,
        FR = Mots~et~phrases~clés                   ,
        DE = Schlüsselwörter~und~Phrasen            ,
        IT = Parole~e~frasi~chiave                  ,
        PT = Palavras~e~frases~chave                ,
        BR = Palavras~e~frases~chave                ,
        ES = Palabras~y~frases~clave                ,
        CN = 关键词                                 ,
        TC = 關鍵詞                                 ,
        JP = キーワード                             ,
        RU = Ключевые~слова~и~фразы                 ,
      }

    \tl_new:N \g__projlib_author_keywords_tl
    \NewDocumentCommand \keywords { m }
      {
        \tl_gset:Nn \g__projlib_author_keywords_tl { #1 }
      }

    \tl_new:N \g__projlib_author_dedicatory_tl
    \NewDocumentCommand \dedicatory { m }
      {
        \tl_gset:Nn \g__projlib_author_dedicatory_tl { #1 }
      }

    \NewCommandCopy \projlib_author_backup_footnotemark: \footnotemark

    \NewCommandCopy \projlib_author_backup_thanks:w \thanks
    \cs_undefine:N \thanks

    % measure the width of "*"
    \hbox_set:Nn \l_tmpa_box { * }
    \dim_const:Nn \c__projlib_author_width_of_asterisk_dim { \box_wd:N \l_tmpa_box }

    \cs_new_nopar:Nn \projlib_author_pre_thanks:n
      {
        \tl_set:Nn \footnotemark {}
        \projlib_author_backup_thanks:w
          {
            \skip_horizontal:n { - \c__projlib_author_width_of_asterisk_dim }
            #1
          }
        \RenewCommandCopy \footnotemark \projlib_author_backup_footnotemark:
      }

    \NewDocumentCommand \thanks { m }
      {
        \tl_if_blank:nF { #1 }
          {
            \hook_gput_code:nnn { cmd/@title/after } { projlib-author }
              {
                \projlib_author_pre_thanks:n { #1 }
              }
          }
      }

    \hook_gput_code:nnn { begindocument/before } { projlib-author }
      {
        \NewCommandCopy \projlib_author_backup_my_author:w \author
        \RenewDocumentCommand \author { m }
          {
            \projlib_author_backup_my_author:w
              {
                \RenewCommandCopy \thanks \projlib_author_backup_thanks:w
                #1
              }
          }
      }

    \NewCommandCopy \projlib_author_backup_abstract: \abstract
    \NewCommandCopy \projlib_author_backup_endabstract: \endabstract
    \cs_undefine:N \abstract
    \cs_undefine:N \endabstract

    \bool_new:N \g__projlib_author_already_maketitle_bool
    \bool_gset_false:N \g__projlib_author_already_maketitle_bool
    \bool_new:N \g__projlib_author_abstract_before_maketitle_bool
    \bool_gset_false:N \g__projlib_author_abstract_before_maketitle_bool

    \RequirePackage { scontents }
    \newenvsc { abstract } [ store-env = projlib-author-abstract, print-env = false ]

    \hook_gput_code:nnn { env/abstract/after } { projlib-author }
      {
        \bool_if:NTF \g__projlib_author_already_maketitle_bool
          {
            \group_begin:
            \projlib_author_backup_abstract:
              \getstored{projlib-author-abstract}
            \projlib_author_backup_endabstract:
            \group_end:
          }
          {
            \bool_gset_true:N \g__projlib_author_abstract_before_maketitle_bool
          }
      }

    \hook_gput_code:nnn { begindocument/before } { projlib-author }
      {
        \makeatletter
        \xpatchcmd { \@maketitle } { \@date }
          {
            \projlib_author_if_content_empty:nTF { \@date } { \skip_vertical:n { -2 \baselineskip } } { \g__projlib_author_font_date_tl \@date }
            \projlib_author_if_content_empty:nF { \g__projlib_author_dedicatory_tl }
              {
                \projlib_author_if_content_empty:nT { \@date } { \skip_vertical:n { 2 \baselineskip } }
                \par
                \skip_vertical:n { -\parskip }
                \skip_vertical:n { \baselineskip }
                \textit{ \g__projlib_author_dedicatory_tl }
                \projlib_author_if_content_empty:nT { \@date }
                  {
                    \skip_vertical:n { -\parskip }
                    \skip_vertical:n { \dim_eval:n { -1.7 \baselineskip } }
                  }
              }
          } {} {}
        \makeatother

        \hook_gput_code:nnn { cmd/maketitle/before } { projlib-author }
          {
            \projlib_author_if_content_empty:nF { \g__projlib_author_keywords_tl }
              {
                \projlib_author_pre_thanks:n
                  {
                    \group_begin:
                      \g_projlib_author_keywordsname_style_tl
                      \keywordsname \c_projlib_author_dot_tl
                    \group_end:
                    \c_space_tl
                    \g__projlib_author_keywords_tl
                  }
              }
          }

        \hook_gput_code:nnn { cmd/maketitle/after } { projlib-author }
          {
            \bool_if:NT \g__projlib_author_abstract_before_maketitle_bool
              {
                \group_begin:
                \projlib_author_backup_abstract:
                  \getstored{projlib-author-abstract}
                \projlib_author_backup_endabstract:
                \group_end:
              }
            \cs_undefine:N \institute
            \cs_undefine:N \address
            \cs_undefine:N \curraddr
            \cs_undefine:N \email
            \cs_undefine:N \dedicatory
            \cs_undefine:N \keywords
            % \cs_undefine:N \footnotemark
            % \NewCommandCopy \footnotemark \projlib_author_backup_footnotemark:
            \bool_gset_true:N \g__projlib_author_already_maketitle_bool
          }
      }

      \tl_new:N \l__projlib_author_subjclassname_tl
      \tl_new:N \l__projlib_author_subjclass_tl
      \msg_new:nnn { projlib-author }
        { unknown-msc-edition }
        { Unknown~edition~(#1)~of~Mathematics~Subject~Classification;~using~'2020' }
      \NewDocumentCommand \subjclass { O{2020} m }
        {
          \tl_set:Nn \l__projlib_author_subjclass_tl { #2 }
          \tl_if_exist:cTF { subjclassname_#1 }
            {
              \tl_set:Nn \l__projlib_author_subjclassname_tl { \tl_use:c { subjclassname_#1 } }
            }
            {
              \msg_warning:nnn { projlib-author } { unknown-msc-edition } { #1 }
              \tl_set:Nn \l__projlib_author_subjclassname_tl { \tl_use:c { subjclassname_2020 } }
            }
          \projlib_author_pre_thanks:n
            {
              \textit{ \l__projlib_author_subjclassname_tl \c_projlib_author_dot_tl }
              \enspace
              \l__projlib_author_subjclass_tl .
            }
        }
      \tl_const:cn { subjclassname_1991 } { \textup{1991}~Mathematics~Subject~Classification }
      \tl_const:cn { subjclassname_2000 } { \textup{2000}~Mathematics~Subject~Classification }
      \tl_const:cn { subjclassname_2010 } { \textup{2010}~Mathematics~Subject~Classification }
      \tl_const:cn { subjclassname_2020 } { \textup{2020}~Mathematics~Subject~Classification }
  }

\endinput
%%
%% End of file `projlib-author.sty'.
