附录中的子目录和作为目录中章节的附录

附录中的子目录和作为目录中章节的附录

我正在尝试编写一份文档

  1. 每部分和每章首页上的“子目录”. Gonzalo Medina 的解决方案上一个问题可以用来实现这一点。

  2. 附录排版为主目录中的章节附录一章。Alan Munn 有之前已解决这。

然而,当这两种解决方案结合在一起时,就会出现我无法解释的效应,更不用说纠正了。


和的应该分别{tocdepths}为和。 这也适用于阑尾。 Medina 建议:\parts\chapters{1}{2}

\documentclass[twoside,a4paper,12pt]{report}

\usepackage{titlesec,titletoc}
\usepackage[titles]{tocloft}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\begin{document}
\setcounter{tocdepth}{2}
\tableofcontents
\clearpage

\part*{A Part}
\startcontents[parts]
\printcontents[parts]{}{1}{\setcounter{tocdepth}{1}} 
\clearpage

\chapter{A Chapter}
\startcontents[chapters]
\printcontents[chapters]{}{}{\setcounter{tocdepth}{2}} 
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}
\stopcontents[chapters]

\chapter{B Chapter}
\section{A Section}
\subsection{A Subsection}
\stopcontents[parts]

%%% The Appendix %%%
\clearpage\phantomsection
\part*{Appendices}

%\addcontentsline{toc}{chapter}{Appendices}
%\addtocontents{toc}{\setcounter{tocdepth}{0}}
%\ChapAsSecOnToc
%\ChapAsChapOnPartoc

\appendix
\startcontents[parts]
\printcontents[parts]{}{}{\setcounter{tocdepth}{1}}

\chapter{An Appendix}
\startcontents[chapters]
\printcontents[chapters]{}{}{\setcounter{tocdepth}{2}}
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}
\stopcontents[chapters]

\chapter{B Appendix}
\section{A Section}
\subsection{A Subsection}
\stopcontents[parts]

\end{document}

在此处输入图片描述

这将按预期完成工作,但附录在目录中的表示仍需更改。

采用 Munn 的解决方案来改变目录中附加章节的设置可以通过以下方式完成: (1)在上面的 MWE 中
注释掉下面的四个命令,并 (2)在文档开始之前定义以下内容:%%% The Appendix %%%

% store current settings
\newlength{\truechapindent}
\setlength{\truechapindent}{\cftchapindent}
\newlength{\truebeforechapskip}
\setlength{\truebeforechapskip}{\cftbeforechapskip}
\let\truechapfont\protect\cftchapfont
\newcounter{truechapdotsep}
\setcounter{truechapdotsep}{\cftchapdotsep}

% chapter to look like section on toc
\newcommand{\ChapAsSecOnToc}{
    \addtocontents{toc}{
    \setlength{\cftbeforechapskip}{\cftbeforesecskip}
    \setlength{\cftchapindent}{\cftsecindent}
    \protect\renewcommand{\cftchapfont}{\cftsecfont}
    \protect\renewcommand{\protect\cftchapdotsep}{\cftsecdotsep}
}}

% chapter still to look like chapter on partoc
\newcommand{\ChapAsChapOnPartoc}{
    \addtocontents{ptc}{
    \setlength{\cftbeforechapskip}{\truebeforechapskip}
    \setlength{\cftchapindent}{\truechapindent}
    \protect\renewcommand{\cftchapfont}{\truechapfont}
    \protect\renewcommand{\protect\cftchapdotsep}{\value{truechapdotsep}}
}}

在此处输入图片描述

现在我遇到了两个问题:

  1. 重置{tocdepth}不再\printcontents[XX]{}{}{\setcounter{tocdepth}{YY}}有效,即{tocdepth}残留{0}
  2. 附录部分中的章节未以粗体显示

我该如何解决问题(1)和(2)?

\printcontent我尝试通过将的第四个参数替换为 来缓解第一个问题{\addtocontents{ptc}{\setcounter{tocdepth}{YY}}}。但现在写入 .ptc 文件的最后一个深度会覆盖第一个。而且附录部分目录中的章节仍然没有设置为粗体。

但是,它使 5 个页面中的 4 个页面看起来像我想要的那样:

在此处输入图片描述

答案1

这是实现此目的的一种方法etoc

编辑:我最初误读了 OP 的查询。请参阅底部的更新答案

\documentclass[twoside,a4paper,12pt]{report}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoc}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\usepackage{hyperref}

\begin{document}

\etocsettocdepth{subsection}
% trick to avoid an "Appendices" entry in TOC.
\etocsetlevel{vanishingpart}{6}% i.e. ignore
\makeatletter
\newcommand*\toclevel@vanishingpart{-1}% make hyperref happy
\makeatother


\tableofcontents

% from now on, TOCs do not open a new page,
% not do they print a prominent "Contents"
\etocsettocstyle{}{}
% for local TOCs, the "vanishingpart" must
% act as real "part" (really important only
% for the main \localtableofcontents of Appendix document part
\etocsetlevel{vanishingpart}{-1}% i.e. part
% ignore from now on the on-the-fly limitation of the tocdepth
% to chapter level applying in the main TOC
% (see \etocsettocdepth.toc{chapter} below)
\etocignoretoctocdepth

\clearpage

\phantomsection
\part*{A Part}
\addcontentsline{toc}{part}{A Part}

\etocsetnexttocdepth{section}
\localtableofcontents

\clearpage

\chapter{A Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}

%%% The Appendix %%%
\clearpage

\phantomsection
\part*{Appendices}
\addcontentsline{toc}{vanishingpart}{Appendices}
\etocsettocdepth.toc{chapter}

\etocsetnexttocdepth{section}
\localtableofcontents

\appendix

\chapter{An Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}

\end{document}

上述操作有一些复杂之处,只是为了避免主目录中有条目,Appendices因为这是 OP 图像中要求的。这看起来很奇怪,因为没有办法在主目录中单击以进入附录,而作为本地目录,最多只能单击附录 A,而缺少文档中所有附录的本地目录。但可以使用 PDF 书签作为替代。

这是一个更简单的方法,它给出的结果与上面完全相同,只是主目录看起来是这样的:

\documentclass[twoside,a4paper,12pt]{report}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoc}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\usepackage{hyperref}

\begin{document}

\etocsettocdepth{subsection}

\tableofcontents

% from now on, TOCs do not open a new page,
% not do they print a prominent "Contents"
\etocsettocstyle{}{}
% ignore from now on the on-the-fly limitation of the tocdepth
% to chapter level applying in the main TOC
% (see \etocsettocdepth.toc{chapter} below)
\etocignoretoctocdepth

\clearpage

\phantomsection
\part*{A Part}
\addcontentsline{toc}{part}{A Part}

\etocsetnexttocdepth{section}
\localtableofcontents

\clearpage

\chapter{A Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}

%%% The Appendix %%%
\clearpage

\phantomsection
\part*{Appendices}
\addcontentsline{toc}{part}{Appendices}
\etocsettocdepth.toc{chapter}

\etocsetnexttocdepth{section}
\localtableofcontents

\appendix

\chapter{An Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}

\end{document}

尽管上述内容有其优点,但这并不是所要求的。

开始了:

\documentclass[twoside,a4paper,12pt]{report}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoc}

% take control of the part-page
\titleformat{\part}[display]
  {\normalfont\huge\filcenter\bfseries}
  {\partname\ \thepart}{0pt}{}
\titleclass{\part}{top}

\usepackage{hyperref}

\makeatletter
\newcommand*\MimickAppendicesAsSections {%
   % This is executed as part of the main \tableofcontents
   % inside a scope limiting group
   \etocsetlevel{part}{0}%
   \etocsetlevel{chapter}{1}%
   \etocsetlevel{section}{2}%
   % etoc is in "compatibility mode", so the actual
   % \l@part will get executed, similarly for chapter
   \let\l@part\l@chapter
   \let\l@chapter\l@section
   % Only used once, for main TOC
   \global\let\MimickAppendicesAsSections\empty
}%
\makeatother

\begin{document}

\etocsettocdepth{subsection}

\etocsetlevel{part}{6}% a priori, ignore "Parts" in the main TOC, as requested

\tableofcontents

% reset to normal level (needed for limiting local TOCs)
\etocsetlevel{part}{-1}
% from now on, TOCs do not open a new page,
% not do they print a prominent "Contents"
\etocsettocstyle{}{}
% ignore from now on the on-the-fly limitation of the tocdepth
% to appendix chapter sin the main TOC (which are rendred as section)
% (see \etocsettocdepth.toc{chapter} below)
\etocignoretoctocdepth

\clearpage

\phantomsection % for PDF bookmarks
\part*{A Part}
\addcontentsline{toc}{part}{A part}

\etocsetnexttocdepth{section}
\localtableofcontents

\clearpage

\chapter{A Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Chapter}
\localtableofcontents

\clearpage

\section{A Section}
\subsection{A Subsection}

%%% The Appendix %%%
\clearpage

\phantomsection
\part*{Appendices}
\addtocontents{toc}{\protect\MimickAppendicesAsSections}
\etocsettocdepth.toc{chapter}
\addcontentsline{toc}{part}{Appendices}

\etocsetnexttocdepth{section}
\localtableofcontents

\appendix

\chapter{An Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}
\section{B Section}
\subsection{A Subsection}

\chapter{B Appendix}
\localtableofcontents
\clearpage

\section{A Section}
\subsection{A Subsection}

\end{document}

这里仅发布主要目录:

在此处输入图片描述

我认为所有其他目录也符合要求。如果我再次读错了什么,请通知我。

相关内容