我看见Gonzalo 在书籍类中制作的精美目录代码我想在回忆录类中使用它。但它的输出与书籍类中的输出不同。如何使此代码与回忆录类兼容?谢谢您的帮助!
答案1
尝试这个。
% memtocprob.tex SE 594921
%%% This code is essentially a slightly modified version of the code
%%% by Gonzalo Medina in https://tex.stackexchange.com/questions/83184
%\documentclass{book}
\documentclass{memoir}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{titletoc}
\usepackage{etoolbox}
\definecolor{secnum}{RGB}{13,151,225}
\definecolor{ptcbackground}{RGB}{212,237,252}
\definecolor{ptctitle}{RGB}{0,177,235}
\pretocmd{\tableofcontents}{\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]}{}{}
\apptocmd{\tableofcontents}{\end{mdframed}}{}{}
\patchcmd{\tableofcontents}{\contentsname}{\color{ptctitle}\contentsname}{}{}
\titlecontents{section}
[4em]{\sffamily}
{\color{secnum}\contentslabel{2.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}
\titlecontents{lsection}
[5.8em]{\sffamily}
{\color{secnum}\contentslabel{2.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}
\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode
\advance\leftskip\@tempdima
\hskip -\leftskip
\colorbox{ptctitle}{\strut%
\makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
\color{white}\bfseries\sffamily#1%
\nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
\penalty\@highpenalty
\endgroup
\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\@chapapp@head}{} %%% PW added
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother
\newcommand\PartialToC{%
\startcontents[chapters]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[chapters]{l}{1}{\colorbox{ptctitle}{%
\parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
\strut\color{white}\bfseries\sffamily\makebox[5em]{%
Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}
\begin{document}
\tableofcontents
\chapter{Chapter One}
%\PartialToC
\section{Section One One}
\section{Section One Two}
\section{Section One Three}
\section{Section One Four}
%\stopcontents[chapters]
\chapter{Chapter Two}
\PartialToC
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}
\stopcontents[chapters]
\chapter{Chapter Three}
\PartialToC
\section{Section Three One}
\section{Section Three Two}
\section{Section Three Three}
\section{Section Three Four}
\section{Section Three Five}
\end{document}
编辑
我费了一番功夫才扩展了代码以满足\subsection
(部分)需求,\part
尽管我不知道您是否使用它们。
% memtocprob.tex (extended) SE 594921
%%% This code is essentially a slightly modified, and extended, version of the code
%%% by Gonzalo Medina in https://tex.stackexchange.com/questions/83184 to cater
%%% for the memoir xlass.
%\documentclass{book}
\documentclass{memoir}
\setsecnumdepth{subsection}
\settocdepth{subsection}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{titletoc}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{comment}
\definecolor{secnum}{RGB}{13,151,225}
\definecolor{ptcbackground}{RGB}{212,237,252}
\definecolor{ptctitle}{RGB}{0,177,235}
\pretocmd{\tableofcontents}{\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]}{}{}
\apptocmd{\tableofcontents}{\end{mdframed}}{}{}
\patchcmd{\tableofcontents}{\contentsname}{\color{ptctitle}\contentsname}{}{}
\titlecontents{section}
[4em]{\sffamily}
{\color{secnum}\contentslabel{2.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}
%%%% PW added subsection code
\titlecontents{subsection}
[7em]{\sffamily}
{\color{secnum}\contentslabel{3.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}
\titlecontents{lsection}
[5.8em]{\sffamily}
{\color{secnum}\contentslabel{2.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}
%%% PW added lsubsection code
\titlecontents{lsubsection}
[8.8em]{\sffamily}
{\color{secnum}\contentslabel{3.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}
%\begin{comment}
\makeatletter
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode
\advance\leftskip\@tempdima
\hskip -\leftskip
\colorbox{ptctitle}{\strut%
\makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
\color{white}\bfseries\sffamily#1%
\nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
\penalty\@highpenalty
\endgroup
\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\newcommand{\@chapapp@head}{} %%% PW added
\providecommand{\@chapapp@head}{} %%% PW added
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother
%\end{comment}
%%%% PW added part code
%\begin{comment}
\makeatletter
\renewcommand*\l@part[2]{%
\ifnum \c@tocdepth >-2\relax
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode
\advance\leftskip\@tempdima
\hskip -\leftskip
\colorbox{ptctitle}{\strut%
\makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
\color{white}\bfseries\sffamily#1%
\nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
\penalty\@highpenalty
\endgroup
\fi}
\makeatother
%\end{comment}
\begin{comment}
\titlecontents{part}
[4em]{\sffamily}
{\color{secnum}\contentslabel{2.3em}\normalcolor}{}
{\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
\color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}
\newcommand\PartialPartToC{%
\startcontents[parts]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[parts]{l}{1}{\colorbox{ptctitle}{%
\parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
\strut\color{white}\bfseries\sffamily\makebox[5em]{%
Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}
\end{comment}
\newcommand\PartialToC{%
\startcontents[chapters]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[chapters]{l}{1}{\colorbox{ptctitle}{%
\parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
\strut\color{white}\bfseries\sffamily\makebox[5em]{%
Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}
\begin{document}
\tableofcontents
\part{First part}
%\PartialPartToC
%\stopcontents[parts]
\chapter{Chapter One}
%\PartialToC
\section{Section One One}
\section{Section One Two}
\section{Section One Three}
\section{Section One Four}
%\stopcontents[chapters]
\chapter{Chapter Two}
\PartialToC
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}
\subsection{Subsection Two Three One}
\stopcontents[chapters]
\chapter[Three ToC][Three header]%
{Chapter Three}
\PartialToC
\section{Section Three One}
\section{Section Three Two}
\section{Section Three Three}
\section{Section Three Four}
\section{Section Three Five}
\subsection{Subsection Three Five One}
\lipsum
\end{document}
enter code[![enter image description here][2]][2] here
如果您需要更多帮助,Gonzalo 也许能够提供帮助。