\usepackage[fontset=windows]{ctex} % invole CJKfntef
\usepackage{xeCJKfntef}
\usepackage{setspace} % spacing
\usepackage{xcolor}   % color
\usepackage{xltxtra}  % print a XeLaTeX
\usepackage{setspace}
\usepackage{subfiles}
\usepackage{fontawesome5}
\usepackage{float}

% Float Environment
\renewcommand{\floatpagefraction}{0.75}

% Font family
\setmainfont[Mapping=tex-text]{Times New Roman}

% 正文字体是宋体小四 1.5行距
\renewcommand{\normalsize}{\zihao{-4}\songti\normalfont}

% 处理中文排版的间距包
\usepackage[
    UseMSWordMultipleLineSpacing,
    MSWordLineSpacingMultiple=1.5
]{zhlineskip}

% 处理标题的上下间距

 \ctexset{%
      % chapter = {
      %   beforeskip = 27bp,
      %   afterskip  = 27bp,
      % },
      % section = {
      %   beforeskip = 12bp,
      %   afterskip  = 0bp,
      % },
      % subsection = {
      %   beforeskip = 12bp,
      %   afterskip  = 0bp,
      % },
      % subsubsection = {
      %   beforeskip = 12bp,
      %   afterskip  = 0bp,
      % },
}%

% Word spacing
% Usage: \ziju{spacing} spacing can be 1em, 10pt, etc.
%\newcommand{\ziju}[1]{\renewcommand{\CJKglue}{\hskip #1}}

%%%%%% Page Style %%%%%%

% Margin
\usepackage[paper=a4paper,left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm,headheight=1.5cm,headsep=0.6cm,footskip=0.5cm]{geometry}


% Header and footer
\usepackage{fancyhdr}

% 正文的页眉页脚格式
\fancypagestyle{mainmatter}{
  \fancyhf{} % clear all header and footer fields
  \fancyhead[C]{\songti\zihao{-5}{北京邮电大学本科毕业设计（论文）}}
  \fancyfoot[C]{\songti\zihao{-5}{\thepage}}
  \renewcommand{\headrulewidth}{0.8pt}
}

% 目录页的页眉页脚格式
\fancypagestyle{frontmatter}{
  \fancyhf{} % clear all header and footer fields
  \fancyfoot[C]{\songti\zihao{-5}{\thepage}}
  \renewcommand{\headrulewidth}{0pt}
}

% Bookmark  (Chinese bookmark supported)
\usepackage{url}
\def\UrlFont{}
\usepackage[xetex, pdfstartview=FitH, 
bookmarksnumbered=true, bookmarksopen=true, bookmarksdepth=2, colorlinks=true, 
pdfborder=001, linkcolor=black, citecolor=black, urlcolor=black]{hyperref}  % 修改bookmarksdepth指定书签层级
% Deal with line breaking in url
\makeatletter
\def\UrlAlphabet{%
    \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
    \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
    \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
    \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
    \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
    \do\Y\do\Z}
\def\UrlDigits{\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\0}
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\UrlBreaks}{\UrlAlphabet}
\g@addto@macro{\UrlBreaks}{\UrlDigits}
\makeatother
% Eliminate warnings about non-string commands not shown in PDF bookmarks. If you use more commands in section titles or chapter titles, you can add the commands here.
\pdfstringdefDisableCommands{
    \def \quad{}
    \def \qquad{}
}

% Footnote
% Usage: \footnote{Your Text}
\usepackage[perpage]{footmisc}
% \usepackage{footnpag} % footnote per page
% \usepackage{footnote}
% \let\ftnt=\footnote
% \renewcommand{\footnote}[1]{\ftnt{\footnotefont{#1}}}

% No Number Footnote
% Especially for stating which project the thesis is supported
% Usage: \blfootnote{Your Text}
\usepackage{lipsum}
\newcommand\blfootnote[1]{
  \begingroup
  \renewcommand\thefootnote{}\footnote{#1}
  \addtocounter{footnote}{-1}
  \endgroup 
}

%%%%%% Detail style %%%%%%
% Underline
% Usage: \ul[length]{<text>}
\makeatletter
\newcommand\ul[2][4cm]{\hskip1pt\underline{\hb@xt@ #1{\hss#2\hss}}\hskip3pt}
\makeatother
\def\ULthickness{1.1pt}%

% Content style
\setcounter{secnumdepth}{3} % let subsubsection have number
\setcounter{tocdepth}{3} % let subsubsection appear in CONTENTS
\usepackage{titletoc}
\newif{\ifpagenumber}
\pagenumbertrue
\renewcommand\contentsname{\centerline{\heiti\zihao{3}\textbf{{目\qquad{}录}}}}
\titlecontents{chapter}[0em]{\heiti\zihao{-4}\vspace{0.4mm}} {%
    \ifpagenumber \CTEXnumber{\CJKsection}{\thecontentslabel}{第\CJKsection{章}\quad{}} \fi}{%
    } {%
    \ifpagenumber \hspace{.5em}\titlerule*[6pt]{$\cdot$}\contentspage\fi}
\titlecontents{section}[1em]{\songti\zihao{-4}\vspace{0.4mm}}{%
    \thecontentslabel\quad{}}{}{%
    \ifpagenumber \hspace{.5em}\titlerule*[6pt]{$\cdot$}\contentspage\fi}%
\titlecontents{subsection}[2em]{\songti\zihao{-4}\vspace{0.4mm}}{%
    \thecontentslabel\quad{}}{}{%
    \ifpagenumber \hspace{.5em}\titlerule*[6pt]{$\cdot$}\contentspage\fi}%
\titlecontents{subsubsection}[3em]{\songti\zihao{-4}\vspace{0.4mm}}{%
    \thecontentslabel\quad{}}{}{%
    \ifpagenumber \hspace{.5em}\titlerule*[6pt]{$\cdot$}\contentspage\fi}%
    
% Content Page style
\makeatletter 
\renewcommand\frontmatter{%
    \if@openright\cleardoublepage\else\clearpage\fi%
    \@mainmatterfalse%
    \pagenumbering{Roman}% Roman style page number
    \pagestyle{frontmatter}
    }
\makeatother%

\makeatletter
\renewcommand\mainmatter{%
    \if@openright\cleardoublepage\else\clearpage\fi%
    \@mainmattertrue%
    \pagenumbering{arabic}% Arabic style page number
    \pagestyle{mainmatter}
    }
\makeatother%

\makeatletter % Translation Page style
\renewcommand\backmatter{%
    \if@openright\cleardoublepage\else\clearpage\fi%
    \@mainmatterfalse%
    \pagestyle{empty}
}
\makeatother%

% Text style
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\heiti\zihao{3}\centering\bfseries}{第\chinese{chapter}章}{1em}{}
% 缩短章节标题的上边距
\titlespacing{\chapter}{0pt}{-20pt}{12pt} 
\titleformat{\section}{\heiti\zihao{4}\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\heiti\zihao{-4}\bfseries}{\qquad{}\thesubsection}{1em}{}
\titleformat{\subsubsection}{\heiti\zihao{-4}\bfseries}{\qquad{}\thesubsubsection}{1em}{}

% Reference style
% \usepackage[numbers,sort&compress]{natbib}
\usepackage[super,numbers,sort&compress]{natbib}
%\usepackage[backend=biber,texencoding=utf8,bibencoding=utf8]{biblatex}
\renewcommand\bibname{\centerline{\heiti\zihao{3}\textbf{参考文献}}}
\setcitestyle{square}
\setlength{\bibsep}{0.2em}

% Appendix
\usepackage{appendix}

% 定义新指令 \appendixsection，接受标题和标签名两个参数
\newcommand{\appendixsection}[2]{
  \section*{#1} 
  \phantomsection                
  \hypertarget{#2}{}             
  \addcontentsline{toc}{section}{#1}
}

% Figure & Table
\usepackage{graphicx}
\usepackage{array,booktabs,multirow} % multirow, multicolumn and more professional format support
\usepackage{tabularx} % deal with text wrapping in tables
\usepackage{longtable}
\usepackage{tabularray}
\usepackage{tblr-extras}


\usepackage{caption}
\usepackage[labelformat=simple]{subcaption}

\renewcommand{\captionfont}{\kaishu\zihao{5}}
\renewcommand{\captionlabelfont}{\kaishu\zihao{5}}
\DeclareCaptionLabelSeparator{twospace}{~}
\captionsetup{labelsep=twospace}  %去掉图1：后冒号
\renewcommand{\thesubfigure}{(\alph{subfigure})}
\renewcommand{\tablename}{表}
\renewcommand{\thetable}{~\arabic{chapter}-\arabic{table}~}
\renewcommand{\arraystretch}{1.5}
\renewcommand{\figurename}{图}
\renewcommand{\thefigure}{~\arabic{chapter}-\arabic{figure}~}
\renewcommand{\theequation}{\arabic{chapter}-\arabic{equation}~}
% 外文文献中表、图、公式、代码编号重置
\newcommand{\newtranschapter}{\thispagestyle{empty}\addtocounter{chapter}{1}\setcounter{table}{0}\setcounter{equation}{0}\setcounter{figure}{0}\setcounter{lstlisting}{0}}

% Tabularray 设置

\SetTblrStyle{contfoot-text}{font=\kaishu\zihao{5}}
\SetTblrStyle{caption-tag}{font=\kaishu\zihao{5}}
\SetTblrStyle{caption-text}{font=\kaishu\zihao{5}}
\DeclareTblrTemplate{contfoot-text}{default}{}
\DeclareTblrTemplate{caption-sep}{default}{}
% \DeclareTblrTemplate{conthead-text}{default}{续表}
% https://tex.stackexchange.com/questions/115195/table-captions-continued
\DefTblrTemplate{firsthead}{default}
{
  \UseTblrTemplate{caption}{default}
}
\DefTblrTemplate{middlehead,lasthead}{default}
{
  \UseTblrTemplate{caption}{default}
}

% equation tweaking
\usepackage{amssymb}
\usepackage{bm} % 加粗使用
\usepackage{amsmath,delarray,bm,mathtools}
\newtagform{newtag}[]{式（}{）} %定义公式编号样式
\usetagform{newtag}

% \usepackage{amsthm}
\newtheorem{definition}{定义}[chapter]
\newtheorem{theorem}{定理}[chapter]
\newtheorem{axiom}{公理}[chapter]
\newtheorem{lemma}{引理}[chapter]
\newtheorem{proposition}{命题}[chapter] 
\newtheorem{corollary}{推论}[chapter]

% Algorithm
\usepackage{algorithm}  
\usepackage{algorithmicx}  
\usepackage{algpseudocode}

\renewcommand{\algorithmicrequire}{\textbf{输入}}
\renewcommand{\algorithmicensure}{\textbf{输出}}
\floatname{algorithm}{算法}

% Code 
\usepackage{listings}
\usepackage{xcolor}
\lstset{
    basicstyle=\ttfamily\zihao{5}, %五号等宽字体
    extendedchars=true,
    keywordstyle=\color{blue},
    commentstyle=\color{green!30!black}, 
    showspaces=false,          % 显示空格
    showstringspaces=false,    % 字符串中显示空格
    showtabs=false,            % 显示 TAB
    tabsize=2,                 % TAB 被当作两个空格
    captionpos=t,              % 标题位置
    breaklines=true,           % 自动断行
    breakatwhitespace=true,   
    basewidth={0.5em,0.35em},
    frame=single,              %单线边框，注释掉则无边框
    numbers=left,               %行号位于左侧
    numberbychapter=true,
%    xleftmargin=.5em,
%    xrightmargin=.5em,
%    aboveskip=1ex,
%    columns=flexible,        % 如果不指定等宽字体用这个
%    mathescape,
    escapeinside={\%*}{*)}
}

\renewcommand{\lstlistingname}{\kaishu{代码}}
\makeatletter
\AtBeginDocument{%
  \renewcommand \thelstlisting
       {\ifnum \c@chapter>\z@ \thechapter-\fi \@arabic\c@lstlisting}%
}
\makeatother

% C# 代码样式
\lstdefinelanguage{CSharp}{
    morekeywords={%
        abstract, as, base, bool, break, byte, case, catch, char, checked, class, const, continue, decimal, default, %
        delegate, do, double, else, enum, event, explicit, extern, false, finally, fixed, float, for, foreach, goto, %
        if, implicit, in, int, interface, internal, is, lock, long, namespace, new, null, object, operator, out, %
        override, params, private, protected, public, readonly, ref, return, sbyte, sealed, sizeof, stackalloc, string, %
        struct, switch, this, throw, true, try, typeof, uint, ulong, unchecked, unsafe, ushort, using, virtual, void, volatile, while, %
        async, await, get, set, add, remove, yield, yield break, yield return, %
        dynamic, var, partial, virtual, override, base, typeof, sizeof, %
        delegate, event, nameof, ref, in, out, params, this, base, new, %
        where, from, group, join, let, orderby, select, into, orderby, descending, ascending, %
        get, set, checked, unchecked, lock, fixed, sizeof, nameof, default, nameof, %
        fixed, stackalloc, nameof, await, async, %
        % 可能需要的上下文关键字
        await, async, get, set, add, remove, yield, %
        % 根据需求添加更多关键字
    },
    sensitive=false,      % 区分大小写（建议设为false，除非需要区分）
    morecomment=[l]{//},  % 单行注释
    morecomment=[s]{/*}{*/}, % 多行注释
    morestring=[b]",      % 字符串用双引号包裹
    literate=%            % 处理特殊符号（如显示为直角引号）
        {`}{{\textquotesingle}}1
        {~}{{\textasciitilde}}1,
    showstringspaces=false% 隐藏字符串中的空格
}

\lstdefinestyle{csharp}{
    language=CSharp,          % 使用定义的语言
    stringstyle=\color{red},    % 字符串颜色
    moredelim=[s][\color{magenta}]{<}{>}, % 泛型符号颜色（如<T>）
    moredelim=*[s][\color{magenta}]{[}{]}, % 数组符号颜色（如[]）
}


% insert PDF
\usepackage[final]{pdfpages}

% insert blank page
\makeatletter
\newcommand\blankmatter{%
    \if@openright\cleardoublepage\else\clearpage\fi%
    \@mainmatterfalse%
    \pagestyle{empty}}%
\makeatother%

% List environment config
\usepackage[inline]{enumitem}
\usepackage{pifont}

% Reference: https://zhuanlan.zhihu.com/p/681663494
\newcommand{\circlednumber}[1]{\ding{\numexpr171+#1\relax}}
\setlist[enumerate,1]{label=(\arabic*)}
\setlist[enumerate,2]{label=\circlednumber{\arabic*}}
\setlist[enumerate,3]{label=\Alph*.}
\setlist[enumerate,4]{label=\alph*.}
% Uncomment the two lines below if you want an list environment to share the same indent
% with a paragraph and no extra blank before, in or after the list.
\setlist[itemize]{labelindent=\parindent,leftmargin=*,noitemsep,topsep=0pt}
\setlist[enumerate]{labelindent=\parindent,leftmargin=*,noitemsep,topsep=0pt} 