我已经适应了漂亮的目录解决方案作者贡萨洛·梅迪纳修改为“蓝框样式”史蒂芬莱姆克帮助我设计了章节标题和目录的一致外观,如下图所示。然而,我面临最后一个让我束手无策的问题。
对于下面的 MWE 示例,目录如下所示,我更改了章节名称的一些颜色。
包含迷你目录的“典型”章节标题如下所示:
但是,“典型”的附录标题在迷你目录中重复出现:
titletoc
请注意,如果我将包放在序言的开头而不是结尾,我就可以实现 (!) 相同的章节外观,如下所示。
换句话说,为了在章节开头的迷你目录中包含适当数量的材料,我需要titletoc
在包含创建“漂亮目录”的代码之后以某种方式titletoc
重新定义一些宏,这些宏可能在解释时被重写- 或者可能一开始\appendix
就没有定义。titletoc
那么,如何删除迷你目录中多余的“附录标题”?
\documentclass[openany]{memoir}
\usepackage{lipsum} % for testing purpose only
\usepackage{mdframed}
\usepackage{mathtools}
\usepackage{hyphenat} % I use it to prevent hyphenation of table of contents items
\usepackage{xcolor}
%%% Do not include titletoc here - do it at the end
%\usepackage{titletoc}
%%% colors definitions
\definecolor{MainRed}{rgb}{.6, .1, .1}
\definecolor{GoldDecoration}{RGB}{170, 120, 70}
% Colored section, subsections, as well as
% chapter and section name in the head - included for completeness
\setsecheadstyle{\color{MainRed}\large\bfseries}
\setsubsecheadstyle{\color{MainRed}\bfseries}
\copypagestyle{myheadings}{headings}
\makeevenhead{myheadings}{\thepage}{}{\color{MainRed}\leftmark}
\makeoddhead{myheadings}{\color{MainRed}\rightmark}{}{\thepage}
%% RedBox style -- adapted from BlueBox style, pp:43-44
% http://mirror.csclub.uwaterloo.ca/CTAN/info/latex-samples/MemoirChapStyles/MemoirChapStyles.pdf
% with modifications from
% https://tex.stackexchange.com/questions/49864/automatically-adjusting-size-of-a-box-based-on-other-content
% and others modifications of my own
\newcommand{\RedBarLength}{3em}
\newsavebox{\ChpNumBox}
\newsavebox{\ChpContBox}
\makeatletter
\newcommand*{\thickhrulefill}{%
\leavevmode\leaders\hrule height 3\p@ \hfill \kern \z@}
\newcommand*\BuildChpNum[3]{%
\begin{tabular}[t]{@{}c@{}}
\makebox[0pt][c]{#1\strut} \\[.5ex]
\colorbox{MainRed}{%
\rule[-\RedBarLength-(#3)]{0pt}{0pt}%
\rule{1ex}{0pt}\color{white}#2\strut
\rule{1ex}{0pt}}%
\end{tabular}}
\makechapterstyle{BoxedChapNum}{%
\renewcommand{\chapnamefont}{\large\scshape}
\setlength{\beforechapskip}{-30pt}
\setlength{\midchapskip}{10pt}
\setlength{\afterchapskip}{30pt}
\renewcommand{\printchaptername}{}
\renewcommand{\chapternamenum}{}
\renewcommand{\printchapternum}{%
\startcontents[chapters]
\sbox{\ChpContBox}{%
\parbox{\linewidth}{%
\printcontents[chapters]{}{1}{\color{blue}}%
}}%
\sbox{\ChpNumBox}{%
\BuildChpNum{\color{GoldDecoration}\bfseries\chapnamefont\@chapapp}%
{\chapnumfont\thechapter}%
{\ht\ChpContBox+\dp\ChpContBox}%
}}
\renewcommand{\printchapternonum}{%
\startcontents[chapters]
\sbox{\ChpContBox}{%
\parbox{\linewidth}{%
%\printcontents[chapters]{}{1}{}% Useless?
}}%
\sbox{\ChpNumBox}{%
\BuildChpNum{\chapnamefont\vphantom{\@chapapp}}%
{\chapnumfont\hphantom{\thechapter}}%
{\ht\ChpContBox+\dp\ChpContBox}%
}}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{%
\usebox{\ChpNumBox}\hfill
\parbox[t]{\hsize-\wd\ChpNumBox-1em}{%
\raggedright\vspace{\midchapskip}%
{\color{GoldDecoration}\thickhrulefill}\\[10pt]
{\chaptitlefont\LARGE\textcolor{MainRed}{\nohyphens{##1}}}\par\vspace*{10pt}
\renewcommand\cftsectionpagefont{\sffamily\itshape\color{black}}
\renewcommand\cftsubsectionpagefont{\sffamily\itshape\color{black}}
\renewcommand\cftsectionfont{\sffamily\color{MainRed}}
\renewcommand\cftsubsectionfont{\sffamily\color{MainRed}}
\printcontents[chapters]{}{1}{}%
}}%
}
\makeatother
%%%%%% use the newly defined style
\chapterstyle{BoxedChapNum}
%%% Pretty ToC
%%%%% adapted from https://tex.stackexchange.com/questions/35825/pretty-table-of-contents/35835
% a modification of the leftbar environment defined by the framed package
% will be used to place a vertical colored bar separating the page number and the
% title in chapter entries
\renewenvironment{leftbar}{%
\def\FrameCommand{\textcolor{GoldDecoration}{\vrule width 2pt depth 6pt}\hspace*{15pt}}%
\MakeFramed{\advance\hsize-\width\FrameRestore}}%
{\endMakeFramed}
\makeatletter
% redefinitions for chapter entries
\renewcommand\chapternumberline[1]{\mbox{\small\@chapapp~#1}\par\noindent\Large}
\renewcommand\cftchapterfont{\sffamily}
\cftsetindents{chapter}{0pt}{0em}
\renewcommand\cftchapterpagefont{\Huge\sffamily\bfseries\color{MainRed}}
\newcommand*{\l@mychap}[3]{%
\def\@chapapp{\color{MainRed}#3}
\vskip1ex%
\par\noindent\begin{minipage}{\textwidth}%
\parbox{4.5em}{%
\hfill{\cftchapterpagefont#2}%
}\hspace*{1.5em}%
\parbox{\dimexpr\textwidth-4.5em-15pt\relax}{%
\leftbar\cftchapterfont{\color{MainRed}#1}\hspace{1sp}\endleftbar%
}%
\end{minipage}\par%
}
\renewcommand*{\l@chapter}[2]{%
\l@mychap{#1}{#2}{\chaptername}%
}
\renewcommand*{\l@appendix}[2]{%
\l@mychap{#1}{#2}{\appendixname}%
}
% redefinitions for section entries
\renewcommand\cftsectionfont{\sffamily\color{black}}
\renewcommand\cftsectionpagefont{\sffamily\itshape\color{MainRed}}
\renewcommand\cftsectionleader{\nobreak}
\renewcommand\cftsectiondotsep{\cftnodots}
\renewcommand\cftsectionafterpnum{\hspace*{\fill}}
\setlength\cftsectionnumwidth{12em}
\cftsetindents{section}{6em}{3em}
\renewcommand\cftsectionformatpnum[1]{%
\hskip1em\hbox to \@pnumwidth{{\cftsectionpagefont #1\hfill}}}
% redefinitions for subsection entries
\renewcommand\cftsubsectionfont{\sffamily\color{black}}
\renewcommand\cftsubsectionpagefont{\sffamily\itshape\color{MainRed}}
\renewcommand\cftsubsectionleader{\nobreak}
\renewcommand\cftsubsectiondotsep{\cftnodots}
\renewcommand\cftsubsectionafterpnum{\hspace*{\fill}}
\setlength\cftsubsectionnumwidth{12em}
\cftsetindents{subsection}{9em}{3em}
\renewcommand\cftsubsectionformatpnum[1]{%
\hskip1em\hbox to \@pnumwidth{{\cftsubsectionpagefont #1\hfill}}}
\makeatother
%%%% End of Pretty ToC
\settocdepth{subsection}
\setsecnumdepth{subsection}
% include after the change above
\usepackage{titletoc}
\begin{document}
\nouppercaseheads
\pagestyle{myheadings}
\frontmatter
\tableofcontents*
\chapter{Foreword}
\lipsum[9]
\mainmatter
\chapter{First Chapter}
\lipsum[1]
\section{First section}
\lipsum[2]
\subsection{First subsection}
\lipsum[3]
\section{Second section}
\lipsum
\chapter{Second Chapter}
\section{First section}
\appendix
\chapter{First Appendix}
\lipsum[7]
\section{First section}
\lipsum[8]
\section{Second section}
\end{document}
请注意,我添加了一些额外的行(例如部分标题的颜色选择),因为我所做的各种尝试都导致其他元素发生改变;所包含的元素是“完整外观和感觉”所需要的。
答案1
这里的问题是memoir
定义了一个名为 的“新”分段结构,appendix
它有自己的\l@appendix
。这在 LaTeX 标准类中不存在。附录中的一章只是一个章节。
titletoc
现在“挂钩”到命令\l@
...,在加载包时保存旧的定义(这是您注意到的位置效果;titletoc
保存旧定义可能会更好\AtBeginDocument
,那么在加载包时就不会产生区别)。
您正在重新定义\l@appendix
,但titletoc
并不了解\l@appendix
,因此无法与之挂钩。
我(天真地!)模仿了titletoc
附录的章节钩子,如下所示:
\let\ttl@savel@appendix\l@appendix
\def\l@appendix{\let\ttl@savel@chapter\ttl@savel@appendix\ttl@lselect{chapter}}
(立即插入\makeatother
到您的 MWE 中)
就其本身而言,这应该使附录的行为与章节类似titletoc
。如果您需要章节和附录的行为有所不同,您可以做出一个不那么幼稚的定义。至少在我的测试中,多余的附录标题消失了。