% \subsection{bithesis.cls 文档类}
%
% \subsubsection{全局变量与临时变量}
%
% 定义全局变量。
% \begin{variable}{\g_@@_thesis_type_int}
% 论文类型，取值从 1 开始，分别对应:
%  \begin{enumerate}
%      \item 本科生毕业设计（论文）
%      \item 本科生毕业设计（论文）外文翻译
%      \item 本科生全英文专业毕业设计（论文）
%      \item 硕士学位论文
%      \item 博士学位论文
%  \end{enumerate}
%    \begin{macrocode}
\int_new:N \g_@@_thesis_type_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_head_zihao_int}
% 页眉字号。研究生论文使用 5 号字，本科生论文使用 4 号字。
%    \begin{macrocode}
\int_new:N \g_@@_head_zihao_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_twoside_bool}
% 是否双面打印。默认单面打印。
%    \begin{macrocode}
\bool_new:N \g_@@_twoside_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_thesis_type_english_bool}
% 是否为英文模板。目前只有本科生全英文专业的模板会将此变量设置为 true。
%    \begin{macrocode}
\bool_new:N \g_@@_thesis_type_english_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_blind_mode_bool}
% 是否为盲审模式。默认为 false。
%    \begin{macrocode}
\bool_new:N \g_@@_blind_mode_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_quirks_mode_bool}
% 是否兼容更符合北理工官方模板或规范，但不太符合一般排版要求的模式。默认为 false。
% 目前此选项包括：
% \begin{itemize}
%   \item \pkg{biblatex} 的专利格式不再使用国标格式，而采用北理工自定义格式。
% \end{itemize}
%    \begin{macrocode}
\bool_new:N \g_@@_quirks_mode_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_label_divide_char_tl}
% 用于分隔标签的字符。默认为「-」或者「.」。
%    \begin{macrocode}
\tl_new:N \g_@@_label_divide_char_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_right_seq, \l_@@_left_seq}
% 定义临时变量。
%    \begin{macrocode}
\seq_new:N \l_@@_right_seq
\seq_new:N \l_@@_left_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_input_clist}
% 定义渲染信息所用临时列表，每次使用前必须 |\clist_set|。
%    \begin{macrocode}
\clist_new:N \l_@@_input_clist
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{辅助函数与常量}
%
% \begin{macro}[added=2023-05-06,updated=2023-07-04]{\@@_secret_info:nn,\@@_secret_info:N,\@@_secret_info:n}
% 普通模式下显示参数一，盲审模式下显示参数二。
% 无参数二时，替换为等量 |\g_@@_const_substitute_symbol_tl|，换行也会被替换。
% \begin{macrocode}
\cs_new:Npn \@@_hide:n #1 {
  \g_@@_const_substitute_symbol_tl
}

\cs_new:Npn \@@_secret_info:nn #1 #2 {
  \bool_if:NTF \g_@@_blind_mode_bool {
    #2
  } {
    #1
  }
}

\cs_new:Npn \@@_secret_info:N #1 {
  \@@_secret_info:nn {#1} {\tl_map_function:NN #1 \@@_hide:n }
}
\cs_new:Npn \@@_secret_info:n #1 {
  \@@_secret_info:nn {#1} {\tl_map_function:NN {#1} \@@_hide:n }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[added=2025-04-30]{\@@_secret_info_fixed:Nn}
% 普通模式下显示参数一，盲审模式下显示参数二个数的 |\g_@@_const_substitute_symbol_tl|。
% 例外：若参数一为空，也输出空。这样允许 |\@@_render_cover_entry:nn| 隐藏此项。
% 为了实现例外，参数一必须是 N 而非 n，不然难以检测是否真正为空。
%
%    \begin{macrocode}
\cs_new:Npn \@@_secret_info_fixed:Nn #1 #2 {
  \tl_if_blank:VF #1 {
    \@@_secret_info:nn {#1} {
      \prg_replicate:nn {#2} {\g_@@_const_substitute_symbol_tl}
    }
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[added=2023-03-16, updated=2025-04-04]{\@@_get_const:n} 获取标题、章节、表格、图形等的常量名称。
% 会区别英文模式和中文模式。
%   \begin{macrocode}
\cs_new:Npn \@@_get_const:n #1 {
  \@@_if_thesis_english:TF {
    \use:c {c_@@_label_ #1 _en_tl}
  } {
    \use:c {c_@@_label_ #1 _tl}
  }
}
%   \end{macrocode}
% \end{macro}
%
% \begin{macro}[added=2023-03-16]{\@@_set_english_mode:}
% 设置为英文模式。
%   \begin{macrocode}
\cs_new:Npn \@@_set_english_mode: {
  \bool_gset_true:N \g_@@_thesis_type_english_bool
}
%   \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tl_if_empty:xTF,\seq_set_split:Nnx}
% 生成变体。
%    \begin{macrocode}
\cs_generate_variant:Nn \tl_if_empty:nTF {x}
\cs_generate_variant:Nn \seq_set_split:Nnn {Nnx}
%    \end{macrocode}
%
% 为兼容旧版生成变体。
%    \begin{macrocode}
% 兼容 TeX Live 2022/2023 及更早版本（l3msg 与 TeX Live 并不同步更新，2023 支持与否均有可能）
\cs_if_exist:NF \msg_warning:nnV {
    \cs_generate_variant:Nn \msg_warning:nnn { nnV }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_warning_footer:n}
% 用 l3msg 创建警告时，需后缀的内容。
% 参数建议为 ⟨message⟩，即 |\msg_new:nnn| 系列函数的第二个参数。
%
% 之所以设置此函数，是因为 VS Code 插件 LaTeX Workshop 无法识别随意放置的 |\msg_line_context:|，只好专门适配。
% \url{https://github.com/James-Yu/LaTeX-Workshop/blob/c62e327b8233e20d61814bfda305811bea1ae83f/src/parse/parser/latexlog.ts#L17}
%    \begin{macrocode}
\cs_new:Npn \@@_warning_footer:n #1 {
    —The~“#1”\\warning~on~input~line~\msg_line_number:.
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_same_page:}
% 取消换页。
%    \begin{macrocode}
\cs_new:Npn \@@_same_page: {
  \let\clearpage\relax
  \let\cleardoublepage\relax
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@@_if_thesis_int_type:nTF,\@@_if_thesis_int_type:nT}
% 是否某一特定模板。
%    \begin{macrocode}
\cs_new:Npn \@@_if_thesis_int_type:nTF #1#2#3 {\int_compare:nNnTF {\g_@@_thesis_type_int} = {#1} {#2} {#3}}
\cs_new:Npn \@@_if_thesis_int_type:nT #1#2 {\@@_if_thesis_int_type:nTF {#1} {#2} {}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@@_if_thesis_english:T,\@@_if_thesis_english:F,\@@_if_thesis_english:TF}
% 是否为英文模板，这里包括全英文专业和研究生模板的英文模式。
%    \begin{macrocode}
\cs_new:Npn \@@_if_thesis_english:TF #1#2 {\bool_if:nTF {\g_@@_thesis_type_english_bool} {#1} {#2}}
\cs_new:Npn \@@_if_thesis_english:T #1 {\@@_if_thesis_english:TF {#1}{}}
\cs_new:Npn \@@_if_thesis_english:F #1 {\@@_if_thesis_english:TF {}{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_if_graduate:TF,\@@_if_graduate:T,\@@_if_bachelor:T}
% 是否为本科或硕博学位论文。二者互为对立事件。
%    \begin{macrocode}
\cs_new:Npn \@@_if_graduate:TF #1#2 {\int_compare:nNnTF {3} < {\g_@@_thesis_type_int} {#1} {#2}}
\cs_new:Npn \@@_if_graduate:T #1 {\@@_if_graduate:TF {#1} {}}
\cs_new:Npn \@@_if_bachelor:T #1 {\@@_if_graduate:TF {} {#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\c_@@_thesis_type_clist}
% 定义论文类型的列表。
%    \begin{macrocode}
\clist_const:Nn \c_@@_thesis_type_clist
    { bachelor, bachelor_translation, bachelor_english, master, doctor}
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_define_label:nn,\@@_define_label_by_thesis_type:nnn,\@@_define_label:nnn,\@@_define_label_by_thesis_type:nnnn}
% 定义常量（标签）的辅助函数。
%    \begin{macrocode}
\cs_new_protected:Npn \@@_define_label:nn #1#2
  { \tl_const:cn { c_@@_label_ #1 _tl } {#2} }

\cs_new_protected:Npn \@@_define_label_by_thesis_type:nnn #1#2#3
  {
    \tl_const:cn { c_@@_ #1 _label_ #2 _tl } {#3}
  }

\cs_new_protected:Npn \@@_define_label:nnn #1#2#3
  {
    \tl_const:cn { c_@@_label_ #1    _tl } {#2}
    \tl_const:cn { c_@@_label_ #1 _en_tl } {#3}
  }

\cs_new_protected:Npn \@@_define_label_by_thesis_type:nnnn #1#2#3#4
  {
    \tl_const:cn { c_@@_ #1 _label_ #2 _tl } {#3}
    \tl_const:cn { c_@@_ #1 _label_ #2 _en_tl } {#4}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_smallgap:}
% 标签文字之间的间距。
%    \begin{macrocode}
\cs_new:Npn \@@_smallgap: {
  \hspace{0.45ex}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_boxempty:}
% 空的选框。
%    \begin{macrocode}
\cs_new:Npn \@@_boxempty:
{
  \makebox[1em][l]
  {
    % 为保证与打了勾的一致，也需套盒子
    \makebox[0pt][l]
    {
      % 默认比基线略高，向下降降
      \raisebox{-1pt}{$\square$}
    }
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_boxcheck:}
% 打了勾的选框。
%    \begin{macrocode}
\cs_new:Npn \@@_boxcheck:
{
  \makebox[1em][l]
  {
    \makebox[0pt][l]
    {
      % 默认比基线略高，向下降降
      \raisebox{-1pt}{$\square$}
    }
    $\checkmark$
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_label_space:}
% 标签与内容之间的空白间距。
%    \begin{macrocode}
\cs_new:Npn \@@_label_space: {
  \@@_if_bachelor:T {
    \quad
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\c_@@_label_code_tl,\c_@@_label_udc_tl,
%   \c_@@_label_classification_tl,\c_@@_label_classified_level_tl,\c_@@_label_type_tl}
% 没有对应英文的常量。
%    \begin{macrocode}
\clist_map_inline:nn
  {
    {code} {代码},
    {udc} {UDC分类号：},
    {classification} {中图分类号：},
    {university_code} {学校代码：},
    {classified_level} {密级},
    {type} {种类},
    {special_type} {学生类型},
    {engineering_special_plan} {工程硕博士专项},
    {cross_research} {交叉研究方向},
    {international_student_ugp} {政府项目留学生},
  }
  {\@@_define_label:nn #1}
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_bachelor_label_xxx_tl}
% 本科毕设的常量。
%    \begin{macrocode}
\clist_map_inline:nn
  {
    {originality} {原创性声明},
    {originality_clause} {本人郑重声明：所呈交的毕业设计（论文），
    是本人在指导老师的指导下独立进行研究所取得的成果。除文中已经注明引用的内容外，
    本文不包含任何其他个人或集体已经发表或撰写过的研究成果。
    对本文的研究做出重要贡献的个人和集体，均已在文中以明确方式标明。\par~特此申明。},
    {authorization} {关于使用授权的声明},
    {authorization_clause} {本人完全了解北京理工大学有关保管、使用毕业设计（论文）的规定，
    其中包括：\circled{1}~学校有权保管、并向有关部门送交本毕业设计（论文）的原件与复印件；
    \circled{2}~学校可以采用影印、缩印或其它复制手段复制并保存本毕业设计（论文）；
    \circled{3}~学校可允许本毕业设计（论文）被查阅或借阅；\circled{4}~学校可以学术交流为目的，
    复制赠送和交换本毕业设计（论文）；\circled{5}~学校可以公布本毕业设计（论文）的全部或部分内容。},
    {originality_author_signature}
      {本人签名：\hspace{40mm}日\hspace{2.5mm}期：\hspace{13mm}年\hspace{8mm}月\hspace{8mm}日},
    {originality_supervisor_signature}
      {指导老师签名：\hspace{40mm}日\hspace{2.5mm}期：\hspace{13mm}年\hspace{8mm}月\hspace{8mm}日},
  } {\@@_define_label_by_thesis_type:nnn {bachelor} #1}
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_bachelor_english_label_xxx_tl}
% 全英文专业的常量。
%    \begin{macrocode}
\clist_map_inline:nn
  {
    {originality} {原创性声明~Statement~of~Originality},
    {originality_clause} {
        本人郑重声明：所呈交的毕业设计（论文），
        是本人在指导老师的指导下独立进行研究所取得的成果。除文中已经注明引用的内容外，
        本文不包含任何其他个人或集体已经发表或撰写过的研究成果。
        对本文的研究做出重要贡献的个人和集体，均已在文中以明确方式标明。特此申明。\par
        \arialfamily I,\@@_dunderline:n{\makebox[18mm]{}},~solemnly~
        declare:~the~submitted~graduation~design~(thesis),~
        is~the~research~achievement~completed~independently~by~myself~
        under~the~guidance~of~the~supervisor.~This~article~does~not~contain~
        any~research~published~or~written~by~any~other~individual~or~group,~
        except~as~already~referenced~in~this~paper.~Individuals~and~groups~
        that~have~made~important~contributions~to~the~study~of~this~paper~
        are~clearly~indicated~and~cited~in~the~paper.\par
    },
    {authorization} {关于使用授权的声明~State~of~Use~Authorization},
    {authorization_clause} {
      本人完全了解北京理工大学有关保管、使用毕业设计（论文）的规定，
      其中包括：\circled{1}学校有权保管、并向有关部门送交本毕业设计（论文）的原件与复印件；
      \circled{2}学校可以采用影印、缩印或其它复制手段复制并保存本毕业设计（论文）；
      \circled{3}学校可允许本毕业设计（论文）被查阅或借阅；
      \circled{4}学校可以学术交流为目的，复制赠送和交换本毕业设计（论文）；
      \circled{5}学校可以公布本毕业设计（论文）的全部或部分内容。\par
      I~fully~understand~the~regulations~on~the~storage,~
      use~of~graduation~design~(thesis)~in~Beijing~Institute~of~Technology.~
      Beijing~Institute~of~Technology~has~the~right~to~(1)~keep,~
      and~to~the~relevant~departments~to~send~the~original~or~copy~
      of~this~graduation~design~(thesis);~(2)~copy~and~preserve~this~
      graduation~design~(thesis)~by~photocopying,~miniature~or~other~
      means~of~reproduction;~(3)~allow~this~graduation~design~(thesis)~
      to~be~read~or~borrowed;~(4)~for~the~purpose~of~academic~exchange,~
      copy,~give~and~exchange~this~graduation~design~(thesis);~(5)~
      publish~all~or~part~of~the~contents~of~this~graduation~design~(thesis).~
    },
  } {\@@_define_label_by_thesis_type:nnn {bachelor_english} #1}
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_graduate_label_xxx_tl}
% 研究生模板的常量。
%    \begin{macrocode}
\clist_map_inline:nn
  {
    {originality} {研究成果声明},
    {originality_clause} {本人郑重声明：所提交的学位论文是我本人在指导教师的指导下独立完成的研究成果。文中所撰写内容符合以下学术规范（请勾选）：
\par \@@_boxcheck:\hspace{0.5em} 论文综述遵循“适当引用”的规范，全部引用的内容不超过50\%。
\par \@@_boxcheck:\hspace{0.5em} 论文中的研究数据及结果不存在篡改、剽窃、抄袭、伪造等学术不端行为，并愿意承担因学术不端行为所带来的一切后果和法律责任。
\par \@@_boxcheck:\hspace{0.5em} 文中依法引用他人的成果，均已做出明确标注或得到许可。
\par \@@_boxcheck:\hspace{0.5em} 论文内容未包含法律意义上已属于他人的任何形式的研究成果，也不包含本人已用于其他学位申请的论文或成果。
\par \@@_boxcheck:\hspace{0.5em} 与本人一同工作的合作者对此研究工作所做的任何贡献均已在学位论文中作了明确的说明并表示了谢意。
\par~特此声明。},
    {authorization} {关于学位论文使用权的说明},
    {authorization_clause} {本人完全了解北京理工大学有关保管、使用学位论文的规定，其中包括：
\par~\circled{1}~学校有权保管、并向有关部门送交学位论文的原件与复印件；
\par~\circled{2}~学校可以采用影印、缩印或其它复制手段复制并保存学位论文；
\par~\circled{3}~学校可允许学位论文被查阅或借阅；
\par~\circled{4}~学校可以学术交流为目的，复制赠送和交换学位论文；
\par~\circled{5}~学校可以公布学位论文的全部或部分内容（保密学位论文在解密后遵守此规定）。},
    {originality_author_signature}
      {签\qquad 名：\hspace{40mm}日\hspace{2.5mm}期：\hspace{30mm}\quad},
    {originality_supervisor_signature}
      {导师签名：\hspace{40mm}日\hspace{2.5mm}期：\hspace{30mm}\quad},
  } {\@@_define_label_by_thesis_type:nnn {graduate} #1}
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_graduate_label_xxx_tl,\c_@@_graduate_label_xxx_en_tl}
% 研究生模板的中英常量。
%    \begin{macrocode}
\clist_map_inline:nn
  {
    % TODO: 自动实现汉字均排
    {author} {作\quad 者\quad 姓\quad 名} {Candidate~Name},
    {school} {二\@@_smallgap: 级\@@_smallgap: 培\@@_smallgap: 养\@@_smallgap: 单\@@_smallgap: 位} {School~or~Department},
    {supervisor} {指\quad 导\quad 教\quad 师} {Faculty~Mentor},
    {industrial_mentor} {行\@@_smallgap: 业\@@_smallgap: 合\@@_smallgap: 作\@@_smallgap: 导\@@_smallgap: 师} {Industry~Collaboration~Mentor},
    {chairman} {答辩委员会主席} {Chair,~Thesis~Committee},
    % degree、major 的中文与 degreeType 有关，此处以 academic 为准，其余情况通过 \c_@@_auto_tl 处理
    % 英文倒是无关。
    {degree} {申\quad 请\quad 学\quad 位} {Degree~Applied},
    {major} {一\quad 级\quad 学\quad 科} {Major},
    {institute}
      {学\@@_smallgap: 位\@@_smallgap: 授\@@_smallgap: 予\@@_smallgap: 单\@@_smallgap: 位}
      {Degree~by},
    {submission_date}
      {论\@@_smallgap: 文\@@_smallgap: 提\@@_smallgap: 交\@@_smallgap: 日\@@_smallgap: 期}
      {Submission~date~of~the~paper},
  } {\@@_define_label_by_thesis_type:nnnn {graduate} #1}
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_label_xxx_tl,\c_@@_label_xxx_en_tl}
% 常用的中英常量。
%    \begin{macrocode}
\clist_map_inline:nn
  {
    {school} {学\qquad 院} {School},
    {major} {专\qquad 业} {Degree},
    {course} {课程名称} {Course},
    {class} {班\qquad{}级} {Class},
    {author} {学生姓名} {Author},
    {student_id} {学\qquad 号} {Student~ID},
    {supervisor} {指导教师} {Supervisor},
    {co_supervisor} {校外指导教师} {Co-Supervisor},
    {teacher} {任课教师} {Teacher},
    {semester} {上课学期} {Semester},
    {keywords} {关键词：} {Key~Words:~},
    {abstract} {摘\@@_label_space: 要} {Abstract},
    {conclusion} {结\@@_label_space: 论} {Conclusions},
    % 附录部分的总标题
    {appendix} {附\@@_label_space: 录} {Appendices},
    {ack} {致\@@_label_space: 谢} {Acknowledgements},
    {figure} {插\@@_label_space: 图} {Illustrations},
    {table} {表\@@_label_space: 格} {Tables},
    % 附录下各部分编号的前缀
    {appendix_prefix} {附录} {Appendix},
    {reference} {参考文献} {References},
    {university} {北京理工大学} {Beijing~Institute~of~Technology},
    {publications} {攻读学位期间发表论文与研究成果清单}
      {Publications~During~Studies},
    % TODO: Not so sure about the translation.
    {resume} {作者简介} {Author~Biography},
    {symbols} {主要符号对照表} {Nomenclature},
    {algo} {算法} {Algorithm},
    {them} {定理} {Theorem},
    {lem} {引理} {Lemma},
    {prop} {命题} {Proposition},
    {cor} {推论} {Corollary},
    {axi} {公理} {Axiom},
    {defn} {定义} {Definition},
    {conj} {猜想} {Conjecture},
    {exmp} {例} {Example},
    {case} {情形} {Case},
    {rem} {注} {Remark},
    {fig} {图} {Figure},
    {tab} {表} {Table},
    {equ} {式} {Equation},
  }
  {\@@_define_label:nnn #1}
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_bachelor_thesis_header_clist,\c_@@_bachelor_thesis_headline_clist}
% 本科生模板的页眉标题与封面大标题常量列表，其中封面大标题不适用于硕士、博士学位论文，只适用于本科生毕业设计（论文）及其衍生物。
%    \begin{macrocode}
\clist_const:Nn \c_@@_bachelor_thesis_header_clist
  {
    北京理工大学本科生毕业设计（论文）,
    北京理工大学本科生毕业设计（论文）外文翻译,
    Beijing~Institute~of~Technology~Bachelor's~Thesis,
    北京理工大学硕士学位论文,
    北京理工大学博士学位论文,
  }
\clist_const:Nn \c_@@_bachelor_thesis_headline_clist
  {
    本科生毕业设计（论文）,
    本科生毕业设计（论文）外文翻译,
    Beijing\nobreak{~}Institute\nobreak{~}of\nobreak{~}Technology~Bachelor's~Thesis,
  }
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{BITSetup l3keys 接口键值对定义}
%
% \begin{macro}{\c_@@_auto_tl}
% 定义 |\c_@@_auto_tl|
% 选项设为它时，表示智能默认值，稍后由 |\@@_resolve_auto_values:| 根据其它信息自动填充。
% 参考 https://typst.app/docs/reference/foundations/auto/ 的设计。
%    \begin{macrocode}
\tl_const:Nn \c_@@_auto_tl { BIThesis-auto-magic }
%    \end{macrocode}
% \end{macro}
%
% 定义 |bithesis| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis }
{
  info .meta:nn = { bithesis / info } {#1},
  misc .meta:nn = { bithesis / misc } {#1},
  cover .meta:nn = { bithesis / cover } {#1},
  style .meta:nn = { bithesis / style } {#1},
  option .meta:nn = { bithesis / option } {#1},
  TOC .meta:nn = { bithesis / TOC } {#1},
  appendices .meta:nn = { bithesis / appendices } {#1},
  publications .meta:nn = { bithesis / publications } {#1},
  acknowledgements .meta:nn = { bithesis / acknowledgements } {#1},
  const .meta:nn = { bithesis / const } {#1},
}
%    \end{macrocode}
%
% 定义 |bithesis/option| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis / option }
{
  type .choice:,
  type .value_required:n = true,
  type .choices:Vn =
    \c_@@_thesis_type_clist
    {
      \int_set_eq:NN \g_@@_thesis_type_int \l_keys_choice_int
      \int_case:nn {\l_keys_choice_int} {
        % 本科全英文也是英文模板。
        {3} {\@@_set_english_mode:}
      }
    },
  type .initial:n = bachelor,
  twoside .bool_gset:N = \g_@@_twoside_bool,
  blindPeerReview .bool_gset:N = \g_@@_blind_mode_bool,
  ctex .tl_set:N = \l_@@_options_to_ctex_tl,
  quirks .bool_gset:N = \g_@@_quirks_mode_bool,
  % xeCJK
  autoFakeBold .tl_set:N = \g_@@_auto_fake_bold_tl,
  autoFakeBold .initial:n = {3},
  % 是否开启英文模式。目前在设计上，这个选项仅对研究生模板生效。
  % 本科生模板的英文模式是根据 |type| 选项自动判断的。
  english .code:n = { \@@_set_english_mode: } ,
}
%    \end{macrocode}
%
% 定义 |bithesis/cover| 键值对类。
%    \begin{macrocode}
% Declare variables used by keys
\tl_new:N \l_@@_cover_delimiter_tl
\tl_new:N \l_@@_cover_label_align_tl
\dim_new:N \l_@@_cover_label_max_width_dim
\dim_new:N \l_@@_cover_value_max_width_dim
\dim_new:N \l_@@_cover_auto_width_padding_dim
\dim_new:N \l_@@_cover_underline_offset_dim
%
% Declare keys
\keys_define:nn { bithesis / cover }
  {
    date .tl_set:N = \l_@@_cover_date_tl,
    headerImage .tl_set:N = \l_bit_coverheaderimage_tl,
    xiheiFont .tl_set:N = \l_@@_cover_xihei_font_path_tl,
    xiheiFont .default:n = {STXihei},
    %% cover entry
    delimiter .tl_set:N = \l_@@_cover_delimiter_tl,
    labelAlign .tl_set:N = \l_@@_cover_label_align_tl,
    labelAlign .initial:n = {r},
    valueAlign .tl_set:N = \l_@@_cover_value_align_tl,
    valueAlign .initial:n = {c},
    labelMaxWidth .dim_set:N = \l_@@_cover_label_max_width_dim,
    valueMaxWidth .dim_set:N = \l_@@_cover_value_max_width_dim,
    autoWidthPadding .dim_set:N = \l_@@_cover_auto_width_padding_dim,
    autoWidthPadding .initial:n = {0.25em},
    autoWidth .bool_set:N = \l_@@_cover_auto_width_bool,
    autoWidth .initial:n = {true},
    underlineThickness .dim_set:N = \l_@@_cover_underline_thickness_dim,
    underlineThickness .initial:n = {1pt},
    underlineOffset .dim_set:N = \l_@@_cover_underline_offset_dim,
    underlineOffset .initial:n = { -10pt },
    hideCoverInPeerReview .bool_set:N = \l_@@_cover_hide_cover_in_peer_review_bool,
    % 此处暂且填充默认值为`false`，待确定`\g_@@_thesis_type_int`后再根据论文类型覆盖默认值
    hideCoverInPeerReview .initial:n = {false},
    % 研究生的「特殊类型」
    showSpecialTypeBox .bool_set:N = \l_@@_cover_show_special_type_box_bool,
    showSpecialTypeBox .initial:n = {true},
    % 本科英文模板也可以使用中文封面
    prefer-zh .bool_set:N = \l_@@_cover_prefer_zh_bool,
    prefer-zh .initial:n = {false},
    % 本科英文模板使用中文封面时可能需要调换中英文标题顺序
    reverse-titles .bool_set:N = \l_@@_cover_reverse_titles_bool,
    reverse-titles .initial:n = {false},
    % 本科英文模板可加入中文标题
    addTitleZh .bool_set:N = \l_@@_cover_add_titlezh_bool,
    addTitleZh .initial:n = {true},
  }
%    \end{macrocode}
%
% 定义 |bithesis/info| 键值对类。
%    \begin{macrocode}
% Declare variables used by keys
\tl_new:N \l_@@_value_vertical_title_tl
\tl_new:N \l_@@_value_classified_level_tl
%
% Declare keys
\keys_define:nn { bithesis / info }
  {
    title .tl_set:N = \l_@@_value_title_tl,
    title .initial:n = {形状记忆聚氨酯的合成及其在织物中的应用（示例）},
    titleEn .tl_set:N = \l_@@_value_title_en_tl,
    titleEn .initial:n = {Synthesis~and~Application~on~Texttiles~of~the~Shape~Memory~Polyurethane~(example)},
    % 因为是覆盖选项，所以不需要设置初始值。
    verticalTitle .tl_set:N = \l_@@_value_vertical_title_tl,
    school .tl_set:N = \l_@@_value_school_tl,
    major .tl_set:N = \l_@@_value_major_tl,
    class .tl_set:N = \l_@@_value_class_tl,
    % 课程名称，读书报告使用
    course .tl_set:N = \l_@@_value_course_tl,
    author .tl_set:N = \l_@@_value_author_tl,
    studentId .tl_set:N = \l_@@_value_student_id_tl,
    % 指导教师
    supervisor .tl_set:N = \l_@@_value_supervisor_tl,
    % 因为默认不显示，所以不需要设置初始值。
    externalSupervisor .tl_set:N = \l_@@_value_external_supervisor_tl,
    % 任课教师，读书报告使用
    teacher .tl_set:N = \l_@@_value_teacher_tl,
    % 上课学期，读书报告使用
    semester .tl_set:N = \l_@@_value_semester_tl,
    keywords .tl_set:N = \l_@@_value_keywords_tl,
    keywords .initial:n = {聚氨酯；形状记忆；织物（示例）},
    keywordsEn .tl_set:N = \l_@@_value_keywords_en_tl,
    keywordsEn .initial:n = {Polyurethane,Shape Memory,Textiles (example)},
    translationTitle .tl_set:N = \l_@@_value_trans_title_tl,
    translationOriginTitle .tl_set:N = \l_@@_value_trans_origin_title_tl,
    % 中图分类号，研究生学位论文使用
    classification .tl_set:N = \l_@@_value_classification_tl,
    classification .initial:n = {TQ~028.1（示例）},
    % UDC 分类号，研究生学位论文使用
    UDC .tl_set:N = \l_@@_value_udc_tl,
    UDC .initial:n = {540（示例）},
    % 学校代码，研究生学位论文使用
    universityCode .tl_set:N = \l_@@_value_university_code_tl,
    universityCode .initial:n = {10007},
    % 学术型/专业型，研究生学位论文使用
    degreeType .choice:,
    degreeType .choices:nn = {professional, academic} {
      \tl_set:Nn \l_@@_value_degree_type_tl {#1}
    },
    degreeType .initial:n = academic,
    chairman .tl_set:N = \l_@@_value_chairman_tl,
    industrialMentor .tl_set:N = \l_@@_value_industrial_mentor_tl,
    industrialMentorEn .tl_set:N = \l_@@_value_industrial_mentor_en_tl,
    degree .tl_set:N = \l_@@_value_degree_tl,
    degreeEn .tl_set:N = \l_@@_value_degree_en_tl,
    institute .tl_set:N = \l_@@_value_institute_tl,
    institute .initial:n = {\tl_use:N \c_@@_label_university_tl},
    submissionDate .tl_set:N = \l_@@_value_submission_date_tl,
    authorEn .tl_set:N = \l_@@_value_author_en_tl,
    schoolEn .tl_set:N = \l_@@_value_school_en_tl,
    supervisorEn .tl_set:N = \l_@@_value_supervisor_en_tl,
    chairmanEn .tl_set:N = \l_@@_value_chairman_en_tl,
    majorEn .tl_set:N = \l_@@_value_major_en_tl,
    instituteEn .tl_set:N = \l_@@_value_institute_en_tl,
    instituteEn .initial:n = {\c_@@_label_university_en_tl},
    submissionDateEn .tl_set:N = \l_@@_value_submission_date_en_tl,
    % 因为默认不显示，所以不需要设置初始值。
    classifiedLevel .tl_set:N = \l_@@_value_classified_level_tl,
    % 学生类型-工程硕博士专项
    工程硕博士专项 .bool_set:N = \l_@@_value_engineering_special_plan_bool,
    工程硕博士专项 .initial:n = {false},
    % 学生类型-交叉研究方向
    crossResearch .bool_set:N = \l_@@_value_cross_research_bool,
    crossResearch .initial:n = {false},
    % 学生类型-政府项目留学生
    internationalStudentUGP .bool_set:N = \l_@@_value_international_student_ugp_bool,
    internationalStudentUGP .initial:n = {false},
  }
%    \end{macrocode}
%
% 定义 |bithesis/misc| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis / misc }
  {
    % 表格字体大小，默认为 5 号字体。
    tabularFontSize .tl_set:N = \l_@@_misc_tabular_font_size_tl,
    tabularFontSize .initial:n = {5},
    arialFont .tl_set:N = \l_@@_misc_arial_font_path_tl,
    autoref / algo .tl_set:N = \l_@@_misc_autoref_algo_tl,
    autoref / algo .initial:n = {\g_@@_const_autoref_algo_tl},
    autoref / them .tl_set:N = \themautorefname,
    autoref / them .initial:n = {\g_@@_const_autoref_them_tl},
    autoref / lem .tl_set:N = \lemautorefname,
    autoref / lem .initial:n = {\g_@@_const_autoref_lem_tl},
    autoref / prop .tl_set:N = \propautorefname,
    autoref / prop .initial:n = {\g_@@_const_autoref_prop_tl},
    autoref / cor .tl_set:N = \corautorefname,
    autoref / cor .initial:n = {\g_@@_const_autoref_cor_tl},
    autoref / axi .tl_set:N = \axiautorefname,
    autoref / axi .initial:n = {\g_@@_const_autoref_axi_tl},
    autoref / defn .tl_set:N = \defnautorefname,
    autoref / defn .initial:n = {\g_@@_const_autoref_defn_tl},
    autoref / conj .tl_set:N = \conjautorefname,
    autoref / conj .initial:n = {\g_@@_const_autoref_conj_tl},
    autoref / exmp .tl_set:N = \exmpautorefname,
    autoref / exmp .initial:n = {\g_@@_const_autoref_exmp_tl},
    autoref / case .tl_set:N = \caseautorefname,
    autoref / case .initial:n = {\g_@@_const_autoref_case_tl},
    autoref / rem .tl_set:N = \remautorefname,
    autoref / rem .initial:n = {\g_@@_const_autoref_rem_tl},
    hideLinks .bool_set:N = \l_@@_misc_hide_links_bool,
    hideLinks .initial:n = {true},
    autoref / figure .tl_set:N = \figureautorefname,
    autoref / figure .initial:n = {\g_@@_const_autoref_fig_tl},
    autoref / table .tl_set:N = \tableautorefname,
    autoref / table .initial:n = {\g_@@_const_autoref_tab_tl},
    autoref / equ .tl_set:N = \equationautorefname,
    autoref / equ .initial:n = {\g_@@_const_autoref_equ_tl},
    % 浮动体相关的各种间距
    floatSeparation .tl_set:N = \l_@@_misc_float_separation_tl,
    floatSeparation .initial:n = {0},
    algorithmSeparation .tl_set:N = \l_@@_misc_algorithm_separation_tl,
    algorithmSeparation .initial:n = {12pt plus 4pt minus 4pt},
    tabularRowSeparation .tl_set:N = \l_@@_misc_tabular_row_separation_tl,
    tabularRowSeparation .initial:n = {1},
  }
%    \end{macrocode}
%
% 定义 |bithesis/const| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis / const }
  {
    autoref .meta:nn = { bithesis / const / autoref } { #1 },
    style .meta:nn = { bithesis / const / style } { #1 },
    info .meta:nn = { bithesis / const / info } { #1 },
    heading .meta:nn = { bithesis / const / heading } { #1 },
  }
\keys_define:nn { bithesis / const / autoref }
  {
    algo .tl_set:N = \g_@@_const_autoref_algo_tl,
    algo .initial:n = {\@@_get_const:n {algo}},
    them .tl_set:N = \g_@@_const_autoref_them_tl,
    them .initial:n = {\@@_get_const:n {them}},
    lem .tl_set:N = \g_@@_const_autoref_lem_tl,
    lem .initial:n = {\@@_get_const:n {lem}},
    prop .tl_set:N = \g_@@_const_autoref_prop_tl,
    prop .initial:n = {\@@_get_const:n {prop}},
    cor .tl_set:N = \g_@@_const_autoref_cor_tl,
    cor .initial:n = {\@@_get_const:n {cor}},
    axi .tl_set:N = \g_@@_const_autoref_axi_tl,
    axi .initial:n = {\@@_get_const:n {axi}},
    defn .tl_set:N = \g_@@_const_autoref_defn_tl,
    defn .initial:n = {\@@_get_const:n {defn}},
    conj .tl_set:N = \g_@@_const_autoref_conj_tl,
    conj .initial:n = {\@@_get_const:n {conj}},
    exmp .tl_set:N = \g_@@_const_autoref_exmp_tl,
    exmp .initial:n = {\@@_get_const:n {exmp}},
    case .tl_set:N = \g_@@_const_autoref_case_tl,
    case .initial:n = {\@@_get_const:n {case}},
    rem .tl_set:N = \g_@@_const_autoref_rem_tl,
    rem .initial:n = {\@@_get_const:n {rem}},
    figure .tl_set:N = \g_@@_const_autoref_fig_tl,
    figure .initial:n =  {\@@_get_const:n {fig}},
    table .tl_set:N = \g_@@_const_autoref_tab_tl,
    table .initial:n =  {\@@_get_const:n {tab}},
    equ .tl_set:N = \g_@@_const_autoref_equ_tl,
    equ .initial:n = {\@@_get_const:n {equ}},
  }
\keys_define:nn { bithesis / const / style }
  {
    substituteSymbol .tl_set:N = \g_@@_const_substitute_symbol_tl,
    substituteSymbol .initial:n = {*},
  }
\keys_define:nn { bithesis / const / info }
  {
    degree .tl_set:N = \g_@@_const_info_degree_tl,
    degree .initial:n = {\c_@@_auto_tl},
    major .tl_set:N = \g_@@_const_info_major_tl,
    major .initial:n = {\c_@@_auto_tl},
  }
\keys_define:nn { bithesis / const / heading }
  {
    acknowledgements .tl_set:N = \g_@@_const_heading_acknowledgements_tl,
    acknowledgements .initial:n = {
      \@@_get_const:n {ack}
    },
  }
%    \end{macrocode}
%
% 定义 |bithesis/style| 键值对类。
%    \begin{macrocode}
% Declare variables used by keys
\tl_new:N \l_@@_style_head_tl
%
% Declare keys
\keys_define:nn { bithesis / style }
{
  head .tl_set:N = \l_@@_style_head_tl,
  head .initial:n = {
    \clist_item:Nn \c_@@_bachelor_thesis_header_clist \g_@@_thesis_type_int
  },
  headline .tl_set:N = \l_@@_style_headline_tl,
  headline .initial:n = {
    \clist_item:Nn \c_@@_bachelor_thesis_headline_clist \g_@@_thesis_type_int
  },
  bibliographyIndent .bool_set:N = \l_@@_style_bibliography_indent_bool,
  bibliographyIndent .initial:n = {true},
  pageVerticalAlign .choices:nn = {top, scattered} {
    \tl_if_eq:NnTF \l_keys_choice_tl {top}
      { \raggedbottom }
      { \flushbottom }
  },
  pageVerticalAlign .initial:n = {top},
  non-CJK-font-in-headings .choice:,
  non-CJK-font-in-headings / serif .code:n = { \bool_set_false:N \l_@@_arial_as_title_font_bool },
  non-CJK-font-in-headings / sans .code:n = { \bool_set_true:N \l_@@_arial_as_title_font_bool },
  non-CJK-font-in-headings .initial:n = {serif},
  % 数学字体配置
  mathFont .choices:nn = {
    asana, bonum, cm, concrete, dejavu, erewhon, euler,
    fira, garamond, gfsneohellenic, kp, libertinus, lm, newcm,
    pagella, schola, stix, stix2, termes, xcharter, xits, none,
    } { \tl_set_eq:NN \l_@@_style_math_font_tl \l_keys_choice_tl },
  mathFont .initial:n = {cm},
  % Options that will be pass to `unicode-math` pkgs.
  unicodeMathOptions .tl_set:N = \l_@@_unicode_math_options_tl,
  % Windows 平台开启宋体伪粗体。
  windowsSimSunFakeBold .bool_set:N = \l_@@_style_windows_simsum_fake_bold,
  windowsSimSunFakeBold .initial:n = {false},
  % 控制英文是否使用 hyphen 进行换行
  hyphen .bool_set:N = \l_@@_style_hyphen_bool,
  hyphen .initial:n = {true},
  % 控制公式和上下文的距离
  mathAboveDisplaySkip .dim_set:N = \l_@@_style_math_above_display_skip_dim,
  mathAboveDisplaySkip .initial:n = {10pt},
  mathBelowDisplaySkip .dim_set:N = \l_@@_style_math_below_display_skip_dim,
  mathBelowDisplaySkip .initial:n = {10pt},
  betterTimesNewRoman .bool_set:N = \l_@@_style_better_new_roman_bool,
  betterTimesNewRoman .initial:n = {false},
}
%    \end{macrocode}
%
% 定义 |bithesis/TOC| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis / TOC }
{
  title .tl_set:N = \l_@@_toc_title_tl,
  title .initial:n = {
    \@@_if_thesis_english:TF {
      Table~of~Contents
    } {
      \@@_if_graduate:TF {目次} {目\@@_label_space: 录}
    }    
  },
  abstract .bool_set:N = \l_@@_add_abstract_to_toc_bool,
  abstract .initial:n = {true},
  abstractEn .bool_set:N = \l_@@_add_abstract_en_to_toc_bool,
  abstractEn .initial:n = {true},
  TOC .bool_set:N = \l_@@_add_toc_to_toc_bool,
  TOC .initial:n = {false},
  symbols .bool_set:N = \l_@@_add_symbols_to_toc_bool,
  symbols .initial:n = {true},
}
%    \end{macrocode}
%
% 定义 |bithesis/appendices| 键值对类。
%    \begin{macrocode}
% Declare variables used by keys
\tl_new:N \l_@@_appendices_title_tl
\tl_new:N \l_@@_appendix_toc_title_tl
%
% Declare keys
\keys_define:nn { bithesis / appendices }
{
  chapterLevel .bool_set:N = \l_@@_appendices_chapter_level_bool,
  title .tl_set:N = \l_@@_appendices_title_tl,
  TOCTitle .tl_set:N = \l_@@_appendix_toc_title_tl,
}
%    \end{macrocode}
%
% 定义 |bithesis/publications| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis / publications }
{
  sorting .bool_set:N = \l_@@_publications_sorting_bool,
  sorting .initial:n = {true},
  omit .bool_set:N = \l_@@_publications_omit_bool,
  omit .initial:n = {false},
  maxbibnames .int_set:N = \l_@@_publications_maxbibnames_int,
  maxbibnames .initial:n = {10},
  minbibnames .int_set:N = \l_@@_publications_minbibnames_int,
  minbibnames .initial:n = {10},
}
%    \end{macrocode}
%
% 定义 |bithesis/acknowledgements| 键值对类。
%    \begin{macrocode}
\keys_define:nn { bithesis / acknowledgements }
{
  showTitleInPeerReview .bool_set:N = \l_@@_acknowledgements_show_title_in_peer_review_bool,
  % 此处暂且填充默认值为`false`，待确定`\g_@@_thesis_type_int`后再根据论文类型覆盖默认值
  showTitleInPeerReview .initial:n = {false},
}
%    \end{macrocode}
%
% \subsubsection{内部 l3keys 接口键值对定义}
%
% 除了 |\BITSetup|，有些用户接口也是键值对，在此通过 |bithesis-internal| 实现。
% 约定它只用于内部解析用户命令传入的参数，不直接面向最终作者，而且每个用户命令都不记忆之前设置的值。
% 此处只定义数据结构，默认值等逻辑由各命令自行处理。
%
% 定义 |bithesis-internal / pre-frontmatter|，用于设置 |\frontmatter| 之前的 |\MakeCover|、|\MakeTitle| 等。
% 目前这些部分的接口相同，故共用 |pre-frontmatter|。
%    \begin{macrocode}
\keys_define:nn { bithesis-internal / pre-frontmatter }
{
  % 是否将标题加入PDF书签
  bookmarked .bool_set:N = \l_@@_internal__pre_frontmatter__bookmarked_bool,
}
%    \end{macrocode}
%
% 定义 |bithesis-internal / conclusion|，用于设置 |conclusion| 环境。
%    \begin{macrocode}
\keys_define:nn { bithesis-internal / conclusion }
{
  title .tl_set:N = \l_@@_internal__conclusion__title_tl,
}
%    \end{macrocode}
%
%
% \subsubsection{BITSetup l3keys 接口键值对预处理}
%
% 在宏加载时，处理 |bithesis/option| 中的值。使得 |bithesis|
% 宏包的模板选项可以在宏加载时生效。
%    \begin{macrocode}
\ProcessKeysOptions { bithesis / option }
%    \end{macrocode}
%
% 确定 |bithesis/option| 中的 |\g_@@_thesis_type_int| 后，根据论文类型自动覆盖某些选项的默认值。
%    \begin{macrocode}
\@@_if_graduate:TF {
  \keys_set:nn {bithesis} {
    cover / hideCoverInPeerReview = false,
    acknowledgements / showTitleInPeerReview = true,
  }
} {
  \keys_set:nn {bithesis} {
    cover / hideCoverInPeerReview = true,
    acknowledgements / showTitleInPeerReview = false,
  }
}
%    \end{macrocode}
%
% \begin{macro}{\@@_resolve_auto_values:}
% 计算 auto 并替换为标准的值
%    \begin{macrocode}
\cs_new:Npn \@@_resolve_auto_values: {
  \tl_if_eq:NnT \g_@@_const_info_degree_tl {\c_@@_auto_tl} {
    \tl_if_eq:NnTF \l_@@_value_degree_type_tl {academic} {
      \keys_set:nn {bithesis} {const/info/degree = \c_@@_graduate_label_degree_tl}
    } {
      \keys_set:nn {bithesis} {const/info/degree = 申\quad 请\quad 类\quad 别}
    }
  }
  \tl_if_eq:NnT \g_@@_const_info_major_tl {\c_@@_auto_tl} {
    \@@_if_graduate:TF {
      \tl_if_eq:NnTF \l_@@_value_degree_type_tl {academic} {
        \keys_set:nn {bithesis} {const/info/major = \c_@@_graduate_label_major_tl}
      } {
        \keys_set:nn {bithesis} {const/info/major = 学\quad 位\quad 领\quad 域}
      }
    } {
      \keys_set:nn {bithesis} {const/info/major = \@@_get_const:n {major}}
    }
  }
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{应用模板选项}
%
% 英文模板需要开启 ctexbook 宏包的英文选项。
%    \begin{macrocode}
\@@_if_thesis_english:T {
  \PassOptionsToClass{scheme=plain}{ctexbook}
}
%    \end{macrocode}
% 如果设置 blindPeerReview 选项，则抑制 twoside 选项。
%    \begin{macrocode}
\bool_if:NT \g_@@_blind_mode_bool {
  \bool_set_false:N \g_@@_twoside_bool
}
%    \end{macrocode}
%
% 如果没有开启双面打印选项，则在 ctexbook 中开启单面打印选项。
% 允许 chapter 直接另起一页（即使是偶数（左手）页）。
%    \begin{macrocode}
\bool_if:NTF \g_@@_twoside_bool {} {
  \PassOptionsToClass{oneside}{ctexbook}
}
\PassOptionsToClass{openany}{ctexbook}
%    \end{macrocode}
%
% 将 |bithesis/option/ctex| 中的值传递给 ctexbook 模板类。
%    \begin{macrocode}
% Any extra option passed by user will be passed to ctexbook.
\DeclareOption*{
  \PassOptionsToClass{\l_@@_options_to_ctex_tl}{ctexbook}
}
%    \end{macrocode}
%
% 抑制 fontspec 宏包关于字体的警告信息。
% 手动开启伪粗体、伪斜体。
%    \begin{macrocode}
\PassOptionsToPackage{quiet,AutoFakeBold=\g_@@_auto_fake_bold_tl,AutoFakeSlant}{xeCJK}
%    \end{macrocode}
% 加载 ctexbook 模板类。
%    \begin{macrocode}
\ProcessOptions\relax
\LoadClass[zihao=-4,]{ctexbook}
%    \end{macrocode}
%
% \subsubsection{定义模板类样式}
%
% 加载所需的宏包。
%
% 在加载宏包之前，尽量不使用 AtBeginDocument 等 hook。
% 因为 listings 等宏包也会用这些 hook。若在加载它们之前使用 hook，会导致我们所有 hook 在这些宏包“之前”运行。
%
%    \begin{macrocode}
\RequirePackage{geometry}
\RequirePackage[table,xcdraw]{xcolor}
% 恢复数学行距（restoremathleading），同时避免改变正文行距。
% （ctex 默认 linespread 1.3 × LaTeX 默认倍数 1.2 = 1.56）
\RequirePackage[bodytextleadingratio=1.56]{zhlineskip}
\RequirePackage{titletoc}
\RequirePackage{graphicx}
\RequirePackage{fancyhdr}
\RequirePackage[nodisplayskipstretch]{setspace}
\RequirePackage{booktabs}
\RequirePackage{multirow}
\RequirePackage{tikz}
\RequirePackage{etoolbox}
% Hide color and border in hyperref.
\RequirePackage[bookmarksnumbered]{hyperref}
% 详见 `caption` 宏包手册和
% https://github.com/CTeX-org/forum/issues/86
\RequirePackage[strut=off]{caption}
\RequirePackage{array}
\RequirePackage{amsmath}
\RequirePackage{amssymb}
\RequirePackage{pifont}
\RequirePackage{amsthm}
\RequirePackage{pdfpages}
\RequirePackage{listings}
\RequirePackage{enumitem}
\RequirePackage{fmtcount}
%    \end{macrocode}
%
% 抑制 \pkg{hyperref} 中对 |\hskip| 的 warning 信息。
%    \begin{macrocode}
\pdfstringdefDisableCommands{%
  \let\quad\empty
}
%    \end{macrocode}
%
% 设置页眉字号，页边距。
%
% 需要注意的是，根据 \pkg{geometry} 的规则，
% |headsep| 和 |footskip| 分别受到 |top| 与 |bottom| 的影响。
% 所以你能看到在计算 |headsep| 与 |footskip| 时，我们
% 首先计算了相应的偏移量。
%    \begin{macrocode}
\@@_if_graduate:TF {
  \int_gset:Nn \g_@@_head_zihao_int {5}
  \geometry{
    a4paper,
    left=2.7cm,
    bottom=2.5cm + 7bp,
    top=3.5cm + 7bp,
    right=2.7cm,
    % `headsep' is affected by `top' option.
    headsep = 3.5cm + 7bp - 2.5cm - 15bp,
    headheight = 15 bp,
    % `footskip' is affected by `bottom' option.
    footskip = 2.5cm + 7bp - 1.8cm,
  }
} {
  \int_gset:Nn \g_@@_head_zihao_int {4}
  \geometry{
    a4paper,
    left=3cm,
    bottom=2.6cm + 7bp,
    top=3.5cm + 7bp,
    right=2.6cm,
    % `headsep' is affected by `top' option.
    headsep = 3.5cm + 7bp - 2.4cm - 20bp,
    headheight = 20 bp,
    % `footskip' is affected by `bottom' option.
    footskip = 2.6cm + 7bp - 2cm,
  }
}
%    \end{macrocode}
%
%  \subsubsection{定义字体相关选项}
%
% 设置 Times New Roman 字体。
% 根据学校规范要求，默认情况下也使用 Times New Roman 字体。
%    \begin{macrocode}
\ctex_at_end_preamble:n {
  \bool_if:NTF \l_@@_style_better_new_roman_bool {
    \defaultfontfeatures[TeX~Gyre~Termes]
    {
     Extension      = .otf ,
     UprightFont    = texgyretermes-regular,
     BoldFont       = texgyretermes-bold,
     ItalicFont     = texgyretermes-italic,
     BoldItalicFont = texgyretermes-bolditalic,
    }
    \setmainfont{TeX~Gyre~Termes}
  }{
    \setmainfont{Times~New~Roman}
    \setromanfont{Times~New~Roman}
  }
}
%    \end{macrocode}
%
% \begin{macro}{\@@_load_unicode_math_pkg:}
% 加载\pkg{unicode-math}宏包。
%    \begin{macrocode}
\cs_new:Npn \@@_load_unicode_math_pkg:
  {
    \PassOptionsToPackage { \l_@@_unicode_math_options_tl } { unicode-math }
    \RequirePackage { unicode-math }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_define_math_font:nn}
% 批量定义数学字体配置。
% \begin{arguments}
%   \item 配置名称。
%   \item 字体名称。
% \end{arguments}
%    \begin{macrocode}
\cs_new:Npn \@@_define_math_font:nn #1#2
  {
    \cs_new:cpn { @@_load_math_font_ #1 : }
      {
        \@@_load_unicode_math_pkg:
        \setmathfont { #2 }
      }
  }
\clist_map_inline:nn
  {
    { asana          } { Asana-Math.otf             },
    { concrete       } { Concrete-Math.otf          },
    { erewhon        } { Erewhon-Math.otf           },
    { euler          } { Euler-Math.otf             },
    { fira           } { FiraMath-Regular.otf       },
    { garamond       } { Garamond-Math.otf          },
    { gfsneohellenic } { GFSNeohellenicMath.otf     },
    { kp             } { KpMath-Regular.otf         },
    { libertinus     } { LibertinusMath-Regular.otf },
    { lm             } { latinmodern-math.otf       },
    { newcm          } { NewCMMath-Regular.otf      },
    { stix           } { STIXMath-Regular.otf       },
    { stix2          } { STIXTwoMath-Regular.otf    },
    { xcharter       } { XCharter-Math.otf          },
    { xits           } { XITSMath-Regular.otf       },
    { bonum          } { texgyrebonum-math.otf      },
    { dejavu         } { texgyredejavu-math.otf     },
    { pagella        } { texgyrepagella-math.otf    },
    { schola         } { texgyreschola-math.otf     },
    { termes         } { texgyretermes-math.otf     }
  }
  { \@@_define_math_font:nn #1 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_load_math_font_cm:}
% 数学字体配置 |cm|。
%    \begin{macrocode}
\cs_new:Npn \@@_load_math_font_cm: { }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_load_math_font_none:}
% 数学字体配置 |none|。
%    \begin{macrocode}
\cs_new:Npn \@@_load_math_font_none: { }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_load_font:}
% 加载数学字体
%    \begin{macrocode}
\cs_new:Npn \@@_load_font:
  {
    \use:c { @@_load_math_font_  \l_@@_style_math_font_tl  : }
  }
%    \end{macrocode}
% \end{macro}
%
%  \paragraph{定义导言区末尾加载内容}
%
% 在 |preamble| 中，加载各个模板需要的字体。
%    \begin{macrocode}
\ctex_at_end_preamble:n {
  % 针对 Windows 字体采用 Fake Bold 宋体
  \bool_if:NT \l_@@_style_windows_simsum_fake_bold
  {
    \RequirePackage{ifplatform}
    \ifwindows
      \setCJKmainfont{SimSun}[AutoFakeBold,AutoFakeSlant]
    \fi
  }

  % 在导言区末尾加载数学字体。
  \@@_load_font:

  % misc / hideLinks 选项
  \bool_if:NTF \l_@@_misc_hide_links_bool
    {
      \hypersetup { hidelinks }
    } {
      \definecolor{blue}{RGB}{10,10,110}
      \hypersetup{
        colorlinks=true,
      }
    }

  \bool_if:NTF \l_@@_arial_as_title_font_bool {
    % 手动指定时要加载 Arial
    \newfontfamily\arialfamily{Arial}
  } {
    % 即使未指定，本科全英文专业模板的声明也需要 Arial
    \@@_if_thesis_int_type:nT {3} {
      \newfontfamily\arialfamily{Arial}
    }
  }
  % 无论中英文，封面都可能需要细黑体。
  \tl_if_blank:VTF \l_@@_cover_xihei_font_path_tl {}
  {
    \setCJKfamilyfont{xihei}[AutoFakeBold,AutoFakeSlant]
      {\l_@@_cover_xihei_font_path_tl}
  }

  % 对于本科全英文专业模板，需要自定义日期格式。
  \@@_if_thesis_int_type:nT {3} {
    \RequirePackage[en-US]{datetime2}
    \RequirePackage{indentfirst}
    \DTMlangsetup[en-US]{dayyearsep={\space}}
  }

  % Define biblatex category if it was imported.
  % 这部分是给研究生模板中的
  % 「攻读学位期间发表论文与研究成果清单」使用的。
  \cs_if_exist:NT \DeclareBibliographyCategory {
    \DeclareBibliographyCategory{mypub}
  }

  % Define biblatex strings if it was imported.
  % 这部分是给研究生模板中的
  % gbpunctin = false 时使用的。
  \cs_if_exist:NT \DefineBibliographyStrings {
    \DefineBibliographyStrings{english}{in={}}
    \DefineBibliographyStrings{english}{incn={}}
  }

  % 修改 biblatex 中「专利」（patent）部分的著录格式。
  % 主要根据北理工自定义的规范，参考 biblatex 和
  % biblatex-gb7714-2015 的实现修改而成。
  %
  % 默认不开启，因为此修改可能会产生其他边界问题。
  \bool_if:NT \g_@@_quirks_mode_bool {
    \cs_if_exist:NT \DeclareBibliographyDriver {
      %
      %   重设专利title的输出，将文献类型标识符输出出去
      %
      \newbibmacro*{patenttitle}{%原输出来自biblatex.def文件
        \ifboolexpr{%
          test{\iffieldundef{title}}%
          and%
          test{\iffieldundef{subtitle}}%
        }%
          {}%
          {\printtext[title]{\bibtitlefont%
             \printfield[titlecase]{title}%
             \ifboolexpr{test {\iffieldundef{subtitle}}}%这里增加了对子标题的判断，解决不判断多一个点的问题
             {}{\setunit{\subtitlepunct}%
             \printfield[titlecase]{subtitle}}%
              \iftoggle{bbx:gbtype}{\printfield[gbtypeflag]{usera}}{}%
             \iffieldundef{titleaddon}{}%判断一下titleaddon，否则直接加可能多一个空格
              {\setunit{\subtitlepunct}\printfield{titleaddon}}%
              % ：地区
              \setunit{\subtitlepunct}\iflistundef{location}
                {}
                {\setunit*{\subtitlepunct}%
                 \printtext{%[parens]
                   \printlist[][-\value{listtotal}]{location}}}%
              % ，专利号
              \setunit{\addcomma\addspace}\printfield{number}%写专利号
              \setunit{\addcomma\addspace}
              \usebibmacro{newsdate}%
           }%
        }%
      }

      %
      %   重定义专利文献驱动
      %
        \DeclareBibliographyDriver{patent}{%源来自standard.BBX
        \usebibmacro{bibindex}%
        \usebibmacro{begentry}%
        \usebibmacro{author}%
        \ifnameundef{author}{}{\setunit{\labelnamepunct}\newblock}%这一段用于去除作者不存在时多出的标点
        \usebibmacro{patenttitle}%给出专利专用的标题输出
        \iftoggle{bbx:gbstrict}{}{%
          \newunit%
          \printlist{language}%
          \newunit\newblock
          \usebibmacro{byauthor}
        }%
        \newunit\newblock
        \printfield{type}%
        \setunit*{\addspace}%
        \newunit\newblock
        \usebibmacro{byholder}%
        \newunit\newblock
        \printfield{note}%
        \newunit\newblock
        \usebibmacro{doi+eprint+url}%
        \newunit\newblock
        \usebibmacro{addendum+pubstate}%
        \setunit{\bibpagerefpunct}\newblock
        \usebibmacro{pageref}%
        \newunit\newblock
        \iftoggle{bbx:related}
          {\usebibmacro{related:init}%
           \usebibmacro{related}}
          {}%
        \usebibmacro{annotation}\usebibmacro{finentry}}
    }
  }
}
%    \end{macrocode}
%
% \begin{macro}{\@@_xihei:n}
% 定义华文细黑字体，采用 |cover/xiheiFont| 指定的字体文件。
% 为兼容旧版，XeLaTeX 上未指定字体文件时，会回退到中易字库的黑体。
%    \begin{macrocode}
\cs_new:Npn \@@_xihei:n #1 {
  \sys_if_engine_xetex:TF {
    \xeCJK_family_if_exist:nTF {xihei} {
      \CJKfamily{xihei} #1
    }{
      \heiti #1
    }
  } {
    \CJKfamily{xihei} #1
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_title_font:}
% 定义标题字体。
%    \begin{macrocode}
\cs_new:Npn \@@_title_font: {
  \bool_if:NTF \l_@@_arial_as_title_font_bool {
    % 即使是英文模板，仍可能出现中文，也需设置中文字体。
    \heiti\arialfamily
  } {
    % 西文保持原本的 Times New Roman。黑体一般不搭配衬线体，但学校要求如此。
    \heiti
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_unnumchapter_style:}
% 定义无序章节的样式。
%    \begin{macrocode}
\cs_new:Npn \@@_unnumchapter_style: {
  % 本科全英文、研究生学位论文需要加粗
  \int_compare:nNnTF {\g_@@_thesis_type_int} > {2} {
    \bfseries
  } {
    \mdseries
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\arabicHeiti}
% 遗留下来的黑体字体定义。
%    \begin{macrocode}
\cs_set:Npn \arabicHeiti #1 {#1}
%    \end{macrocode}
% \end{macro}
%
% 定义 \pkg{fancyhdr} 的页眉页脚。
%    \begin{macrocode}
\fancypagestyle{BIThesis}{
  \fancyhf{}
  % 此处按 N 判断是因为 style/head 可能填写 \LaTeX 这样的宏，不能随便按 e/x 展开；模板默认值虽需展开才能判断，但部分非空，所以不展开亦可。
  \tl_if_empty:NTF \l_@@_style_head_tl {
    % 若 style/head 为空，则不仅页眉文字为空，页眉线也应删除
    \renewcommand{\headrulewidth}{0pt}
  } {
    % 定义页眉（模板默认）
    \fancyhead[C]{
      \zihao{\int_use:N \g_@@_head_zihao_int}
      \ziju{0.08}\songti
      \tl_use:N \l_@@_style_head_tl
    }
    % 页眉分割线稍微粗一些
    \renewcommand{\headrulewidth}{0.6pt}
  }
  % 定义页码
  \fancyfoot[C]{\songti\zihao{5} \thepage}
}
%    \end{macrocode}
%
%  定义 \pkg{ctex} 的章节标题形式。
%    \begin{macrocode}
\ctexset{chapter={
    number = {\arabicHeiti{ \arabic{chapter} }},
    % 对于硕博模板，此处样式会被 \mainmatter 继续修改
    format = { \@@_title_font: \bfseries \centering \zihao{3}},
    nameformat = {},
    titleformat = {},
    aftername = \hspace{9bp},
    pagestyle = BIThesis,
    beforeskip = \@@_if_graduate:TF {13bp} {8bp},
    afterskip = \@@_if_graduate:TF {24.5bp} {32bp},
    fixskip = true,
    lofskip = 0cm,
    lotskip = 0cm,
  }
}
% 硕博模板于2026年3月将“第1章”改为“1”
\@@_if_graduate:T {
  \ctexset{ chapter / name = {} }
}

\ctexset{section={
    number = {\arabicHeiti{\thechapter.\hspace{1bp}\arabic{section}}},
    format = {\@@_title_font: \raggedright \bfseries \zihao{4}},
    nameformat = {},
    titleformat = {},
    aftername = \hspace{8bp},
    beforeskip = \@@_if_graduate:TF {26bp} {20bp},
    afterskip = \@@_if_graduate:TF {18bp} {17bp},
    fixskip = true,
  }
}

\ctexset{subsection={
    number = {
      \arabicHeiti{
        \thechapter.\hspace{1bp}
        \arabic{section}.\hspace{1bp}
        \arabic{subsection}
      }
    },
    format = {\@@_title_font: \bfseries \raggedright \zihao{-4}},
    nameformat = {},
    titleformat = {},
    aftername = \hspace{7bp},
    beforeskip = 17bp,
    afterskip = 17bp,
    fixskip = true,
  }
}

\ctexset{
  secnumdepth = 3,
  subsubsection={
    numbering = true,
    number = {
      \arabicHeiti{
        \arabic{chapter}.\hspace{1bp}
        \arabic{section}.\hspace{1bp}
        \arabic{subsection}.\hspace{1bp}
        \arabic{subsubsection}
      }
    },
    format={\@@_title_font: \raggedright \zihao{-4}},
    nameformat = {},
    titleformat = {},
    beforeskip=14bp,
    afterskip=14bp,
    fixskip=true,
  }
}
%    \end{macrocode}
%
%  定义 TOC 样式。
%    \begin{macrocode}
\addtocontents{toc}{\protect\hypersetup{hidelinks}}

\@@_if_graduate:TF {
  % 对于研究生模板，《撰写规范》要求各级标题字号不同，且缩进 2n 字符。
  % 为尽可能视觉对齐，缩进的字宽统一按四号 14pt，页码统一按小四。
  % chapter 不缩进，标题四号，页码小四。行距为保持与其它项相同，设定为：原本 baselinestretch 1.56 × 小四 12pt / 四号 14pt = 1.33714 倍。
  \titlecontents{chapter}[0pt]{\songti \zihao{4} \linespread{1.33714}\selectfont}
  {\thecontentslabel\unskip\hspace{\ccwd}}{}
  {\hspace{.5em}\zihao{-4}\titlerule*{.}\contentspage}
  % section 缩进两字，全部小四。
  \titlecontents{section}[14pt * 2]{\songti \zihao{-4}}
  {\thecontentslabel\hspace{\ccwd}}{}
  {\hspace{.5em}\titlerule*{.}\contentspage}
  % subsection 缩进四字，全部小四。
  \titlecontents{subsection}[14pt * 4]{\songti \zihao{-4}}
  {\thecontentslabel\hspace{\ccwd}}{}
  {\hspace{.5em}\titlerule*{.}\contentspage}
} {
  % 对于其他，定义各章标题为宋体小四号。
  \titlecontents{chapter}[0pt]{\songti \zihao{-4}}
  {\thecontentslabel\unskip\hspace{\ccwd}}{}
  {\hspace{.5em}\titlerule*{.}\contentspage}
  % section 标题为宋体小四号。
  \titlecontents{section}[1\ccwd]{\songti \zihao{-4}}
  {\thecontentslabel\hspace{\ccwd}}{}
  {\hspace{.5em}\titlerule*{.}\contentspage}
  % subsection 标题为宋体小四号。
  \titlecontents{subsection}[2\ccwd]{\songti \zihao{-4}}
  {\thecontentslabel\hspace{\ccwd}}{}
  {\hspace{.5em}\titlerule*{.}\contentspage}
}
% listoffigure 样式优化
\titlecontents{figure}[0pt]{\songti\zihao{-4}}
    {\figurename~\thecontentslabel\quad}{\hspace*{-1.5cm}}
    {\hspace{.5em}\titlerule*{.}\contentspage}
% listoftable 样式优化
\titlecontents{table}[0pt]{\songti\zihao{-4}}
    {\tablename~\thecontentslabel\quad}{\hspace*{-1.5cm}}
    {\hspace{.5em}\titlerule*{.}\contentspage}
%    \end{macrocode}
%
%  \subsubsection{定义样式相关函数}
%
% \begin{macro}{\frontmatter}
% 定义前置内容的页面样式。
%    \begin{macrocode}
\RenewDocumentCommand \frontmatter {} {
  \pagenumbering{Roman}
  % 这部分的章节标题不进行编号。
  \ctexset{
    chapter = {
      numbering = false,
    }
  }
  \linespread{1.53}\selectfont
  \pagestyle{BIThesis}

  % 调整表格内容字号（默认五号）和各行之间的距离。
  %
  % 由于这种方式会影响所有的表格，
  % 所以我们尽可能延迟这种影响。
  %
  % 不过，在目前的代码实现中没有在封面
  % 之类的地方使用表格，所以目前即使放在
  % preamble 中也不会有影响。
  %
  % 支持标准tabular、tabular*环境和宏包tabularx、longtable。
  \clist_map_inline:nn
    {tabular, tabular*, tabularx, longtable}
    {
      \AtBeginEnvironment {##1} {
        % 字号只想设置表格内容，不想影响caption。
        % 一般caption在环境之外，自然不受影响；
        % 而longtable的caption虽在环境内，但caption宏包能正常处理。
        \zihao{\l_@@_misc_tabular_font_size_tl}
        % 各行间距只想影响表格，不想影响矩阵，因此也必须在钩子中设置。
        \cs_set:Npn \arraystretch {\l_@@_misc_tabular_row_separation_tl}
      }
    }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mainmatter}
% 主体内容的页面样式。
%    \begin{macrocode}
\RenewDocumentCommand \mainmatter {} {
  % 另起一个空页，以便于后续的章节标题编号。
  % \clearpage
  \cleardoublepage
  % 这部分的章节标题进行编号。
  \ctexset{
    chapter = {
      numbering = true,
    }
  }
  % 硕博模板于2026年3月将正文部分的章标题改为顶左，但其它仍居中；本科模板全文样式统一，此处不必继续设置
  \@@_if_graduate:T {
    \ctexset{
      chapter / format+ = { \raggedright}
    }
  }
  % 页码使用阿拉伯数字。
  \pagenumbering{arabic}
  \pagestyle{BIThesis}
  % 正文行距 22 磅，同时略保有弹性以支持 style/pageVerticalAlign = scattered
  \setlength{\parskip}{0em plus 0.2em}
  \linespread{1.53}\selectfont
  % 修复脚注出现跨页的问题
  \interfootnotelinepenalty=10000
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\backmatter}
% 后置内容的页面样式。
%    \begin{macrocode}
\RenewDocumentCommand \backmatter {} {
  % 同样，所有的章节标题不进行编号。
  \setcounter{section}{0}
  \setcounter{subsection}{0}
  \setcounter{subsubsection}{0}
  \ctexset{
    chapter = {
      numbering = false,
      beforeskip = 18bp,
      format = {
        \@@_title_font: \@@_unnumchapter_style: \centering \zihao{3}
      },
      afterskip = \@@_if_graduate:TF {31bp} {26bp},
    }
  }
}
%    \end{macrocode}
% \end{macro}
%
% 设置图表浮动体与前后正文的间距，以及浮动体内部的图表本体与 caption 的间距。
%
% 此处只满足学校要求；后续`misc/floatSeparation`等设置会在此基础上叠加。
%
% 学校要求：
%
% - 本科生
%   - 图表与上下文之间各空一行。
%   - 空行一边是 caption，一边是正文，字号不同。空行的字号具体采用哪个没有说明。
% - 研究生
%   - 图序、图题、表序、表题单倍行距，段前6磅，段后6磅。
%   - 图表本体与正文的间距没有明确规定，不过Word模板实作明显窄于LaTeX默认间距（甚至小于行距），特别是表格。
%
% 这些间距在LaTeX中比较复杂：
%
% - 图表 caption 的布局位置相反，却共用术语
%
%   图自上而下的布局顺序是“正文 → 图本体 → caption → 正文”，而表是“正文 → caption → 表本体 → 正文”。
%   按照图的布局，caption 的 below 指“caption 🡘 正文”距离，above 指“图表本体 🡘 caption”距离。
%   这种 below、above 说法外推到了表的布局，导致表的所谓 below、above 与实际空间关系相反。
%   详见`caption`宏包手册。
%
% - 间距是多个设置之和
%
%   可能涉及以下四类设置。
%   1. 浮动体设置 intextsep、textfloatsep、floatsep ——默认是一段非零的弹性长度（由 LaTeX 和 CTeX 共同提供），可`setlength`。
%   2. caption 设置 belowcaptionskip、abovecaptionskip ——默认为零，可直接`setlength`，或间接用`captionsetup`设置。
%   3. `caption`宏包加的 strut——这部分为零，因为引入宏包时已设置`strut=off`。
%   4. 行距——捉摸不定（因为间距两边字号不同），并且难以修改。
%   详见 https://github.com/CTeX-org/forum/issues/86 和 https://tex.stackexchange.com/q/26521/266758。
%
% - 图表具体形式多样
%
%   图可能包含子图，子图还可能有多行，要避免各行间遮挡。
%   表除了普通 table，还可能有 longtable，后者有另外的`LTpre`、`LTpost`机制。
%
%    \begin{macrocode}
%
% 1. 设置相对简单的浮动体内部“图表本体 🡘 caption”距离。
%
\@@_if_graduate:TF {
  \captionsetup[table]{skip=8bp}
} {
  \setlength{\abovecaptionskip}{11pt}

  % 调整表格 caption 和表格本体间的距离。
  % 英文模板缺乏规定，只调整中文模板
  \@@_if_thesis_english:F {
    % 本来默认有一定空隙，现改为紧贴，这样更接近Word模板实作。
    \captionsetup[table]{skip=5pt}
  }
}
%
% 2. 设置比较复杂的图表浮动体与前后正文的间距。
%    a. 首先设置浮动体，保证“图表本体 🡘 caption”距离；
%    b. 然后设置 caption，在浮动体设置的基础上，补正“caption 🡘 正文”距离。
%    c. 同时适配 subcaption、longtable 等宏包。
%
\@@_if_graduate:TF {
  % 保留“正文 🡘 图片本体”距离不动，缩小“表格本体 🡘 正文”实际距离，使二者相近
  \AtBeginEnvironment{table}{
    \setlength{\intextsep}{3bp plus 1pt minus 1pt}
  }

  % 调整“正文 🡘 表格 caption”距离
  \captionsetup[table]{belowskip = 6bp + 0.25\baselineskip}

  % 调整“图片 caption 🡘 正文”距离
  % 这一距离是 intextsep 与 belowcaptionskip 之和。
  % 前者在小四 ctexbook 中默认为`14.0pt plus 4.0pt minus 4.0pt`，我们未改。
  % 计算下来，后者应设置为 14.0pt - 6bp 左右。
  \captionsetup[figure]{belowskip = -12pt}
  % 浮动体连续排列时，补偿以上负间距
  \addtolength{\floatsep}{12pt}

  % 适配子图
  \captionsetup[subfigure]{aboveskip = 3bp, belowskip = 3bp}

  \AtBeginDocument {
    % longtable 宏包有另外的机制，需专门调整
    \@ifpackageloaded{longtable}{
      \setlength{\LTpre}{0.2\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip}
      \setlength{\LTpost}{0.1\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip}
    }{}
  }
} {
  % 浮动体位于正文中间时，调整浮动体与上下正文之间的距离，即"前后加上一行空白"
  \setlength{\intextsep}{1.80\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip}
  % 浮动体位于页面顶部或底部时，调整浮动体与正文之间的距离，后或前加上一行空白
  \setlength{\textfloatsep}{1.80\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip}

  \@@_if_thesis_english:TF {
    \setlength{\belowcaptionskip}{9pt}
  } {
    % 为了满足“前后一行空白的问题”，需要删除 caption 下方的间距。
    %
    % 这里实际的 skip 在 15pt 左右，但是全部移除会导致当图片置于页面顶部时，
    % 图片与上方的间距过小，因此这里只移除 5pt。
    % 当然，这样会导致文本间的图片的 caption 下方的间距微微大于一行。
    %
    % 至于表格，`caption`宏包已考虑布局区别，统一设置 belowskip 即可。
    \captionsetup{belowskip=-5pt}
    % 不过 longtable 宏包有另外的机制，不设置 belowskip 时间距已可较小，
    % 设置成负数还导致 caption 和表格本体的距离变大。因此我们撤销更改。
    \captionsetup[longtable]{belowskip=0pt}

  }
  \AtBeginDocument {
    % longtable 宏包有另外的机制，需专门调整
    \@ifpackageloaded{longtable}{
      \setlength{\LTpre}{1.60\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip}
      \setlength{\LTpost}{1.60\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip}
    }{}
  }
}
%    \end{macrocode}
%
% 定义图表标题中的分隔字符，同时调整字号。
%    \begin{macrocode}
\@@_if_graduate:TF {
  \tl_gset:Nn \g_@@_label_divide_char_tl {.}
  % 研究生模板要求 "图序和图题间空1个中文字距"
  \captionsetup[figure]{font=small,labelsep=quad}
  % 研究生模板要求 "表序和题目间空1个中文字距"
  \captionsetup[table]{font=small,labelsep=quad}
  % 其它 caption 也参照上述格式
  \captionsetup[lstlisting]{font=small,labelsep=quad}
  \captionsetup[algorithm]{font=small,labelsep=quad}
} {
  \tl_gset:Nn \g_@@_label_divide_char_tl {-}
  % 本科生模板无 caption 字距要求
  \captionsetup[figure]{font=small,labelsep=space}
  \captionsetup[table]{font=small,labelsep=space}
  \captionsetup[lstlisting]{font=small,labelsep=space}
  \captionsetup[algorithm]{font=small,labelsep=space}
}
% 为了不强制导入宏包，且允许最终作者在导言区覆写，不使用`\@ifpackageloaded`或`\AtBeginDocument`
\ctex_at_end_package:nn {algorithm2e} {
  \SetAlCapFnt{\small}
  \SetAlCapNameFnt{\small}
}
%    \end{macrocode}
%
% 为保证各种表格、算法效果一致，要手动重置 setspace 宏包漏掉的情况，具体包括 longtable 和任意 H 型浮动体。
% 由于浮动体的 H 很难检测，干脆统一设置。（H 未必由 float 宏包提供，例如 algorithm2e 的 H 就不是。）
% 未使用 |\singlespacing|，因为它会在 |\setstretch{1}| 基础上空一行，添加多余空白。
%    \begin{macrocode}
\clist_map_inline:nn {longtable, table, algorithm} {
  \AtBeginEnvironment {#1} { \setstretch{1} }
}
%    \end{macrocode}
%
% 调整算法环境内部的字号。
%
% 学校无规定，但默认与正文相同，很怪。需要调小至与 caption 相同。
%    \begin{macrocode}
\AtBeginEnvironment {algorithmic} {\small}
\ctex_at_end_package:nn {algorithm2e} { \SetAlFnt{\small} }
%    \end{macrocode}
%
% 汉化宏包。
%    \begin{macrocode}
\@@_if_thesis_english:F {
  \ctex_at_end_package:nn {algorithm2e} {
    \SetKwInput{KwData}{数据}
    \SetKwInput{KwResult}{结果}
    \SetKwInput{KwIn}{输入}
    \SetKwInput{KwOut}{输出}
  }
  \ctex_at_end_package:nn {algorithmic} {
    \renewcommand*{\algorithmicrequire}{\textbf{输入：}}
    \renewcommand*{\algorithmicensure}{\textbf{输出：}}
  }
}
%    \end{macrocode}
%
% \begin{macro}{\thefigure,\thetable,\theequation,\thelstlisting,\lstlistingname}
% 定义各种计数器的格式。
%    \begin{macrocode}
% 图片：五号字。
\cs_set:Npn \thefigure {\thechapter\g_@@_label_divide_char_tl\arabic{figure}}

% 表格：五号字。
\cs_set:Npn \thetable {\thechapter\g_@@_label_divide_char_tl\arabic{table}}

% equation
\cs_set:Npn \theequation {\thechapter\g_@@_label_divide_char_tl\arabic{equation}}

% 适配 cleveref 宏包
% 在 AtBeginDocument 之外，保证最终作者可以覆写
\ctex_at_end_package:nn {cleveref} {
  % cleveref 支持同一处引用多个标签，英文涉及单复数两个值；
  % 而 BITSetup 主要为中文设计，接口只允许一个值，无法简单套用到英文。
  % 再考虑到我校缺乏英文规范，我们干脆只给中文适配，英文沿用 cleveref 默认，最终作者有需要可以覆写。
  \@@_if_thesis_english:F {
    % “第……”格式在编号后有字，autoref 无法可靠设置，BITSetup 也难以预留接口
    \crefformat{chapter}{第~#2#1#3~章}
    \crefformat{section}{第~#2#1#3~节}
    \crefformat{subsection}{第~#2#1#3~小节}
    \crefformat{subsubsection}{第~#2#1#3~小节}
    % 普通格式沿用 autoref 设置
    \crefname{figure}{\figureautorefname}{\figureautorefname}
    \crefname{table}{\tableautorefname}{\tableautorefname}
    \crefname{listing}{\c_@@_label_code_tl}{\c_@@_label_code_tl}
    \crefname{algorithm}{\l_@@_misc_autoref_algo_tl}{\l_@@_misc_autoref_algo_tl}
    \crefname{algo}{\l_@@_misc_autoref_algo_tl}{\l_@@_misc_autoref_algo_tl}
    \crefname{them}{\themautorefname}{\themautorefname}
    \crefname{lem}{\lemautorefname}{\lemautorefname}
    \crefname{prop}{\propautorefname}{\propautorefname}
    \crefname{cor}{\corautorefname}{\corautorefname}
    \crefname{axi}{\axiautorefname}{\axiautorefname}
    \crefname{defn}{\defnautorefname}{\defnautorefname}
    \crefname{conj}{\conjautorefname}{\conjautorefname}
    \crefname{exmp}{\exmpautorefname}{\exmpautorefname}
    \crefname{case}{\caseautorefname}{\caseautorefname}
    \crefname{rem}{\remautorefname}{\remautorefname}
    % 同一处引用多个公式较为常见，专门适配
    % #2、#3 标识的链接范围不可包含括号，否则影响标点挤压。
    \crefformat{equation}{\equationautorefname（#2#1#3）}
    \crefrangeformat{equation}{\equationautorefname（#3#1#4）至（#5#2#6）}
    \crefmultiformat{equation}{\equationautorefname（#2#1#3）}{和（#2#1#3）}{、（#2#1#3）}{和（#2#1#3）}
  }
}

% code snippet
\AtBeginDocument{
  \cs_gset:Npn \thelstlisting {\thechapter\g_@@_label_divide_char_tl\arabic{lstlisting}}
  \cs_gset:Npn \lstlistingname {\c_@@_label_code_tl}

  % 定义算法标题
  % 针对模板的 algo 环境
  \tl_set:Nn \algoautorefname {\l_@@_misc_autoref_algo_tl}
  % 针对 algorithm 宏包
  \tl_set:Nn \ALG@name {\l_@@_misc_autoref_algo_tl}
  % 针对 algorithm2e 宏包
  \tl_set:Nn \algorithmcfname {\l_@@_misc_autoref_algo_tl}

  % 定义算法的 autoref
  % algorithm2e 宏包会覆写它，所以我们必须AtBeginDocument时再修改
  \tl_set:Nn \algorithmautorefname {\l_@@_misc_autoref_algo_tl}

  % 算法变成「章节号-序号」
  % 为了减少修改，我们只适配按章编号的情况。
  % 针对 algorithm 宏包
  \@ifpackagewith{algorithm}{chapter}{
    \cs_gset:Npn \thealgorithm
    {\thechapter\g_@@_label_divide_char_tl\arabic{algorithm}}
  }{}
  % 针对 algorithm2e 宏包
  \@ifpackagewith{algorithm2e}{algochapter}{
    % 名字中的“cf”是指其作者 Christophe Fiorio。
    \cs_gset:Npn \thealgocf
    {\thechapter\g_@@_label_divide_char_tl\arabic{algocf}}
  }{}

  % 默认的情况下，保留公式和上下文的一定间距。（会比 Word 稍宽一些）
  \setlength{\abovedisplayskip}{\l_@@_style_math_above_display_skip_dim}
  \setlength{\abovedisplayshortskip}{\l_@@_style_math_above_display_skip_dim}
  \setlength{\belowdisplayskip}{\l_@@_style_math_below_display_skip_dim}
  \setlength{\belowdisplayshortskip}{\l_@@_style_math_below_display_skip_dim}
  % 调整浮动体与文字之间的距离
  \addtolength{\intextsep}{\l_@@_misc_float_separation_tl\baselineskip}
  \addtolength{\textfloatsep}{\l_@@_misc_float_separation_tl\baselineskip}
  % longtable 宏包有另外的机制，需专门调整
  \@ifpackageloaded{longtable}{
    \addtolength{\LTpre}{\l_@@_misc_float_separation_tl\baselineskip}
    \addtolength{\LTpost}{\l_@@_misc_float_separation_tl\baselineskip}
  }{}
  % 调整算法与文字之间的距离
  % 针对 algorithm2e 宏包
  \@ifpackageloaded{algorithm2e}{
    % 宏包手册介绍可自定义宏，再`\SetAlgoSkip`；我们为简洁，直接覆写。
    \renewcommand{\@algoskip}{\vspace{\l_@@_misc_algorithm_separation_tl}}
  }{}
}
%    \end{macrocode}
% \end{macro}
%
% 调整底层 TeX 排版引擎参数以保证所有段落能够很好地以两端对齐的方式呈现。
% 是的，这是祖传代码。
% 在英文模式下禁用，因为这段代码会禁用 hyphenation.
%    \begin{macrocode}
\bool_if:NF \l_@@_style_hyphen_bool {
  \hbadness=10000
  \tolerance=1
  \emergencystretch=\maxdimen
  \hyphenpenalty=10000
}
%    \end{macrocode}
%
% 自定义一个默认的 lstlisting 样式。
%    \begin{macrocode}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{examplestyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}
\lstset{style=examplestyle}
%    \end{macrocode}
%
% 调整插图目录与表格目录的标题。
%    \begin{macrocode}
\cs_set:Npn \listfigurename {\currentpdfbookmark{\c_@@_label_figure_tl}{ch:figures}\@@_get_const:n {figure}}
\cs_set:Npn \listtablename {\currentpdfbookmark{\c_@@_label_table_tl}{ch:tables}\@@_get_const:n {table}}
%    \end{macrocode}
%
% 预定义用户常用的证明环境。
%    \begin{macrocode}
\theoremstyle{plain}
  \newtheorem{algo}{\@@_get_const:n {algo}}[chapter]
  \newtheorem{them}{\@@_get_const:n {them}}[chapter]
  \newtheorem{lem}{\@@_get_const:n {lem}}[chapter]
  \newtheorem{prop}{\@@_get_const:n {prop}}[chapter]
  \newtheorem{cor}{\@@_get_const:n {cor}}[chapter]
  \newtheorem{axi}{\@@_get_const:n {axi}}[chapter]
\theoremstyle{definition}
  \newtheorem{defn}{\@@_get_const:n {defn}}[chapter]
  \newtheorem{conj}{\@@_get_const:n {conj}}[chapter]
  \newtheorem{exmp}{\@@_get_const:n {exmp}}[chapter]
  \newtheorem{case}{\@@_get_const:n {case}}
\theoremstyle{remark}
  \newtheorem{rem}{\@@_get_const:n {rem}}
  \renewcommand{\qedsymbol}{\ensuremath{\blacksquare}}
%    \end{macrocode}
%
% \begin{macro}{\@@_dunderline:nnn,\@@_dunderline:n}
% 用于渲染下划线。
%
% 参数如下：
% \begin{itemize}
%   \item \#1 |dim| 线的竖直偏移量，例如 |-10pt|。
%   \item \#2 |dim| 线宽（粗细），例如 |1pt|。
%   \item \#3 |tl| 文字内容。
% \end{itemize}
% 
% 前两个可以统一不提供，此时分别默认为 |-1pt|、|1pt|。
%    \begin{macrocode}
\cs_new:Npn \@@_dunderline:nnn #1#2#3 {
  {\setbox0=\hbox{#3}\ooalign{\copy0\cr\rule[\dimexpr#1-#2\relax]{\wd0}{#2}}}
}
\cs_new:Npn \@@_dunderline:n #1 {
  \@@_dunderline:nnn {-1pt} {1pt} {#1}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{|@@_render_cover_entry:nn}
% 用于渲染封面的辅助函数。
%
% 参数如下：
% \begin{itemize}
%   \item \#1 |{token_list}| 为封面信息条目的名称，含分隔符。
%   \item \#2 |{token_list}| 为封面信息条目的内容。
% \end{itemize}
%
% 需要在 |\l_@@_cover_label_max_width_dim| 和 |\l_@@_cover_value_max_width_dim|
% 存储已经计算出来的最大宽度。
%    \begin{macrocode}
\cs_new:Npn \@@_render_cover_entry:nn #1#2 {
  \makebox[\l_@@_cover_label_max_width_dim][\l_@@_cover_label_align_tl]{
    \tl_if_blank:VTF #1 {} {#1}
  }
  \hspace{1ex}
  \@@_dunderline:nnn{\l_@@_cover_underline_offset_dim}
    {\l_@@_cover_underline_thickness_dim}{
    \makebox[\l_@@_cover_value_max_width_dim][\l_@@_cover_value_align_tl]{#2}
  }\par
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{|@@_cover_entry_rendered_width:}
% 预估 |\@@_render_cover_entry:nn| 会占用的宽度，可用于判断行是否会溢出
%    \begin{macrocode}
\cs_new:Nn \@@_cover_entry_rendered_width: {
  \l_@@_cover_label_max_width_dim + 1ex + \l_@@_cover_value_max_width_dim
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{|@@_get_text_width:Nn,\@@_get_text_width:NV}
% 计算 \#2 所占用的宽度，将结果存储在 \#1 中。
%
% 参数如下：
% \begin{itemize}
%   \item \#1 |dim| 存储宽度的变量。
%   \item \#2 |tl| 要计算宽度的文本。
% \end{itemize}
%    \begin{macrocode}
\cs_new:Npn \@@_get_text_width:Nn #1#2
  {
    \hbox_set:Nn \l_tmpa_box {#2}
    \dim_set:Nn #1 { \box_wd:N \l_tmpa_box }
  }
\cs_generate_variant:Nn \@@_get_text_width:Nn { NV }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_get_max_text_width:NN}
% 从 \#2 中获取最大的文本宽度，然后设置到 \#1 中。
%
% 参数如下：
% \begin{itemize}
%   \item \#1: |dim| 用于输出最大宽度。
%   \item \#2: |seq| 用于输入文本。
% \end{itemize}
%    \begin{macrocode}
\cs_new:Npn \@@_get_max_text_width:NN #1#2
  {
% 这里用 |group| 确保局部变量不会被污染。
    \group_begin:
      \seq_set_eq:NN \l_@@_tmpa_seq #2
      \dim_zero:N #1
      \dim_zero_new:N \l_@@_tmpa_dim
      \bool_until_do:nn { \seq_if_empty_p:N \l_@@_tmpa_seq }
        {
          \seq_pop_left:NN \l_@@_tmpa_seq \l_@@_tmpa_tl
          \@@_get_text_width:NV \l_@@_tmpa_dim \l_@@_tmpa_tl
          \dim_gset:Nn #1 { \dim_max:nn {#1} { \l_@@_tmpa_dim } }
        }
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_entry:nn}
% 解析封面信息条目，丢弃空项，并添加分隔符。
%
% 参数如下：
% \begin{itemize}
%   \item \#1: |tl| 为封面信息条目的名称。
%   \item \#2: |tl| 为封面信息条目的内容。
% \end{itemize}
% |\\| 会被视为换行符，从而实现信息条目换行的效果。
%
% 结果会存储到 |\l_@@_tmp_left_seq| 和 |\l_@@_tmp_right_seq|。
%
%    \begin{macrocode}
\cs_new:Npn \@@_parse_entry:nn #1 #2 {
  \seq_clear:N \l_@@_tmp_left_seq
  \seq_clear:N \l_@@_tmp_right_seq

  % 只处理 value 非空的项
  \tl_set:Nn \l_tmpa_tl {#2}
  \tl_if_empty:xTF {\l_tmpa_tl} {} {
    \seq_set_split:Nnx \l_@@_tmp_right_seq {\\} {#2}
    \seq_map_inline:Nn \l_@@_tmp_right_seq {
      \seq_put_right:Nn \l_@@_tmp_left_seq {}
    }
    \seq_put_left:Nn \l_@@_tmp_left_seq {#1\l_@@_cover_delimiter_tl}
    \seq_pop_right:NN \l_@@_tmp_left_seq \g_@@_trashcan_tl
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_render_cover_entry}
% 渲染封面信息项。此函数为主函数。
%    \begin{macrocode}
\msg_new:nnn { bithesis } { render-cover-entry/overfull-hbox }
  {
    One~or~more~cover~entries~are~too~wide,~which~may~result~in~poor~layout.\\
    To~fix~it,~please~split~long~entries~into~multiple~lines~by~inserting~“\c_backslash_str\c_backslash_str”.\\
    \@@_warning_footer:n { render-cover-entry/overfull-hbox }
  }
\cs_new:Npn \@@_render_cover_entry:N #1 {
  % 左边是标签，右边是值。
  % 形如：
  % { {label_1} {value_1}, {label_2} {value 2} }
  % 首先转换成 seq 类型。
  \seq_set_from_clist:NN \l_@@_input_seq #1
  \seq_map_inline:Nn \l_@@_input_seq {
    % 然后对于每一对 label 和 value，首先查找
    % value 中是否含有 \\ 字符，如果有，则将其分割成多个
    % label - value 对。
    % 比如 {label_1} {value \\ 1} 会被转换成
    % { {label_1} {value}, {} {1} }
    \@@_parse_entry:nn ##1
    % 然后将这些 label - value 对添加到 \l_@@_right_seq
    % 或者 \l_@@_left_sql 中。
    % left 就是 label，right 就是 value。
    \seq_concat:NNN \l_@@_right_seq \l_@@_right_seq \l_@@_tmp_right_seq
    \seq_concat:NNN \l_@@_left_seq \l_@@_left_seq \l_@@_tmp_left_seq
  }

  % 如果用户选择自动计算最大宽度，则计算最大宽度。
  \bool_if:NT \l_@@_cover_auto_width_bool {
    \@@_get_max_text_width:NN \l_@@_cover_label_max_width_dim \l_@@_left_seq
    \@@_get_max_text_width:NN \l_@@_cover_value_max_width_dim \l_@@_right_seq
    % 在 value 两边加上空白，避免文本太靠边。
    \dim_add:Nn \l_@@_cover_value_max_width_dim { \l_@@_cover_auto_width_padding_dim * 2 }
  }
  \dim_compare:nNnT { \linewidth } < { \@@_cover_entry_rendered_width: } {
    \msg_warning:nn {bithesis} {render-cover-entry/overfull-hbox}
  }


  % 最后，根据宽度渲染 label 和 value 对。
  \group_begin:
    \bool_until_do:nn { \seq_if_empty_p:N \l_@@_left_seq }
      {
        \seq_pop_left:NN \l_@@_left_seq \l_@@_tmpa_tl
        \seq_pop_left:NN \l_@@_right_seq \l_@@_tmpb_tl
        \@@_render_cover_entry:nn {\l_@@_tmpa_tl} {\l_@@_tmpb_tl}
      }
  \group_end:
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_make_graduate_cover:}
% 制作研究生论文模板封面。
%
% 学校会发一个彩色封皮，上半部分带有“北京理工大学”“博士学位论文”“（专业型）”等通用标题。
% 这里制造的封面包含文章信息，打印店会把它打印到通用封皮上。
% 因此，这里生成的内容要避让封皮的通用内容，不能随意扩张。
%    \begin{macrocode}
\cs_new:Npn \@@_make_graduate_cover: {
  \cleardoublepage
  \bool_if:NT \l_@@_internal__pre_frontmatter__bookmarked_bool {
    \currentpdfbookmark{封面}{pre-frontmatter:cover}
  }
  \begin{titlepage}
    {
      \heiti\zihao{5}
      \tl_if_blank:VTF \l_@@_value_classified_level_tl {} {
        \flushright
        \c_@@_label_classified_level_tl：~
        \l_@@_value_classified_level_tl \par
      }
    }
    \centering
    \vspace*{90mm}
    {\heiti\zihao{-2} \l_@@_value_title_tl \par}
    \vskip 50mm
    {
      % 渲染信息。
      \clist_set:Nn \l_@@_input_clist {
        {姓名：} {\@@_secret_info_fixed:Nn \l_@@_value_author_tl {3}},
        {学号：} {\@@_secret_info:N \l_@@_value_student_id_tl},
        {学院：} {\l_@@_value_school_tl},
      }
      \linespread{2.0}\selectfont
      \dim_set:Nn \l_@@_cover_auto_width_padding_dim {2em}
      % 黑体 小三
      \heiti \zihao{-3} \@@_render_cover_entry:N \l_@@_input_clist
    }
    \vskip 10mm
    % 黑体 小三
    {\heiti \zihao{-3} \l_@@_cover_date_tl}
  \end{titlepage}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_make_paper_back:}
% 制作书脊。
%    \begin{macrocode}
\msg_new:nnn { bithesis } { paper-back/missing-degree-type-icon-file }
  {
    Failed~to~find~the~file~for~degree~type~in~the~paper~back:~#1.\\
    Please~download~from~https://github.com/BITNP/BIThesis/blob/main/templates/graduate-thesis/ #1.\\
    \@@_warning_footer:n { paper-back/missing-degree-type-icon-file }
  }
\cs_new:Npn \@@_make_paper_back: {
  \cleardoublepage
  \bool_if:NT \l_@@_internal__pre_frontmatter__bookmarked_bool {
    \currentpdfbookmark{书脊}{pre-frontmatter:paperback}
  }
  \begin{titlepage}
    \centering
    % 实现竖排——将水平宽度设得很窄，让文字自动换行，并改小行距
    \linespread{1.1}\selectfont
    \begin{minipage}[c][19.7cm]{2em}
      \centering
      {
        \tl_new:N \l_@@_icon_path_tl
        \tl_set:Nx \l_@@_icon_path_tl { misc/icon_ \l_@@_value_degree_type_tl .jpg }
        \file_if_exist:nTF {\l_@@_icon_path_tl} {
          \includegraphics[width=1.5em]{\l_@@_icon_path_tl}
        } {
          \msg_warning:nnV {bithesis} {paper-back/missing-degree-type-icon-file} \l_@@_icon_path_tl
        }
      }
      \vspace{1em plus 1fill}
      \par
      {
        \heiti\zihao{3}
        \tl_if_blank:VTF \l_@@_value_vertical_title_tl
          {\l_@@_value_title_tl}{\l_@@_value_vertical_title_tl}
      }
      \par
      \vspace{1em plus 1fill}
      {\heiti\zihao{3}\@@_secret_info:nn{\l_@@_value_author_tl}{\g_@@_const_substitute_symbol_tl\quad\g_@@_const_substitute_symbol_tl\quad\g_@@_const_substitute_symbol_tl}}
      \par
      \vspace{1em plus 1fill}
      {\heiti\zihao{3}\c_@@_label_university_tl}
    \end{minipage}
  \end{titlepage}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_make_chinese_title_page:}
% 制作中文封面页。
%    \begin{macrocode}
\cs_new:Npn \@@_make_chinese_title_page: {
  \cleardoublepage
  \bool_if:NT \l_@@_internal__pre_frontmatter__bookmarked_bool {
    \currentpdfbookmark{中文题名页}{pre-frontmatter:titlepage-zh}
  }
  \begin{titlepage}
      \noindent\begin{minipage}[t]{0.48\textwidth}
        % 分类号、学校代码
        {\heiti \zihao{5} \c_@@_label_classification_tl}
        \l_@@_value_classification_tl\\
        {\heiti \zihao{5} \c_@@_label_udc_tl}  \l_@@_value_udc_tl\\
        {\heiti \zihao{5} \c_@@_label_university_code_tl}  \l_@@_value_university_code_tl
      \end{minipage}

      \hfill

      % 以下内容是「学生类型」的内容，
      % 若 cover/showSpecialTypeBox 为 false 且没有任何勾选，则隐藏。
      \bool_if:nT {\l_@@_cover_show_special_type_box_bool || \l_@@_value_international_student_ugp_bool || \l_@@_value_cross_research_bool || \l_@@_value_engineering_special_plan_bool} {
        \begin{minipage}[t]{0.48\textwidth}
          \vspace{-12pt}
          \begin{flushright}
          \setlength\fboxrule{1pt}\setlength\fboxsep{3mm}
          \fbox{
            \noindent\begin{minipage}{0.48\linewidth}
              \heiti \zihao{5}
              \scalebox{1.1}\BigStar{}\hspace{4pt} \c_@@_label_special_type_tl\\

              {
                \zihao{-4}
                \bool_if:NTF \l_@@_value_engineering_special_plan_bool {\@@_boxcheck:} {\@@_boxempty:}
              }
              \hspace{1pt}\c_@@_label_engineering_special_plan_tl\\

              {
                \zihao{-4}
                \bool_if:NTF \l_@@_value_cross_research_bool {\@@_boxcheck:} {\@@_boxempty:}
              }
              \hspace{1pt}\c_@@_label_cross_research_tl\\

              {
                \zihao{-4}
                \bool_if:NTF \l_@@_value_international_student_ugp_bool {\@@_boxcheck:} {\@@_boxempty:}
              }
              \hspace{1pt}\c_@@_label_international_student_ugp_tl
            \end{minipage}
          }
          \end{flushright}
        \end{minipage}
        % 保证下面的内容空间不会受到挤占。
        \vspace{-35pt}
      }

     \begin{center}

      \vskip \stretch{1}

      {\heiti\zihao{-2} \l_@@_value_title_tl \par}

      \vskip \stretch{1}

      \def\tabcolsep{1pt}
      \def\arraystretch{1.5}

      {
        \renewcommand{\baselinestretch}{2}

        \tl_if_empty:NT \l_@@_cover_delimiter_tl {
          \tl_set:Nn \l_@@_cover_delimiter_tl {\qquad}
        }
        \dim_set:Nn \l_@@_cover_underline_offset_dim {-5pt}

        % 如果不是自动计算宽度，且用户没有自定义宽度，
        % 则尝试提供一个默认宽度。
        \bool_if:NF \l_@@_cover_auto_width_bool {
          \dim_compare:nNnT {\l_@@_cover_label_max_width_dim} = {0pt} {
            \dim_set:Nn \l_@@_cover_label_max_width_dim {45mm}
          }
          \dim_compare:nNnT {\l_@@_cover_value_max_width_dim} = {0pt} {
            \dim_set:Nn \l_@@_cover_value_max_width_dim {60mm}
          }
        }

        % 渲染信息。
        % 此处必须限制盲审模式替换符号数量，不然会泄露信息，还可能导致行溢出
        \clist_set:Nn \l_@@_input_clist {
            {\c_@@_graduate_label_author_tl} {\@@_secret_info_fixed:Nn \l_@@_value_author_tl {3}},
            {\c_@@_graduate_label_school_tl} {\l_@@_value_school_tl},
            {\c_@@_graduate_label_supervisor_tl} {\@@_secret_info_fixed:Nn \l_@@_value_supervisor_tl {5}},
            {\c_@@_graduate_label_industrial_mentor_tl} {\@@_secret_info_fixed:Nn \l_@@_value_industrial_mentor_tl {5}},
            {\c_@@_graduate_label_chairman_tl} {\@@_secret_info_fixed:Nn \l_@@_value_chairman_tl {5}},
            {\g_@@_const_info_degree_tl} {\l_@@_value_degree_tl},
            {\g_@@_const_info_major_tl} {\l_@@_value_major_tl},
            {\c_@@_graduate_label_institute_tl} {\l_@@_value_institute_tl},
            {\c_@@_graduate_label_submission_date_tl} {\l_@@_value_submission_date_tl},
         }

        \heiti\zihao{-3}
        \@@_render_cover_entry:N \l_@@_input_clist
      }
    \end{center}
    \vskip \stretch{0.5}
  \end{titlepage}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_make_english_title_page:}
% 制作英文封面页。
%    \begin{macrocode}
\cs_new:Npn \@@_make_english_title_page: {
  \bool_if:NT \l_@@_internal__pre_frontmatter__bookmarked_bool {
    \currentpdfbookmark{英文题名页}{pre-frontmatter:titlepage-en}
  }
  \begin{titlepage}
    \begin{center}

    \vspace*{10em}

    {
      \zihao{-2}
      \textbf{\l_@@_value_title_en_tl}
      \par
    }

    \vskip \stretch{1}

    {
      \tl_if_empty:NT \l_@@_cover_delimiter_tl {
        \tl_set:Nn \l_@@_cover_delimiter_tl {:~}
      }

      \tl_set:Nn \l_@@_cover_label_align_tl {l}
      \dim_set:Nn \l_@@_cover_underline_offset_dim {-5pt}

      % 如果不是自动计算宽度，且用户没有自定义宽度，
      % 则尝试提供一个默认宽度。
      \bool_if:NF \l_@@_cover_auto_width_bool {
        \dim_compare:nNnT {\l_@@_cover_label_max_width_dim} = {0pt} {
          \dim_set:Nn \l_@@_cover_label_max_width_dim {55mm}
        }
        \dim_compare:nNnT {\l_@@_cover_value_max_width_dim} = {0pt} {
          \dim_set:Nn \l_@@_cover_value_max_width_dim {85mm}
        }
      }

      % 渲染信息。
      % 此处必须限制盲审模式替换符号数量，不然会泄露信息，还可能导致行溢出
      \clist_set:Nn \l_@@_input_clist {
          {\c_@@_graduate_label_author_en_tl} {\@@_secret_info_fixed:Nn \l_@@_value_author_en_tl {9}},
          {\c_@@_graduate_label_school_en_tl} {\l_@@_value_school_en_tl},
          {\c_@@_graduate_label_supervisor_en_tl} {\@@_secret_info_fixed:Nn \l_@@_value_supervisor_en_tl {12}},
          {\c_@@_graduate_label_industrial_mentor_en_tl} {\@@_secret_info_fixed:Nn \l_@@_value_industrial_mentor_en_tl {12}},
          {\c_@@_graduate_label_chairman_en_tl} {\@@_secret_info_fixed:Nn \l_@@_value_chairman_en_tl {12}},
          {\c_@@_graduate_label_degree_en_tl} {\l_@@_value_degree_en_tl},
          {\c_@@_graduate_label_major_en_tl} {\l_@@_value_major_en_tl},
          {\c_@@_graduate_label_institute_en_tl} {\l_@@_value_institute_en_tl},
          {\c_@@_graduate_label_submission_date_en_tl} {\l_@@_value_submission_date_en_tl},
       }

      \zihao{-3}
      \@@_render_cover_entry:N \l_@@_input_clist
    }

    \end{center}

    \vskip \stretch{0.5}
  \end{titlepage}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\circled}
% 圆形数字编号定义。
%    \begin{macrocode}
\newcommand{\circled}[2][]{\tikz[baseline=(char.base)]
  {\node[shape = circle, draw, inner~sep = 1pt]
  (char) {\phantom{\ifblank{#1}{#2}{#1}}};
  \node at (char.center) {\makebox[0pt][c]{#2}};}}
\robustify{\circled}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_graduate_originality:}
% 研究生原创性声明。
%    \begin{macrocode}
\cs_new:Npn \@@_graduate_originality:
  {
    % 取消页眉页脚。
    \ctexset {
      chapter / pagestyle = plain,
    }

    \begin{titlepage}
      % 不计算页码。
      \pagenumbering{gobble}

      % 原创性声明部分
      \begin{center}
        \@@_same_page:
        \ctexset{
          chapter = {
            titleformat = {\heiti\zihao{-2}},
            afterskip = 30pt,
          }
        }
        \bool_if:NT \l_@@_internal__pre_frontmatter__bookmarked_bool {
          \currentpdfbookmark{\c_@@_graduate_label_originality_tl}{pre-frontmatter:originality}
        }
        \chapter*{
          \c_@@_graduate_label_originality_tl
        }
      \end{center}

      % 本部分字号为三号，且 Word 行距为 28 pt（≈ 1.46 × 1.2 基础行距 × 16 pt 三号字）。
      \zihao{3}
      \linespread{1.46}\selectfont

      \qquad\c_@@_graduate_label_originality_clause_tl
      \vspace{3\baselineskip}

      \begin{flushright}
        \c_@@_graduate_label_originality_author_signature_tl\par
      \end{flushright}

      \cleardoublepage

      % 使用授权声明部分。
      \begin{center}
        \@@_same_page:
        \ctexset{
          chapter = {
            titleformat = {\heiti\zihao{-2}},
            afterskip = 30pt,
          }
        }
        \bool_if:NT \l_@@_internal__pre_frontmatter__bookmarked_bool {
          \currentpdfbookmark{\c_@@_graduate_label_authorization_tl}{pre-frontmatter:originality-authorization}
        }
        \chapter*{
          \c_@@_graduate_label_authorization_tl
        }
      \end{center}

      \qquad\c_@@_graduate_label_authorization_clause_tl

      \vspace{3\baselineskip}

      \begin{flushright}
        \begin{spacing}{1.65}
          \zihao{3}
          % \hspace{5mm}\raisebox{-2ex}{\includegraphics[width=30mm]{example-image}}\hspace{5mm}
          \c_@@_graduate_label_originality_author_signature_tl\par
          \vspace{1\baselineskip}
          \c_@@_graduate_label_originality_supervisor_signature_tl\par
        \end{spacing}
      \end{flushright}
    \end{titlepage}
    \cleardoublepage
  }
%    \end{macrocode}
% \end{macro}
