我有一个几乎 100% 有效的自定义样式,但我注意到有几个错误,而这个我似乎无法弄清楚。
\titleformat{\section}
{\color{cyan}\itshape\LARGE}
{\llap{\thesection\ }#1}
{1em}
{}
[\titleline{\color{cyan}\titlerule[1pt]}]
在我的 cls 文件中,我有许多代码,其中一些可能与此相冲突。当我根据该类创建一个新的空文档book
(并将上述代码附加到序言中)时,一切都正常。但是对于我自己的类,未编号部分的节文本不会显示。
这是我的 cls 文件:
\LoadClass[10pt,letterpaper]{book}
\let\cleardoublepage\clearpage
\RequirePackage[
left=2.25in,
right=0.75in,
top=1.25in,
bottom=1.25in,
marginparwidth=1.75in,
marginparsep=.25in,
asymmetric]{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}
\RequirePackage{environ}
\RequirePackage[strict]{changepage}
\RequirePackage{fancyhdr}
\RequirePackage{calc}
\RequirePackage{titletoc}
\renewcommand*\familydefault{\sfdefault} % Set default font
\newcounter{definition}
\setcounter{definition}{0}
%%% Table of Contents ----------------------------------------------------------
\setcounter{tocdepth}{1} % Show only Chapters and Sections
% this stuff moved here for clarity
\makeatletter
\g@addto@macro{\appendix}{%
\addtocontents{toc}{\protect\renewcommand*{\protect\@chapapp}{\protect\appendixname}}}
%%% Change font/color/layout of TOC
% START OF MODIFIED CODE
\contentsmargin{1.5cm} % global right margin, perhaps also global left
\titlecontents{chapter}[3cm] % <-- seems to set some specific left margin
{\color{cyan}\bfseries\LARGE\addvspace{3mm}}
{\makebox[0cm][r]{\@chapapp\hspace{.5em}\thecontentslabel\hspace{.75cm}}}
{} % ^^^ pretendously zero width box puts its contents in the left margin
{\hfill\makebox[-3cm]{\thecontentspage}} % 3cm = twice 1.5cm
\titlecontents{section}[3cm] % <-- again this left (additional?) margin
{\color{cyan}\large\itshape\addvspace{3mm}}
{\makebox[0cm][r]{\thecontentslabel\hspace{.75cm}}} % box pushed to the left
{}
{\hfill\makebox[-3cm]{\thecontentspage}} % 3cm = twice 1.5cm
[\addvspace{0mm}]
% END OF MODIFICATIONS
\makeatother
\makeatletter
% Basically the same as for `\l@section` etc, just `\@nodottedtocline` instead of `\@dottedtcline`:
\newcommand*\l@chapterinfo{\@nodottedtocline{0}{7em}{2.1em}}
\newcommand*\l@sectioninfo{\@nodottedtocline{1}{7em}{2.1em}}
% Copied from the book class macro `\@dottedtocline`. Removed the dots and page number
\def\@nodottedtocline#1#2#3#4#5{%
\ifnum #1>\c@tocdepth \else
\vskip \z@ \@plus.2\p@
{\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
\parindent #2\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima #3\relax
\advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
{#4}\nobreak
\leaders\hbox{$\m@th
\mkern \@dotsep mu\hbox{\,}\mkern \@dotsep
mu$}\hfill
\nobreak
\hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor }%
\par}%
\fi}
\makeatother
%%% New commands to add custom description to Chapter and Section.
\def\chapterinfo#1{%
\addcontentsline{toc}{chapterinfo}{%
\noexpand\numberline{}#1}}%
\def\sectioninfo#1{%
\addcontentsline{toc}{sectioninfo}{%
\noexpand\numberline{}#1}}%
%-------------------------------------------------------------------------------
%%% Header and Footer ----------------------------------------------------------
\pagestyle{fancy}
\fancyhf{} % sets both header and footer to nothing
\lhead[\textbf{\thepage}\hspace{7mm}\leftmark]{}
\rhead[]{\rightmark\hspace{7mm}\textbf{\thepage}}
\renewcommand{\headrulewidth}{0pt}
\fancyhfoffset[L]{1.5in}
\fancyhfoffset[R]{0in}
%-------------------------------------------------------------------------------
%%% Sction Styles --------------------------------------------------------------
\makeatletter
\titleformat{\chapter}
{\normalfont\sffamily\Huge\scshape}
{}
{0pt}
{
\thispagestyle{empty} % Remove page number on new chapters
\setcounter{definition}{0}
\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-3cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
\draw[fill=cyan] (0,-1) rectangle
(25cm,3cm);
\draw[fill=cyan] (0,-24) rectangle
(25cm,-25cm);
\ifttl@label% <---------------------- Added condition on \ifttl@label
\node[anchor=west,xshift=.21\paperwidth,yshift=-.01\paperheight,rectangle]
{\color{white}\LARGE \MakeUppercase{\@chapapp} \Huge\thechapter};
\fi% <------------------------------- end condition on \ifttl@label
\end{tikzpicture}
};
\end{tikzpicture}\endgraf
\vskip-.7cm
\color{cyan}\Huge\raggedright\leftskip-1cm
\noindent\MakeUppercase{#1}\endgraf
}
\makeatother
\titlespacing*{\chapter}{0pt}{10pt}{0pt}{}
\titleformat{\section}
{\color{cyan}\itshape\LARGE}
{\llap{\thesection\ }#1}
{1em}
{}
[\titleline{\color{cyan}\titlerule[1pt]}]
\titleformat{\subsection}{\color{cyan}\normalfont\large}{\; #1}{1em}{}
%-------------------------------------------------------------------------------
%%% Custom Environments --------------------------------------------------------
\tikzstyle{cyanbox} = [draw=cyan!50, fill=cyan!10, very thick,
rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{cyanbox2} = [draw=cyan!50, fill=cyan!10, very thick,
rectangle, inner sep=10pt, inner ysep=20pt]
\tikzstyle{whitebox} = [draw=cyan!50, fill=white, very thick,
rectangle, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} =[fill=cyan!50, text=white]
\def\thedefinition{\thechapter.\arabic{definition}}
\NewEnviron{definition}[1][Definition]{
\stepcounter{definition}
\begin{center}
\begin{tikzpicture}
\node [cyanbox] (box){%
\begin{minipage}{.9\textwidth}
\BODY
\end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {\bfseries$\bullet$ Definition \thedefinition};
\end{tikzpicture}
\end{center}}
\NewEnviron{emphasis}{
\begin{center}
\begin{tikzpicture}
\node [whitebox] (box){%
\begin{minipage}{.9\textwidth}
\BODY
\end{minipage}
};
\end{tikzpicture}
\end{center}}
\NewEnviron{fullwidth}{
\begin{adjustwidth}{-1.55in}{0in}
\begin{center}
\begin{tikzpicture}
\node [cyanbox2] (box){%
\begin{minipage}{\paperwidth - 1.8in}
\BODY
\end{minipage}
};
\end{tikzpicture}
\end{center}
\end{adjustwidth}}
\def\leftmarginshift{\marginparwidth + \marginparsep}
\def\rightmarginshift{\marginparsep}
\def\bodysize{\textwidth + \rightmarginshift}
\NewEnviron{myimage}[2]{
\begin{adjustwidth}{-2.0in}{0.25in}
asdf
\begin{table}[h]
\centering
% \makebox[0pt][c]{\parbox{\textwidth}{%
\begin{minipage}{2.0in}
#1.#2
\end{minipage}
\begin{minipage}{\textwidth}
\BODY
\end{minipage}
% }}
\end{table}
\end{adjustwidth}
}
\NewEnviron{notation}{
\begin{tikzpicture}
\node [mybox] (box){%
\begin{minipage}{.9\textwidth}
\BODY
\end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {\bfseries Notation};
\end{tikzpicture}}
%-------------------------------------------------------------------------------
%%% Custom Theorems ------------------------------------------------------------
\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]{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}
这是与之配合的 MWE。
\documentclass{textbook}
\titleformat{\section}
{\color{cyan}\itshape\LARGE}
{\llap{\thesection}}
{1em}
{\sectiontitle}
[\titleline{\color{cyan}\titlerule[1pt]}]
\begin{document}
\section*{Acknowledgement}% This section is not numbered
asdf
\chapter{test}
\end{document}
我可以说问题与我的代码中的 #1 有关。我尝试用 替换\sectiontitle
,但也没有用。
答案1
第三个必需参数\titleformat
用于排版数字(当没有使用 * 时);第四个是数字和标题之间的分隔符,第五个用于排版标题。所以你想要的是
\titleformat{\section}
{\color{cyan}\itshape\LARGE}
{\llap{\thesection\ }}
{0pt}
{#1}
[\titleline{\color{cyan}\titlerule[1pt]}]
这是输出
\section*{Abc}
\section{Def}
绝不在类文件中使用\makeatletter
和,它们分别在开始和结束时已经隐含。\makeatother