\titleformat 在类文件中使用时似乎不起作用

\titleformat 在类文件中使用时似乎不起作用

当我在自定义类中定义以下代码块时,它似乎没有执行任何操作。但是当我使用该类但将此块放在序言中时,它按预期工作。

\titleformat{\chapter}
  {\normalfont\sffamily\Huge\scshape}
  {}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=cyan] (0,-1) rectangle
          (\paperwidth,3cm);
        \node[anchor=west,xshift=.21\paperwidth,yshift=-.01\paperheight,rectangle]
              {\color{white}\LARGE CHAPTER \Huge\thechapter};
        \node[anchor=west,xshift=.21\paperwidth,yshift=-.065\paperheight,rectangle]
              {\color{cyan}\Huge\MakeUppercase{#1}};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }

这是我的课程文件:

\LoadClass[10pt,letterpaper,oneside]{book}
\let\cleardoublepage\clearpage

\RequirePackage[left=2.25in,right=0.75in,top=1.5in,bottom=1.5in]{geometry}
\RequirePackage{graphicx}
\RequirePackage[hidelinks]{hyperref}
\RequirePackage{amsthm, amsfonts, amsmath}
\RequirePackage{thmtools}
\RequirePackage{paralist}
\RequirePackage{tikz}
\RequirePackage[explicit]{titlesec}
\RequirePackage{blindtext, xcolor}
\RequirePackage[T1]{fontenc}
\RequirePackage{mdframed}
\RequirePackage{DejaVuSansCondensed}
\RequirePackage[normalem]{ulem}

\titleformat{\chapter}
  {\normalfont\sffamily\Huge\scshape}
  {}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=cyan] (0,-1) rectangle
          (\paperwidth,3cm);
        \node[anchor=west,xshift=.21\paperwidth,yshift=-.01\paperheight,rectangle]
              {\color{white}\LARGE CHAPTER \Huge\thechapter};
        \node[anchor=west,xshift=.21\paperwidth,yshift=-.065\paperheight,rectangle]
              {\color{cyan}\Huge\MakeUppercase{#1}};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }
\titlespacing*{\chapter}{0pt}{50pt}{0pt}{}

\titleformat{\section}{\color{cyan}\itshape\LARGE}{\llap{\thesection} #1}{1em}{}
\titleformat{\subsection}{\color{cyan}\normalfont\Large}{\; #1}{1em}{}

\renewcommand*\familydefault{\sfdefault}

% Custom Theorem - Example
\declaretheoremstyle[%
  within=section,%
  spaceabove=5mm,%
  spacebelow=0mm%,
  headfont=\bfseries,%
  headpunct={},%
  postheadspace=\newline,%
  notefont=\normalfont\itshape,%
  notebraces={--~}{},% punctuation before and after the note
]{examplestyle}
\declaretheorem[style=examplestyle,name=Example]{myex}

% Alter spacing after chapter title
\titleformat{\chapter}[display]
    {\normalfont\huge\bfseries}{\chaptertitlename~\thechapter}{20pt}{\Huge}
\titlespacing*{\chapter}{0pt}{50pt}{40pt}
\titlespacing*{name=\chapter,numberless}{0pt}{50pt}{40pt}

\declaretheoremstyle[
    within=section,
    spaceabove=10mm,
    spacebelow=3mm,
    headfont=\color{cyan}\normalfont,
    notefont=\normalfont,
    bodyfont=\itshape,
    headpunct=\newline\newline,
    %postheadspace={10pt},
    notebraces={}{},
    headformat={%
        \makebox[-15pt][r]{\normalfont\large\NAME\ \NUMBER\;\;\;\;}\setbox0\hbox{\ }\hspace{-\the\wd0}\large\NOTE%
    },
]{theorem}
\declaretheorem[style=theorem]{theorem}
\declaretheorem[style=theorem]{definition}
\declaretheorem[style=theorem]{example}

\declaretheoremstyle[
    within=section,
    spaceabove=10mm,
    spacebelow=3mm,
    headfont=\color{cyan}\normalfont,
    notefont=\normalfont,
    bodyfont=\itshape,
    headpunct=\newline\newline,
    %postheadspace={10pt},
    notebraces={}{},
    headformat={%
        \makebox[-10pt][r]{}\setbox0\hbox{\ }\hspace{-\the\wd0}\large\NAME%
    },
]{proof}
\let\proof\relax
\declaretheorem[style=proof,qed=\qedsymbol,name=Proof]{proof}
\declaretheorem[style=proof,qed=\qedsymbol,name=Solution]{solution}

答案1

您已定义titleformat两次chapter。当您发出titleformat命令时,它会覆盖所有以前的格式,以支持新的格式。(具体来说,我正在查看第 55 行)。

18 号线

\titleformat{\chapter}
  {\normalfont\sffamily\Huge\scshape}
  {}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=cyan] (0,-1) rectangle
          (\paperwidth,3cm);
        \node[anchor=west,xshift=.21\paperwidth,yshift=-.01\paperheight,rectangle]
              {\color{white}\LARGE CHAPTER \Huge\thechapter};
        \node[anchor=west,xshift=.21\paperwidth,yshift=-.065\paperheight,rectangle]
              {\color{cyan}\Huge\MakeUppercase{#1}};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }
\titlespacing*{\chapter}{0pt}{50pt}{0pt}{}

然后,在第 55 行

\titleformat{\chapter}[display]
   {\normalfont\huge\bfseries}{\chaptertitlename~\thechapter}{20pt}{\Huge}
\titlespacing*{\chapter}{0pt}{50pt}{40pt}
\titlespacing*{name=\chapter,numberless}{0pt}{50pt}{40pt}

相关内容