\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{handouts}[2025/08/06 Kirino House document class]

% --------------------
%     文档类
% --------------------

\LoadClass[openright]{ctexbook}

% --------------------
%     导入宏包
% --------------------

% 基础与兼容
\RequirePackage{silence} % 先加载以过滤后续警告
% 按需过滤特定警告
\WarningFilter{latexfont}{Some font shapes were not available}
\WarningFilter{latexfont}{Font shape `TU/方正兰亭中黑_GBK.ttf(1)/bx/n' undefined}
\WarningFilter{latexfont}{Missing character}
\WarningFilter{fancyhdr}{\headheight is too small}
\WarningFilter{microtype}{The `disable' option is in effect}

\RequirePackage{ifthen}
\RequirePackage{xstring}
\RequirePackage{xcolor}
\RequirePackage{graphicx} % 图片支持

% 字体与字符编码
\RequirePackage{fontspec}       % 字体加载（必须 XeLaTeX 编译）
\RequirePackage{xeCJK}          % 中文支持
\RequirePackage{unicode-math}   % 数学字体（替代 amssymb/amsfonts）
\RequirePackage{newunicodechar} % Unicode 字符映射
\RequirePackage{bm}             % 粗体数学符号
\RequirePackage{amsmath}        % 数学环境支持（与 unicode-math 兼容）

% 版面与标题
\RequirePackage{microtype}     % 虽然加载，但立即禁用
\microtypesetup{disable}
\RequirePackage{setspace}       % 控制行间距，放前比较安全
\RequirePackage{titlesec}       % 控制章节标题，需在 fancyhdr 前加载
\RequirePackage{titling}        % 自定义标题页
\RequirePackage{fancyhdr}       % 页眉页脚，必须在 titlesec 后加载
\RequirePackage{ragged2e}       % 对齐宏包，放这里没问题
\RequirePackage{afterpage}      % 延迟执行，位置可灵活
\RequirePackage{emptypage}      % 空白页处理，位置可灵活
\RequirePackage{wallpaper}      % 背景图，一般放后面

% 表格与列表
\RequirePackage{tabularray}
\UseTblrLibrary{amsmath}
\RequirePackage{enumitem}  % 列表控制
\setlist{nolistsep}        % 去掉列表多余间距
\RequirePackage{tasks}     % 多列任务列表
\RequirePackage{multicol}

% 绘图
\RequirePackage{pgf}
\RequirePackage{tikz}
\usetikzlibrary{
    backgrounds, mindmap, positioning, trees, fit,
    shapes, matrix, shadows, shadows.blur, arrows.meta, shapes.geometric,
    decorations.pathreplacing, angles, quotes, calc, patterns, intersections
}
\RequirePackage{pgfplots}
\pgfplotsset{compat=1.18}
\RequirePackage{tikz-3dplot}

% 其他工具
\RequirePackage{ulem}
\normalem   % 保留 \emph 原样式

\RequirePackage{tcolorbox}
\tcbuselibrary{skins, breakable, theorems}

\RequirePackage{paracol}
\RequirePackage{zhlipsum}
\RequirePackage{dashrule}

% --------------------
%     颜色设置
% --------------------

\definecolor{dd4d49}{HTML}{DD4D49}    % 红色
\definecolor{e8a6a3}{HTML}{E8A6A3}    % 浅红色
\definecolor{c9c9c9}{HTML}{C9C9C9}    % 灰色
\definecolor{999388}{HTML}{999388}    % 深灰色
\definecolor{3b5b87}{HTML}{3B5B87}    % 蓝色
\definecolor{dbe3ef}{HTML}{DBE3EF}    % 极浅蓝色
\definecolor{c7d3e7}{HTML}{C7D3E7}    % 浅蓝色
\definecolor{63baaf}{HTML}{63BAAF}    % 填充用青色
\definecolor{f1d87c}{HTML}{F1D87C}    % 填充用黄色
\definecolor{e8745d}{HTML}{E8745D}    % 填充用红色

% --------------------
%     可视化窗格
% --------------------

%\RequirePackage{showframe} 

% --------------------
%     页面设置
% --------------------

\RequirePackage{geometry}

\geometry{
    a4paper,
    left=1.27cm,
    right=1.27cm,
    top=2.0cm,
    headheight=1.2cm,
    headsep=0.4cm,
    bottom=1.5cm,
    footskip=0.6cm, 
    bindingoffset=1cm
}

% 取消缩进
\setlength{\parindent}{0pt}

% 固定导言
\author{}
\date{}

% --------------------
%     字体设置
% --------------------

% 指定字号
\AtBeginDocument{
  \zihao{-5}  % 设置为五号（10.5pt）
}

% 设置字体
\setmathfont{XITS Math}                               % 数学字体
\setCJKfamilyfont{titlefont}{思源黑体}                 % 标题无衬线字体
\setCJKfamilyfont{titlefont2}{手书体}                  % 侧栏手写字体
\setCJKfamilyfont{titlefont4}{方正兰亭大黑_GBK}         % 封面大标题字体
\newfontfamily\titlefontx{思源黑体}                    % 题目标签数字字体

% 中文双引号
\newcommand{\myCJKquote}[1]{\text{\CJKfamily{song}\hspace{0.18em}#1\hspace{0.18em}}}
\newunicodechar{“}{\myCJKquote{“}}
\newunicodechar{”}{\myCJKquote{”}}

% --------------------
%     页眉页脚设置
% --------------------

\fancypagestyle{default}{%
    \fancyhf{} %
    % 偶数页（左页）页眉
    \fancyhead[LE]{%
      \begin{tikzpicture}[overlay, remember picture]
        \fill[color=c9c9c9]
          (0, 5pt) --
          ({0.71*\textwidth - 5pt}, 5pt) --
          ({0.71*\textwidth - 10pt}, -5pt) --
          (0, -5pt) -- cycle;
        \node[anchor=west, font=\sffamily\bfseries\footnotesize] 
          at ({0.71*\textwidth - 10pt + 2mm}, 0) {高\kern1.7pt中\kern1.7pt数\kern1.7pt学\kern1.7pt个\kern1.7pt人\kern1.7pt工\kern1.7pt作\kern1.7pt室};
        \fill[color=3b5b87]
          ({0.89*\textwidth}, 5pt) --
          ({0.92*\textwidth}, 5pt) --
          ({0.92*\textwidth - 5pt}, -5pt) --
          ({0.89*\textwidth - 5pt}, -5pt) -- cycle;
        \fill[color=3b5b87]
          ({0.93*\textwidth}, 5pt) --
          ({0.96*\textwidth}, 5pt) --
          ({0.96*\textwidth - 5pt}, -5pt) --
          ({0.93*\textwidth - 5pt}, -5pt) -- cycle;
        \fill[color=3b5b87]
          ({0.97*\textwidth}, 5pt) --
          ({1*\textwidth}, 5pt) --
          ({1*\textwidth - 5pt}, -5pt) --
          ({0.97*\textwidth - 5pt}, -5pt) -- cycle;
        \node[anchor=west] at (-1.5mm,3.0mm) {\includegraphics[height=1cm]{./image/logo.png}};
      \end{tikzpicture}%
    }
    % 奇数页（右页）页眉
    \fancyhead[RO]{%
      \begin{tikzpicture}[overlay, remember picture]
        \fill[color=c9c9c9]
          (0, 5pt) --
          ({-0.69*\textwidth}, 5pt) --
          ({-0.69*\textwidth - 5pt}, -5pt) --
          (0, -5pt) -- cycle;
        \node[anchor=east, font=\sffamily\bfseries\footnotesize] 
          at ({-0.69*\textwidth - 5pt - 0.5mm}, 0) {高\kern1.7pt中\kern1.7pt数\kern1.7pt学\kern1.7pt个\kern1.7pt人\kern1.7pt工\kern1.7pt作\kern1.7pt室};
        \fill[color=3b5b87]
          ({-0.89*\textwidth + 5pt}, 5pt) --
          ({-0.92*\textwidth + 5pt}, 5pt) --
          ({-0.92*\textwidth}, -5pt) --
          ({-0.89*\textwidth}, -5pt) -- cycle;
        \fill[color=3b5b87]
          ({-0.93*\textwidth + 5pt}, 5pt) --
          ({-0.96*\textwidth + 5pt}, 5pt) --
          ({-0.96*\textwidth}, -5pt) --
          ({-0.93*\textwidth}, -5pt) -- cycle;
        \fill[color=3b5b87]
          ({-0.97*\textwidth + 5pt}, 5pt) --
          ({-1*\textwidth + 5pt}, 5pt) --
          ({-1*\textwidth}, -5pt) --
          ({-0.97*\textwidth}, -5pt) -- cycle;
        \node[anchor=east] at (1.5mm,3.0mm) {\includegraphics[height=1cm]{./image/logo.png}};
      \end{tikzpicture}%
    }
    \renewcommand{\headrulewidth}{0pt}
    % 偶数页左下角页码
    \fancyfoot[LE]{%
      \begin{tikzpicture}[overlay, remember picture]
        \fill[color=3b5b87]
          (0,0) rectangle ({0.01*\textwidth}, 10pt);
        \fill[color=c9c9c9]
          ({0.04*\textwidth},0) rectangle ({0.08*\textwidth}, 10pt);
        \node[anchor=base, font=\sffamily\bfseries\footnotesize, text=white] at ({0.025*\textwidth}, 2pt)
          {\textcolor{black}{\ifnum\value{page}<10 0\thepage\else\thepage\fi}};
      \end{tikzpicture}%
    }
    % 奇数页右下角页码
    \fancyfoot[RO]{%
      \begin{tikzpicture}[overlay, remember picture]
        \fill[color=3b5b87]
          (0,0) rectangle ({-0.01*\textwidth}, 10pt);
        \fill[color=c9c9c9]
          ({-0.04*\textwidth},0) rectangle ({-0.08*\textwidth}, 10pt);
        \node[anchor=base, font=\sffamily\bfseries\footnotesize, text=white] at ({-0.025*\textwidth}, 2pt)
          {\textcolor{black}{\ifnum\value{page}<10 0\thepage\else\thepage\fi}};
      \end{tikzpicture}%
    }
    \renewcommand{\footrulewidth}{0pt}
}

% --------------------
%     标题页样式
% --------------------

% 变量设置
\newcommand{\subtitle}[1]{\gdef\@subtitle{#1}}
\newcommand{\order}[1]{\gdef\@order{#1}}
\newcommand{\textbook}[1]{\gdef\@textbook{#1}}
\newcommand{\category}[1]{\gdef\@category{#1}}

% 自定义样式
\renewcommand{\maketitle}{%
  \begin{titlepage}
    \thispagestyle{empty}
    % 背景图片（示范，保持原逻辑）
    \IfSubStr{\@category}{基础}
      {\ThisCenterWallPaper{1}{./image/cover.png}}
      {\ThisCenterWallPaper{1}{./image/cover2.png}}

    % 用 tikzpicture 画布固定大小，原点为左下角
    \begin{tikzpicture}[remember picture,overlay]
      % 定义封面尺寸
      \useasboundingbox (0,0) rectangle (21cm,29.7cm);

      % 教材标题
      \node[anchor=center,font=\CJKfamily{titlefont}\fontsize{24}{36}\selectfont,
            text=white,
            yscale=0.7
          ] 
          at (5.85cm,-7.31cm) {%
            \renewcommand{\CJKglue}{\hskip 0.2em}%
            \@textbook%
          };

      % 讲义类别
      \node[anchor=center,font=\CJKfamily{titlefont}\fontsize{24}{36}\selectfont,
            text=black,
            yscale=0.7
          ] 
        at (12.2cm,-7.31cm) {%
            \renewcommand{\CJKglue}{\hskip 0.2em}%
            \@category%
          };

      % 章节标题
      \node[anchor=center,font=\bfseries\CJKfamily{titlefont}\fontsize{24}{28.8}\selectfont,
            text=black]
        at ($(current page.center) + (0.5cm, 3.8cm)$) {%
            \renewcommand{\CJKglue}{\hskip 0.2em}%
            \@title%
          };

      % 课节标题（右对齐）
      \node[anchor=east,font=\bfseries\CJKfamily{titlefont}\fontsize{30}{36}\selectfont,
            text=black]
        at (13.5cm,-18.5cm) {%
            \renewcommand{\CJKglue}{\hskip 0.1em}%
            \@subtitle%
          };

      % 课节编号
      \node[anchor=center,font=\bfseries\titlefontx\fontsize{44}{66}\selectfont,
            text=white]
        at (14.65cm,-18.5cm) {\@order};
    \end{tikzpicture}

  \end{titlepage}

  \ClearWallPaper

  \newpage
  \thispagestyle{empty}
  \mbox{}
}

% --------------------
%     各级标题样式
% --------------------

% 定义 chapter 样式
\newcommand{\myroundedbox}[1]{%
  \begin{tikzpicture}[baseline=(mbox.base), overlay, remember picture]
    \StrLeft{#1}{1}[\firstchar]
    \StrGobbleLeft{#1}{1}[\restchars]
    \draw[
      e8a6a3,
      line width=2pt
    ] (0,0) -- (\textwidth,0);
    \node[
      anchor=east,
      draw=dd4d49,
      fill=dd4d49,
      inner sep=0.15cm
    ] at (\textwidth,0) {};
    \node[
      anchor=west,
      draw=dd4d49,
      line width=2pt,
      fill=white,
      inner xsep=3pt,
      inner ysep=3pt,
      font=\CJKfamily{titlefont}\bfseries
    ] (mbox) {%
        \firstchar
        \hskip 0.3em
        \restchars
    };
    \node[
      draw=dd4d49,
      line width=2pt,
      fill=dd4d49,
      text=white,
      inner xsep=2pt,
      inner ysep=3pt,
      anchor=west,
      font=\CJKfamily{titlefont}\bfseries
    ] at (mbox.west) {\firstchar};
  \end{tikzpicture}%
}
\titleformat{\chapter}[display]
  {%
    \sffamily%
    \fontsize{15}{18}\selectfont%
    \renewcommand{\CJKglue}{\hskip 0.15em}%
    \thispagestyle{default}%
  }
  {}
  {-1.1em}
  {\myroundedbox}
\titlespacing{\chapter}{0pt}{-1.5\baselineskip}{0\baselineskip}

% 定义 section 样式
\newcommand{\myroundedboxx}[1]{%

    \StrLeft{#1}{3}[\firstThree] % 取前三个字符
    \StrChar{#1}{4}[\fourthChar] % 取第四个字符
    \StrGobbleLeft{#1}{4}[\restChars] % 取第5个及以后字符

    \begin{tikzpicture}[baseline=(mbox.base)]
      \node[
        draw=c9c9c9,
        line width=2pt,
        fill=c9c9c9,
        inner xsep=3pt,
        inner ysep=3pt,
        rounded corners=2pt,
        font=\CJKfamily{titlefont}\large\bfseries
      ] (mbox) {%
          \firstThree
          \hskip 0.5em % 第四字符前的额外间距
          \fourthChar
          \hskip 0.5em % 第四字符后的额外间距
          \restChars
      };

      % 高亮第四个字符节点
      \node[
        draw=dd4d49,
        line width=2pt,
        fill=dd4d49,
        text=white,
        inner xsep=2.3pt,
        inner ysep=4pt,
        anchor=west,
        rounded corners=0.5pt,
        xshift=43.2pt,
        yshift=3pt,
        drop shadow={shadow xshift=-3pt, shadow yshift=-2.5pt, opacity=0.25, fill=black},
        font=\titlefontx\large\bfseries
      ] at (mbox.west) {\fourthChar};
    \end{tikzpicture}%
}
\titleformat{\section}[display]
  {%
    \sffamily%
    \huge\fontsize{12}{15}\selectfont%
  }
  {}
  {-1.1em}
  {\myroundedboxx}

% section 自动奇数页
\newcommand{\mysection}[1][]{%
  \ifthenelse{\equal{#1}{1}}{%
    \clearpage
    \ifodd\value{page}
      % do nothing
    \else
      \thispagestyle{empty}
      \null
      \clearpage
    \fi
  }{}%
}

% 定义 subsection 样式
\renewcommand{\thesubsection}{\arabic{subsection}}
\titleformat{\subsection}
{\normalfont\large\bfseries}
{\thesubsection}
{0.5em}
{}

% --------------------
%     表格样式
% --------------------

% 纵向表格
\NewDocumentCommand{\mytblrs}{m m +m}{%

  \begin{tblr}{
    width = 0.73\textwidth,
    rows={#1},
    colspec = {#2},
    row{1} = {bg=c7d3e7,font=\CJKfamily{titlefont}\bfseries\selectfont},
    hlines = {gray},
    hline{1,Z} = {2pt,black},
    hline{2} = {1pt,black},
    vline{2-Y} = {gray}
  }
    #3
  \end{tblr}
}

% 横向表格
\NewDocumentCommand{\mytblrh}{m m +m}{%

  \begin{tblr}{
    width = 0.73\textwidth,
    rows={#1},
    colspec = {#2},
    column{1} = {bg=c7d3e7,font=\CJKfamily{titlefont}\bfseries\selectfont},
    hlines = {gray},
    hline{1,Z} = {2pt,black},
    vline{2-Y} = {gray},
    vline{2} = {1pt,black}
  }
    #3
  \end{tblr}
}

% --------------------
%     自动样式
% --------------------

% 多栏环境拉伸行距开关
\raggedcolumns

% 定义分栏样式(二八分栏)
\columnratio{0.75,0.25}
\setlength{\columnseprule}{1pt}
\colseprulecolor{c7d3e7}

% 定义分栏样式(五五分栏)
\setlength{\columnseprule}{1pt}
\renewcommand{\columnseprulecolor}{\color{c7d3e7}}

% 课本对标表格样式
\newcommand{\materials}[2]{%
  \vspace{-0.6\baselineskip}
  \begin{center}
    \begin{tblr}{
      width = 1\textwidth,
      rows={rowsep=0.9ex},
      colspec = {X[1,r,m] X[1,l,m]|X[1,r,m] X[2,l,m]},
      row{1} = {bg=c7d3e7,font=\CJKfamily{titlefont}\bfseries\selectfont},
      hline{1,Z} = {2pt,black},
      hline{2} = {1pt,black},
      vline{3} = {gray}
    }
      \SetCell[c=4]{c,m}{#1} & & & \\ % 第一行合并4列
      #2
    \end{tblr}
  \end{center}
}

% 命题方向导图样式
\tikzset{
    mymatrix/.style={
        matrix of nodes,
        row sep=5mm,
        nodes={
            font=\sffamily\bfseries,
            draw, thick, rounded corners=8pt,
            minimum height=1cm, minimum width=3.5cm,
            align=center, anchor=center,
            inner xsep=12pt, inner ysep=8pt,
            top color=dbe3ef, bottom color=c7d3e7,
            blur shadow={shadow blur steps=3}
        },
        row 2/.style={nodes={xshift=10mm}}, % 偶数行缩进
        row 4/.style={nodes={xshift=10mm}}
    }
}

% 公式速记表格样式
\newcommand{\formula}[1]{%
  \begin{center}
    \begin{tblr}{
      width = 1\textwidth,
      rows={rowsep=1ex},
      colspec = {X[2,c,m]|X[3,c,m]},
      row{1} = {bg=c7d3e7,font=\CJKfamily{titlefont}\bfseries\selectfont},
      hline{1,Z} = {2pt,black},
      hline{2} = {1pt,black},
      vline{2} = {gray}
    }
      公式名称 & 公式内容 \\
      #1
    \end{tblr}
  \end{center}
}

% 定义习题演练样式
\newenvironment{exer}[1][2] % 可选参数为栏数，默认两栏
{
    \begin{multicols}{#1}
    \setlength{\columnsep}{1cm}   % 两栏间距
    \begin{spacing}{1.5}          % 行距
    \zihao{-5}                    % 字号
}
{
    \end{spacing}
    \end{multicols}
}

% 定义综合练习样式
\newenvironment{exerciseenum}[1][1]{%
  \begin{enumerate}[%              
      labelindent=0mm,
      align=right,
      labelwidth=3em,
      itemsep=2em,
      labelsep=0.1em,
      leftmargin=4em,
      label={\priceboxlabel{习题}{\arabic*}},
      before=\vspace{1em}%
  ]%
  \setcounter{enumi}{#1-1}% 这里设置起始编号
  \zihao{-5}%
}{%
  \end{enumerate}
}

% 定义选择题样式
\settasks{
    label=\Alph*.,
    label-offset = 0.4em,
    label-align = right,
    column-sep = 13pt,
    label-width = 3ex,
    item-indent = 20pt,
    before-skip = 0.5em,
    after-skip = 0em
}

% --------------------
%     手动样式
% --------------------

% 左右栏切换
\newenvironment{switch}{%
  \switchcolumn*%
  \CJKfamily{titlefont2}\selectfont
}{%
  \switchcolumn%
}

% 水印标注
\newcommand{\highlightbox}[1]{%
  \tikz[remember picture, overlay]{%
    \renewcommand{\CJKglue}{\hskip 0.15em}%
    \node[
      anchor=base west, rotate=-15, text=e8a6a3, draw=e8a6a3, thick,
      font=\CJKfamily{titlefont}\selectfont,
      inner xsep=4pt, inner ysep=2pt
    ] at ($(5.5cm,0) + (1.2cm, 7.5ex)$) {#1};
  }%
  \vspace{-0.6\baselineskip}%
}

% 定义书摘样式
\newtcbtheorem[no counter]{myexcerpt}{文本摘抄}{%
  enhanced,
  width=0.73\textwidth,
  colback = white,
  colframe = 3b5b87,
  colbacktitle = 3b5b87,
  sharp corners,
  attach boxed title to top left = {yshift = -5.55mm},
  boxed title style = {
    enhanced,
    sharp corners,
    boxrule=0pt,
    colback=3b5b87,
    overlay={
      \path[fill=3b5b87,draw=none]
        (frame.south west) -- (frame.south east) -- ([xshift=0.4em,yshift=-0.9em]frame.north east) -- ([xshift=33.25em,yshift=-0.9em]frame.north east) -- ([xshift=33.25em]frame.north east) -- (frame.north east) -- cycle;
    }
  },
  fonttitle = {%
    \CJKfamily{titlefont}\color{white}%
    \renewcommand{\CJKglue}{\hskip 0.3em}%
  },
  before skip = 0ex, 
  after skip = 1ex,
  top=2em,
}{}

% 定义例题探究样式
\NewDocumentCommand{\examplebox}{+m}{%

  \vspace{0.5\baselineskip}

  \begin{tcolorbox}[
      width=0.73\textwidth,
      title={\hspace*{-3.5mm}典\kern2pt型\kern2pt例\kern2pt题\hspace{104.5mm}
          \tikz[scale=0.1,baseline=-0.5ex]{          
            % 顺时针旋转45度
            \begin{scope}[rotate=45]
              \filldraw[white] (0,0) -- (0.2,0) -- (0.3,2) -- (0,2.5) -- (-0.3,2) -- (-0.2,0) -- cycle;
              \filldraw[white] (-0.15,-0.3) rectangle (0.15,-1.1);
              \filldraw[white, rounded corners=0.3pt] (-0.6,-0.2) rectangle (0.6,-0.4);
            \end{scope}
            % 逆时针旋转45度
            \begin{scope}[rotate=-45, shift={(-1,-1)}]
              \filldraw[white] (0,0) -- (0.2,0) -- (0.3,2) -- (0,2.5) -- (-0.3,2) -- (-0.2,0) -- cycle;
              \filldraw[white] (-0.15,-0.3) rectangle (0.15,-1.1);
              \filldraw[white, rounded corners=0.3pt] (-0.6,-0.2) rectangle (0.6,-0.4);
            \end{scope}
          }
      },
      fonttitle=\bfseries,
      colback=white,
      colframe=dd4d49,
      coltitle=white,
      before upper=\setstretch{1.5},
      sharp corners
  ]
  \zihao{-5}
  #1
  \end{tcolorbox}
}

% 例题虚线
\newcommand{\dashedline}{%
  \vspace{0.5\baselineskip}%
  \noindent
  \begin{tikzpicture}[overlay, remember picture]
    \draw[dashed, thick, color=dd4d49] 
      ({-0.03*\linewidth},0) -- ({1.03*\linewidth},0);
  \end{tikzpicture}%
  \vspace{0.25\baselineskip}%
}

% 定义例题标签样式
\newcommand{\customlabelr}[1]{%
  \raisebox{0.42\height}{%
    \begin{tikzpicture}[baseline=(current bounding box.center)]
      \StrLeft{#1}{1}[\firstchar]
      \node[
        fill=c9c9c9,
        rounded corners=2pt,
        inner xsep=3pt,
        inner ysep=3pt,
        text depth=0.25ex,
      ] (first) {\textbf{#1}};
      \node[
        fill=dd4d49,
        text=white,
        rounded corners=2pt,
        inner xsep=2pt,
        inner ysep=3pt,
        anchor=west,
        xshift=0pt,
      ] at (first.west) {\textbf{\firstchar}};
    \end{tikzpicture}%
    \hspace{0.1em}
  }%
}

% 定义例题（习题）标签样式
\newcommand{\customlabel}[1]{%
  \raisebox{0.42\height}{%
    \begin{tikzpicture}[baseline=(current bounding box.center)]
      \StrLeft{#1}{1}[\firstchar]
      \node[
        fill=c9c9c9,
        rounded corners=2pt,
        inner xsep=3pt,
        inner ysep=3pt,
        text depth=0.25ex,
      ] (first) {\textbf{#1}};
      \node[
        fill=3b5b87,
        text=white,
        rounded corners=2pt,
        inner xsep=2pt,
        inner ysep=3pt,
        anchor=west,
        xshift=0pt,
      ] at (first.west) {\textbf{\firstchar}};
    \end{tikzpicture}%
    \hspace{0.1em}
  }%
}

% 自定义习题标签样式
\newsavebox{\mypricebox}
\newcommand{\priceboxlabel}[2]{%
  % 保存整个组合到盒子
  \sbox{\mypricebox}{%
    % 左侧浅色圆角矩形
    \begin{tcolorbox}[
      enhanced,
      colback=c9c9c9,
      boxrule=0pt,
      arc=0.8mm,
      width=11mm,
      height=3.8mm,
      valign=top,
      left=0.5mm,
      top=0.4mm,
      boxsep=0pt
    ]
      \CJKfamily{titlefont} #1
    \end{tcolorbox}
    \hspace{-7.8mm}
    % 中间深色重叠块
    \begin{tikzpicture}[baseline={(0,0)}]
      \fill[3b5b87] (8.7mm,0) -- (11mm,0) -- (11mm,3.2mm) -- (10.3mm,3.2mm) -- cycle;
    \end{tikzpicture}%
    \hspace{-1.8mm} % 调整右侧矩形位置
    % 右侧深色小矩形（汉字）
    \begin{tcolorbox}[
      enhanced,
      colback=3b5b87,
      coltext=white,
      boxrule=0pt,
      width=4.0mm,
      height=3.8mm,
      boxsep=0pt,
      left=0mm,
      right=0.2mm,
      top=0mm,
      bottom=0.5mm,
      arc=0.8mm,
      halign=right,
      valign=bottom
    ]
      \textbf{\titlefontx #2}
    \end{tcolorbox}}
    \raisebox{-0.7ex}{\usebox{\mypricebox}}%
    \hspace{0.1em}
}

% 直接把普通数字转换为罗马数字（大写）
\newcommand{\Roma}[1]{%
  \ifcase#1\or I\or II\or III\or IV\or V\or VI\or VII\or VIII\or IX\or X%
  \or XI\or XII\or XIII\or XIV\or XV\or XVI\or XVII\or XVIII\or XIX\or XX%
  \else ??\fi
}

% 直接把普通数字转换为小写罗马数字
\newcommand{\roma}[1]{\MakeLowercase{\Roma{#1}}}

% 带圈编号
\newcommand{\circlednum}[1]{%
  \tikz[baseline=(char.base)]{
    \node[shape=circle, draw, inner sep=1pt, line width=0.5pt, font=\fontsize{10pt}{11pt}\selectfont] (char) {#1};
  }%
  \hspace{0.2em}
}

% 定义逆向裁剪命令
\makeatletter
\pgfutil@tempcnta=0
\def\pgf@insert@before@path{\pgfutil@tempcnta=0}
\tikzset{
  reverseclip/.style={insert path={(current bounding box.south west)rectangle(current bounding box.north east)}}
}
\makeatother

% 定义习题留白
\NewDocumentCommand{\noteblock}{O{short}}{%
  \IfEqCase{#1}{%
    {short}{\vspace*{5\baselineskip plus 0.5\baselineskip minus 0.5\baselineskip}}%
    {long}{\vspace*{8\baselineskip plus 1.5\baselineskip minus 1\baselineskip}}%
  }[\vspace*{5\baselineskip plus 1\baselineskip minus 0.5\baselineskip}]%
}
