%%
%% This is file `ctex-book.def',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% ctex.dtx  (with options: `heading,book')
%% 
%%     Copyright (C) 2003--2015
%%     CTEX.ORG and any individual authors listed in the documentation.
%% ------------------------------------------------------------------------------
%% 
%%     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 Maintainers of this work are Leo Liu, Qing Lee and Liam Huang.
%% 
%% ------------------------------------------------------------------------------
%% 
\GetIdInfo$Id: ctex.dtx 3d246c0 2015-05-11 23:20:45 +0800 Qing Lee <sobenlee@gmail.com> $
  {Heading modification for book (CTEX)}
\ProvidesExplFile{ctex-book.def}
  {\ExplFileDate}{2.0}{\ExplFileDescription}
\seq_new:N \c__ctex_headings_seq
\seq_gset_from_clist:Nn \c__ctex_headings_seq
  {
    part , chapter , section , subsection , subsubsection ,
    paragraph , subparagraph
  }
\cs_new_protected_nopar:Npn \__ctex_initial_heading:n #1
  {
    \tl_new:c { CTEX@pre#1 }
    \tl_new:c { CTEX@post#1 }
    \tl_const:cx { CTEXthe#1 }
      {
        \exp_not:c { CTEX@pre#1 }
        \exp_not:c { CTEX@the#1 }
        \exp_not:c { CTEX@post#1 }
      }
    \tl_const:cx { CTEX@#1name }
      {
        \exp_not:c { CTEX@#1@nameformat }
        \exp_not:c { CTEX@pre#1 }
        \exp_not:N \tl_if_empty:NTF \exp_not:c { CTEX@#1@numberformat }
          { \exp_not:c { CTEX@the#1 } }
          {
            \group_begin:
              \exp_not:c { CTEX@#1@numberformat }
              \exp_not:c { CTEX@the#1 }
            \group_end:
          }
        \exp_not:c { CTEX@post#1 }
        \exp_not:c { CTEX@#1@aftername }
      }
  }
\cs_new_protected_nopar:Npn \__ctex_def_heading_keys:n #1
  {
    \tl_put_right:Nx \l__ctex_tmp_tl
      {
        #1                 .meta:nn = { ctex / #1 } { ####1 } ,
        #1 / name           .code:n =
          { \ctex_assign_heading_name:nn {#1} { ####1 } } ,
        #1 / number       .tl_set:N = \exp_not:c { CTEX@the#1 } ,
        #1 / format       .tl_set:N = \exp_not:c { CTEX@#1@format } ,
        #1 / nameformat   .tl_set:N = \exp_not:c { CTEX@#1@nameformat } ,
        #1 / numberformat .tl_set:N = \exp_not:c { CTEX@#1@numberformat } ,
        #1 / aftername    .tl_set:N = \exp_not:c { CTEX@#1@aftername } ,
        #1 / titleformat  .tl_set:N = \exp_not:c { CTEX@#1@titleformat } ,
        #1 / beforeskip   .tl_set:N = \exp_not:c { CTEX@#1@beforeskip } ,
        #1 / afterskip    .tl_set:N = \exp_not:c { CTEX@#1@afterskip} ,
        #1 / indent       .tl_set:N = \exp_not:c { CTEX@#1@indent } ,
        #1 / format+        .code:n =
          { \tl_put_right:Nn \exp_not:c { CTEX@#1@format } { ####1 } } ,
        #1 / nameformat+    .code:n =
          { \tl_put_right:Nn \exp_not:c { CTEX@#1@nameformat } { ####1 } } ,
        #1 / numberformat+  .code:n =
          { \tl_put_right:Nn \exp_not:c { CTEX@#1@numberformat } { ####1 } } ,
        #1 / aftername+     .code:n =
          { \tl_put_right:Nn \exp_not:c { CTEX@#1@aftername } { ####1 } } ,
        #1 / titleformat+   .code:n =
          { \tl_put_right:Nn \exp_not:c { CTEX@#1@titleformat } { ####1 } } ,
        #1 / beforeskip  .initial:n = \c_zero_skip ,
        #1 / afterskip   .initial:n = \c_zero_skip ,
        #1 / indent      .initial:n = \c_zero_dim ,
        #1 / beforeskip  .value_required: ,
        #1 / afterskip   .value_required: ,
        #1 / indent      .value_required: ,
      }
  }
\NewDocumentCommand \ctex_assign_heading_name:nn
  { m > { \SplitArgument { \c_one } { , } } +m }
  { \__ctex_assign_heading_name:nnn {#1} #2 }
\cs_new_protected:Npn \__ctex_assign_heading_name:nnn #1#2#3
  {
    \tl_set:cn { CTEX@pre#1 } {#2}
    \IfNoValueTF {#3}
      { \tl_clear:c { CTEX@post#1 } }
      { \tl_set:cn { CTEX@post#1 } {#3} }
  }
\tl_clear:N \l__ctex_tmp_tl
\seq_map_inline:Nn \c__ctex_headings_seq
  {
    \__ctex_initial_heading:n {#1}
    \__ctex_def_heading_keys:n {#1}
  }
\use:x { \keys_define:nn { ctex } { \exp_not:o { \l__ctex_tmp_tl } } }
\tl_clear:N \l__ctex_tmp_tl
\keys_define:nn { ctex }
  {
    part    / pagestyle .tl_set:N = \CTEX@part@pagestyle ,
    chapter / pagestyle .tl_set:N = \CTEX@chapter@pagestyle
  }
\def\@part[#1]#2{%
  \thispagestyle{\CTEX@part@pagestyle}%
  \ifnum \c@secnumdepth >-2\relax
    \refstepcounter{part}%
    \addcontentsline{toc}{part}{\CTEXthepart\hspace{1em}#1}%
  \else
    \addcontentsline{toc}{part}{#1}%
  \fi
  \markboth{}{}%
  {\interlinepenalty \@M
   \normalfont \CTEX@part@format
   \ifnum \c@secnumdepth >-2\relax
     \CTEX@partname
   \fi
   \CTEX@part@titleformat{#2}\par}%
  \@endpart}
\def\@spart#1{%
    {\interlinepenalty \@M
     \normalfont \CTEX@part@format
     \CTEX@part@titleformat{#1}\par}%
    \@endpart}
\def\@chapter[#1]#2{%
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \refstepcounter{chapter}%
      \typeout{\CTEXthechapter}%
      \addcontentsline{toc}{chapter}
        {\protect\numberline{\CTEXthechapter\hspace{0.3em}}#1}%
    \else
      \addcontentsline{toc}{chapter}{#1}%
    \fi
  \else
    \addcontentsline{toc}{chapter}{#1}%
  \fi
  \chaptermark{#1}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
  \else
    \@makechapterhead{#2}%
  \@afterheading
  \fi}
\def\@makechapterhead#1{%
  \thispagestyle{\CTEX@chapter@pagestyle}%
  \@tempskipa \CTEX@chapter@beforeskip \relax
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \else
    \@afterindenttrue
  \fi
  \vspace*{\@tempskipa}%
  {\normalfont \parindent \CTEX@chapter@indent \CTEX@chapter@format
   \ifnum \c@secnumdepth >\m@ne
     \if@mainmatter
       \CTEX@chaptername
     \fi
   \fi
   \interlinepenalty\@M
   \CTEX@chapter@titleformat{#1}\par\nobreak
   \vskip \CTEX@chapter@afterskip
  }}
\def\@makeschapterhead#1{%
  \thispagestyle{\CTEX@chapter@pagestyle}%
  \@tempskipa \CTEX@chapter@beforeskip \relax
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \else
    \@afterindenttrue
  \fi
  \vspace*{\@tempskipa}%
  {\normalfont \parindent \CTEX@chapter@indent \CTEX@chapter@format
   \interlinepenalty\@M
   \CTEX@chapter@titleformat{#1}\par\nobreak
   \vskip \CTEX@chapter@afterskip
  }}
\def\@seccntformat#1{%
  \@ifundefined{CTEX@#1name}%
    {\csname the#1\endcsname\quad}%
    {\csname CTEX@#1name\endcsname}}
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
        \interlinepenalty \@M
        \csname CTEX@#1@titleformat\endcsname{#8}\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\@ifundefined{CTEXthe#1}%
                              {\csname the#1\endcsname}%
                              {\csname CTEXthe#1\endcsname}}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
    #6{\hskip #3\relax
      \@svsec \csname CTEX@#1@titleformat\endcsname{#8}}%
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\@ifundefined{CTEXthe#1}%
                              {\csname the#1\endcsname}%
                              {\csname CTEXthe#1\endcsname}}%
      \fi
      #7}}%
  \fi
  \@xsect{#5}}
\def\@ssect#1#2#3#4#5{%
  \@tempskipa #3\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #4{%
        \@hangfrom{\hskip #1}%
          \interlinepenalty \@M
          \CTEX@titleformat@n{#5}\@@par}%
    \endgroup
  \else
    \def\@svsechd{#4{\hskip #1\relax \CTEX@titleformat@n{#5}}}%
  \fi
  \@xsect{#3}}
\cs_new_protected_nopar:Npn \CTEX@set@titleformat@n #1
  { \cs_set_eq:Nc \CTEX@titleformat@n { CTEX@#1@titleformat } }
\cs_new_eq:NN \CTEX@titleformat@n \use:n
\int_zero:N \l__ctex_tmp_int
\clist_map_inline:nn
  { section , subsection , subsubsection , paragraph , subparagraph }
  {
    \int_incr:N \l__ctex_tmp_int
    \cs_gset_protected_nopar:cpx  {#1}
      {
        \exp_not:N \@startsection {#1}
          { \int_use:N \l__ctex_tmp_int }
          { \exp_not:c { CTEX@#1@indent } }
          { \exp_not:c { CTEX@#1@beforeskip } }
          { \exp_not:c { CTEX@#1@afterskip } }
          {
            \CTEX@set@titleformat@n {#1}
            \exp_not:N \normalfont \exp_not:c { CTEX@#1@format }
          }
      }
  }
\tl_new:N \CTEX@preappendix
\tl_new:N \CTEX@postappendix
\keys_define:nn { ctex }
  { appendix .meta:nn = { ctex / appendix } {#1} }
\keys_define:nn { ctex / appendix }
  {
    name      .code:n = { \ctex_assign_heading_name:nn { appendix } {#1} } ,
    number  .tl_set:N = \CTEX@appendixnumber
  }
\cs_new_eq:NN \CTEX@save@appendix \appendix
\cs_gset_protected_nopar:Npn \appendix
  {
    \CTEX@save@appendix
    \gdef \CTEX@prechapter { \CTEX@preappendix }
    \gdef \CTEX@thechapter { \CTEX@appendixnumber }
    \gdef \CTEX@postchapter { \CTEX@postappendix }
  }
\cs_new_protected:Npn \CTEX@toc@width@n #1
  {
    \hbox_set:Nn \l__ctex_tmp_box {#1}
    \dim_set:Nn \@tempdima
      {
        \dim_max:nn { \@tempdima }
          { \box_wd:N \l__ctex_tmp_box + \f@size \p@ / \c_two }
      }
  }
\group_begin:
\char_set_catcode_other:N \#
\use:n
  {
    \group_end:
    \ExplSyntaxOff
    \ctex_preto_cmd:NnTF \numberline { \CTEX@toc@width@n {#1} } { }
      { \ctex_patch_failure:N \numberline }
    \@ifpackageloaded { tocloft }
      { }
      {
        \ctex_at_end_package:nn { tocloft }
          {
            \ctex_preto_cmd:NnTF \numberline { \CTEX@toc@width@n {#1} } { }
              { \ctex_patch_failure:N \numberline }
          }
      }
    \ExplSyntaxOn
  }
\ctex_patch_cmd:Nnn \ps@headings
  { \@chapapp\ \thechapter.~\ } { \CTEXthechapter \quad }
\if@twoside
  \ctex_patch_cmd:Nnn \ps@headings { \thesection.~\ } { \CTEXthesection \quad }
\fi:
\ctex_at_end_package:nn { fancyhdr }
  {
    \ctex_patch_cmd:Nnn \ps@fancy
      { \@chapapp\ \thechapter.~\ } { \if@mainmatter \CTEXthechapter \quad \fi }
    \ctex_patch_cmd:Nnn \ps@fancy
      { \thesection.~\ } { \CTEXthesection \quad }
  }
\cs_new_protected_nopar:Npn \CTEX@setcurrentlabel@n #1
  {
    \protected@edef \@currentlabel
      {
        \cs_if_exist:cTF { CTEX@the#1 }
          { \exp_args:cc { p@#1 } { CTEX@the#1 } }
          { \exp_not:o { \@currentlabel } }
      }
  }
\cs_new_protected_nopar:Npn \ctex_varioref_hook:
  {
    \seq_map_inline:Nn \c__ctex_headings_seq
      { \ctex_fix_varioref_label:n { ##1 } }
  }
\cs_new_protected_nopar:Npn \ctex_fix_varioref_label:n #1
  {
    \tl_if_empty:cT { p@#1 }
      { \exp_args:Nno \labelformat {#1} { \cs:w CTEX@the#1 \cs_end: } }
  }
\@ifpackageloaded { varioref }
  { \ctex_varioref_hook: }
  {
    \cs_new_eq:NN \CTEX@save@refstepcounter \refstepcounter
    \RenewDocumentCommand \refstepcounter { m }
      {
        \CTEX@save@refstepcounter {#1}
        \CTEX@setcurrentlabel@n {#1}
      }
    \ctex_at_end_package:nn { varioref } { \ctex_varioref_hook: }
  }
\ctex_scheme_input:o { \l__ctex_scheme_tl }
%% 
%%
%% End of file `ctex-book.def'.
