为什么 \titleclass 会从目录中删除我的章节?

为什么 \titleclass 会从目录中删除我的章节?

我正在尝试定义一个\preface与以下命令相同的命令,\chapter只是

  • 它不应该增加\thechapter计数器。
  • 在目录中,“前言”应放在“第 N 章”通常所在的位置。
  • 在页脚中,我想要“序言 - {章节标题}”而不是“第 N 章 - {章节标题}”

既然我反正都在使用 titlesec,我想我会先尝试一下 titleclass。然而,当我添加前言 titleclass 时,我的章节级条目就从目录中消失了!

经过几个小时的调试,我没有任何进展。知道这是怎么回事吗?

以下是 MWE:

\documentclass[oneside]{book}

\usepackage{blindtext}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{hyperref}
\usepackage{xcolor}

% use arabic numbering throughout
\makeatletter
\renewcommand{\frontmatter}{\cleardoublepage \@mainmatterfalse}
\renewcommand{\mainmatter}{\cleardoublepage \@mainmattertrue}
\makeatother

%%%% begin preface defs %%%%

% see http://tex.stackexchange.com/a/17278/2041 and the titlesec manual
\titleclass{\preface}{top}[\part]

\newcounter{preface} % required when defining a new titleclass; accessible via \thepreface

\titleformat{\preface}
    {\huge\bfseries}{}{0em}{}

% TODO verify that these dimensions make sense
\titlespacing*{\preface}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

% see autoref in hyperref manual
\newcommand{\prefaceautorefname}{preface}

%%%% end preface defs %%%%

%\setcounter{secnumdepth}{2} % I'm not sure if this is appropriate

\setcounter{tocdepth}{1} % only go down to the section level in TOC

%%%% begin TOC formatting %%%%

\titlecontents{chapter}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Chapter~\thecontentslabel}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{preface}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Preface}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{section}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \large
               \llap{\makebox[1.5in]{%
                   \hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

%%%% end TOC formatting %%%%


\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\preface{Will This Work?}
%\chapter{Perhaps This Will Work}

\Blindtext[2]

\section{Under The Preface}

\Blindtext[3]

\subsection{To The Subway}

\Blindtext[2]

\subsection{Then To A Submarine}

\Blindtext[2]

\chapter{My First Chapter}

\blindtext[1]

\section{First Section Under A Chapter}

\Blindtext[2]

\section{Secondi}

\Blindtext[2]

\section{Triad}

\Blindtext[3]

\chapter{How About Another Chapter}

\section{Woot}

\Blindtext[2]

\end{document}

答案1

我一直觉得这\titleclass很神秘。

您可以用不同的方法解决这个问题,通过在前言中设置序言(但这只是为了逻辑放置)\chapter*,手动添加所需的位。

\documentclass[oneside]{book}

\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{hyperref}

% use arabic numbering throughout
\makeatletter
\renewcommand{\frontmatter}{\cleardoublepage \@mainmatterfalse}
\renewcommand{\mainmatter}{\cleardoublepage \@mainmattertrue}
\makeatother

%%%% begin preface defs %%%%
\makeatletter
\newcommand{\preface}[1]{%
  \chapter*{#1}%
  \addcontentsline{toc}{chapter}{\protect\numberline{\protect\preface}#1}%
  \chaptermark{#1}%
%  \vspace*{-6ex}% add if really wanted
}
\makeatother
\newcommand{\chapterorpreface}{%
  \expandafter\ifx\expandafter\preface\thecontentslabel
    Preface
  \else
    Chapter~\thecontentslabel
  \fi
}
%%%% end preface defs %%%%

\setcounter{tocdepth}{1} % only go down to the section level in TOC

%%%% begin TOC formatting %%%%

\titlecontents{chapter}
  [3pt]
  {\addvspace{1pc}}%
  {\contentsmargin{0pt}%
   \bfseries\large
   \makebox[0pt][r]{\makebox[1.5in]{%
     \textcolor{gray}{\chapterorpreface}\hfill\textbf{\thecontentspage}\hspace{8pt}%
   }}%
  }
  {\contentsmargin{0pt}\large}
  {}
  [\addvspace{.5pc}]

\titlecontents{section}
  [3pt]
  {\addvspace{1pc}}%
  {\contentsmargin{0pt}%
   \large
   \makebox[0pt][r]{\makebox[1.5in]{%
     \hfill\textbf{\thecontentspage}\hspace{8pt}%
   }}%
  }
  {\contentsmargin{0pt}\large}
  {}
  [\addvspace{.5pc}]

%%%% end TOC formatting %%%%

\begin{document}

\frontmatter

\tableofcontents

\preface{Will This Work?}

\Blindtext[2]

\section{Under The Preface}

\Blindtext[3]

\subsection{To The Subway}

\Blindtext[2]

\subsection{Then To A Submarine}

\Blindtext[2]

\mainmatter

\chapter{My First Chapter}

\blindtext[1]

\section{First Section Under A Chapter}

\Blindtext[2]

\section{Secondi}

\Blindtext[2]

\section{Triad}

\Blindtext[3]

\chapter{How About Another Chapter}

\section{Woot}

\Blindtext[2]

\end{document}

\chapterorpreface宏检查当前\thecontentslabel,如果是\preface,它就打印“前言”。

在此处输入图片描述


一个解决方案\titleclass,其中\preface与处于同一级别\chapter,即0。我还添加了对书签和标题的支持。

\documentclass[oneside]{book}

\usepackage{blindtext}

\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{hyperref}
\usepackage{bookmark} % better using this

% use arabic numbering throughout
\makeatletter
\renewcommand{\frontmatter}{\cleardoublepage \@mainmatterfalse}
\renewcommand{\mainmatter}{\cleardoublepage \@mainmattertrue}
\makeatother

%%%% begin preface defs %%%%

% see http://tex.stackexchange.com/a/17278/2041 and the titlesec manual
\titleclass{\preface}[0]{top}%[\part]

\newcounter{preface} % required when defining a new titleclass; accessible via \thepreface

\titleformat{\preface}{\huge\bfseries}{}{0em}{}

% TODO verify that these dimensions make sense
\titlespacing*{\preface}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

% see autoref in hyperref manual
\newcommand{\prefaceautorefname}{preface}
\makeatletter
\def\toclevel@preface{0}
\makeatother
\renewcommand{\prefacemark}[1]{%
  \markboth{\MakeUppercase{Preface}}{\MakeUppercase{Preface}}% or whatever
}
%%%% end preface defs %%%%

%\setcounter{secnumdepth}{2} % I'm not sure if this is appropriate

\setcounter{tocdepth}{1} % only go down to the section level in TOC

%%%% begin TOC formatting %%%%

\titlecontents{chapter}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Chapter~\thecontentslabel}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{preface}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Preface}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{section}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \large
               \llap{\makebox[1.5in]{%
                   \hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

%%%% end TOC formatting %%%%


\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\preface{Will This Work?}
%\chapter{Perhaps This Will Work}

\Blindtext[2]

\section{Under The Preface}

\Blindtext[3]

\subsection{To The Subway}

\Blindtext[2]

\subsection{Then To A Submarine}

\Blindtext[2]

\chapter{My First Chapter}

\blindtext[1]

\section{First Section Under A Chapter}

\Blindtext[2]

\section{Secondi}

\Blindtext[2]

\section{Triad}

\Blindtext[3]

\chapter{How About Another Chapter}

\section{Woot}

\Blindtext[2]

\end{document}

相关内容