从 \frontmatter 中删除章节编号

从 \frontmatter 中删除章节编号

此代码是根据以下解决方案构建的:罗尼成立这里

我想知道如何从前页(如目录、图表列表等)和没有关联章节编号的后页(如附录和索引)中删除章节编号“0”。

这是他的代码:

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{lipsum}
\usepackage{xcolor,etoolbox,fancyhdr}
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}
\usepackage{fourier}
\usepackage{titlesec}
\definecolor{lightblue}{RGB}{199,232,250}
\definecolor{darkblue}{RGB}{59,134,215}
\usetikzlibrary{calc}
% Define just chapter start pages

\pagestyle{fancy}
\fancyhf[LE]{Author's Name}\fancyhead[LE,RO]{}

\fancyhead[LO]{%\textcolor{mybluei} \rightmark%
\begin{tikzpicture}[overlay,remember picture]
    % Box
    \draw[fill=lightblue,draw=none] ($(current page.north east) - (1cm,0)$) -- (current page.north east) -- (current page.south east) -- ++ (-1cm,0) -- cycle;
    % Chapter Number
    \node[scale=3,darkblue] at ($(current page.north east) + (-1cm,-4cm)$)  {\thechapter};
    % Chapter
    \node[rotate=90, anchor=east] at ($(current page.north east) + (-.5cm,-4cm)$) {\leftmark{}};
    % Circle for page number
    \draw[fill=darkblue,draw=none] ($(current page.south east) + (-1cm,3cm)$) circle (3mm);
    % Page number
    \node at ($(current page.south east) + (-1cm,3cm)$)  {\textcolor{white}\thepage};
  \end{tikzpicture}}
\fancyhead[RE]{
\begin{tikzpicture}[overlay,remember picture]
    \draw[fill=lightblue,draw=none] ($(current page.north west) + (1cm,0)$) -- (current page.north west) -- (current page.south west) -- ++ (1cm,0) -- cycle;
    \node[rotate=90,anchor=east] at ($(current page.north west) + (.5cm,-4cm)$) {\rightmark};
    \node[scale=3,darkblue] at ($(current page.north west) + (1cm,-4cm)$) {\thechapter};
    \draw[fill=darkblue,draw=none] ($(current page.south west) + (1cm,3cm)$) circle (3mm);
    \node at ($(current page.south west) + (1cm,3cm)$)  {\textcolor{white}\thepage};
  \end{tikzpicture}}


\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}

\begin{document}

%========================================================================================
%   TABLE OF CONTENTS
%========================================================================================
\frontmatter
\tableofcontents

\listoffigures

%\lstlistoflistings

\mainmatter
\chapter{My first Chapter: Problems}

\section{Problem 1}
\subsection{Problem 1}
\lipsum[1]
\subsection{Solution 1}
\lipsum[1-7]
\newpage
\section{Problem 2}
\subsection{Problem 2}
\lipsum[1]
\subsection{Solution 2}
\lipsum[1-4]

\chapter{A second Chapter Title}
\section{Problem 1}
\subsection{Problem 1}
\lipsum[1]
\newpage
\subsection{Solution 1}
\lipsum[1]
\newpage
\section{Problem 2}
\subsection{Problem 2}
\lipsum[1]
\subsection{Solution 2}
\lipsum[1]

\end{document} 

答案1

使用 ,将章节号作为 tikz 节点的标签插入到页边距中\thechapter。由于您只想在文档的“主要内容”部分打印章节号,因此控制此操作的最佳方法是使用\@mainmatter设置为 true 的 if 标志\mainmatter。以下代码将 的两个用法\thechapter(用于左侧和右侧页边距)替换为\TheChapter,其定义为:

\makeatletter% print \the chapter in main matter components of the document
\newcommand\TheChapter{\if@mainmatter\thechapter\fi}
\makeatother

这达到了预期的效果(左边距类似):

在此处输入图片描述 在此处输入图片描述

正如我所使用过的\if@mainmatter,执行完命令后,数字将不再出现在边距中\backmatter,我认为这就是您想要的。

完整代码如下:

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{lipsum}
\usepackage{xcolor,etoolbox,fancyhdr}
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}
\usepackage{fourier}
\usepackage{titlesec}
\definecolor{lightblue}{RGB}{199,232,250}
\definecolor{darkblue}{RGB}{59,134,215}
\usetikzlibrary{calc}
% Define just chapter start pages

\pagestyle{fancy}
\fancyhf[LE]{Author's Name}\fancyhead[LE,RO]{}
\makeatletter% print \the chapter in main matter components of the document
\newcommand\TheChapter{\if@mainmatter\thechapter\fi}
\makeatother
\fancyhead[LO]{%\textcolor{mybluei} \rightmark%
\begin{tikzpicture}[overlay,remember picture]
    % Box
    \draw[fill=lightblue,draw=none] ($(current page.north east) - (1cm,0)$) -- (current page.north east) -- (current page.south east) -- ++ (-1cm,0) -- cycle;
    % Chapter Number
    \node[scale=3,darkblue] at ($(current page.north east) + (-1cm,-4cm)$)  {\TheChapter};% selectively print chapter number
    % Chapter
    \node[rotate=90, anchor=east] at ($(current page.north east) + (-.5cm,-4cm)$) {\leftmark{}};
    % Circle for page number
    \draw[fill=darkblue,draw=none] ($(current page.south east) + (-1cm,3cm)$) circle (3mm);
    % Page number
    \node at ($(current page.south east) + (-1cm,3cm)$)  {\textcolor{white}\thepage};
  \end{tikzpicture}}
\fancyhead[RE]{
\begin{tikzpicture}[overlay,remember picture]
    \draw[fill=lightblue,draw=none] ($(current page.north west) + (1cm,0)$) -- (current page.north west) -- (current page.south west) -- ++ (1cm,0) -- cycle;
    \node[rotate=90,anchor=east] at ($(current page.north west) + (.5cm,-4cm)$) {\rightmark};
    \node[scale=3,darkblue] at ($(current page.north west) + (1cm,-4cm)$) {\TheChapter};% selectively print chapter number
    \draw[fill=darkblue,draw=none] ($(current page.south west) + (1cm,3cm)$) circle (3mm);
    \node at ($(current page.south west) + (1cm,3cm)$)  {\textcolor{white}\thepage};
  \end{tikzpicture}}


\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}

\begin{document}

%========================================================================================
%   TABLE OF CONTENTS
%========================================================================================
\frontmatter
\tableofcontents

\listoffigures

%\lstlistoflistings
\mainmatter
\chapter{My first Chapter: Problems}

\section{Problem 1}
\subsection{Problem 1}
\lipsum[1]
\subsection{Solution 1}
\lipsum[1-7]
\newpage
\section{Problem 2}
\subsection{Problem 2}
\lipsum[1]
\subsection{Solution 2}
\lipsum[1-4]

\chapter{A second Chapter Title}
\section{Problem 1}
\subsection{Problem 1}
\lipsum[1]
\newpage
\subsection{Solution 1}
\lipsum[1]
\newpage
\section{Problem 2}
\subsection{Problem 2}
\lipsum[1]
\subsection{Solution 2}
\lipsum[1]

\end{document}

相关内容