附录出现在上一章的子目录中

附录出现在上一章的子目录中

我想在每个章节的开头添加一个子目录。我通过使用包titletoc并重新定义章节来实现此目的。这在主要内容中效果很好,但是当我添加附录时,每个附录的标题都会出现在上一章的迷你目录中。

\documentclass[]{memoir}

\usepackage{titletoc}
\makeatletter
\let\stdchapter\chapter
\renewcommand*\chapter{%
    \@ifstar{\starchapter}{\@dblarg\nostarchapter}}
\newcommand*\starchapter[1]{\stdchapter*{#1}}
\def\nostarchapter[#1]#2{%
    \stdchapter[{#1}]{#2}
    \startcontents[chapters]
    \printcontents[chapters]{}{1}{\setcounter{tocdepth}{3}}}
\makeatother

\begin{document}

\chapter{A chapter with a nice table of contents}
\section{A section}
\subsection{a subsection}
\section{Another section}

\chapter{A chapter that has the first appendix in its table of contents}
\section{A section}
\subsection{a subsection}
\section{Another section}

\begin{appendices}
\chapter{An appendix}
\section{A section}
\subsection{a subsection}
\section{This appendix has the second appendix in its table of contents}

\chapter{The second appendix}
\section{A section}
\subsection{a subsection}
\section{Another section}
\end{appendices}

\end{document}

如何避免在附录 n-1 的小目录中获取附录 n 的标题?

答案1

这里有两个问题:

  • appendices使用appendixtoc 条目类型,而不是chapter,因此 chapters 中的 chapter 条目appendix会进入文件中的错误位置.ptc。可以通过以下方法解决此问题\let\l@appendix\l@chapter
  • 使用明确的\stopcontent[chapters]before\begin{appendices}来防止第一个附录出现在上一章的目录中。

\documentclass[]{memoir}

\usepackage{xpatch}
\usepackage{titletoc}
\makeatletter
\let\stdchapter\chapter
\renewcommand*\chapter{%
    \@ifstar{\starchapter}{\@dblarg\nostarchapter}}
\newcommand*\starchapter[1]{\stdchapter*{#1}}
\def\nostarchapter[#1]#2{%
    \stdchapter[{#1}]{#2}
    \startcontents[chapters]%
    \printcontents[chapters]{}{1}{\setcounter{tocdepth}{3}}%
}
\makeatother


\begin{document}

\chapter{A chapter with a nice table of contents}
\section{A section}
\subsection{a subsection}
\section{Another section}

\chapter{Another chapter with a nice table of contents}
\section{A section}
\subsection{a subsection}
\section{Another section}


\chapter{A chapter that has the first appendix in its table of contents}
\section{A section}
\subsection{a subsection}
\section{Another section}


\makeatletter
\let\l@appendix\l@chapter
\makeatother
\stopcontents[chapters]
\begin{appendices}
\chapter{An appendix}
\section{A section}
\subsection{a subsection}
\section{This appendix has the second appendix in its table of contents}

\chapter{The second appendix}
\section{A section}
\subsection{a subsection}
\section{Another section}

\chapter{The third appendix}
\section{A section}
\subsection{a subsection}
\section{Another section}

\end{appendices}

\end{document}

相关内容