我想在文档末尾创建附录列表,并将相应的条目和页码添加到目录中。
以下代码是我的代码(MWE)的简化版本,其中应用了虚拟文本。
\documentclass{report}
\usepackage{setspace}
\usepackage{titlesec}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage{appendix}
\usepackage{lipsum}
\makeatletter % start of
\newcommand{\listappendixname}{Anhangsverzeichnis}
\newlistof{appendix}{apc}{\listappendixname}
\AtBeginEnvironment{appendices}{%
\clearpage
\addcontentsline{toc}{chapter}{Anhangsverzeichnis}
\write\@auxout{%
\string\let\string\latex@tf@toc\string\tf@toc%
\string\let\string\tf@toc\string\tf@apc%
}
}
\AtEndEnvironment{appendices}{%
\write\@auxout{%
\string\let\string\tf@toc\string\latex@tf@toc%
}
}
\makeatother
\begin{document}
\tableofcontents
%\nopagebreakchapter{bla bla}
\chapter{bla bla}
\lipsum[2-3]
\section{bla bla bla}
\lipsum[2-3]
\subsection{bla bla bla}
\lipsum[2-3]
\subsection{bla bla bla}
\lipsum[2-3]
\subsection{bla bla bla}
\lipsum[2-3]
\chapter{bla bla}
\lipsum[2-3]
\section{bla bla bla}
\lipsum[2-3]
\subsection{bla bla bla}
\lipsum[2-3]
\subsection{bla bla bla}
\lipsum[2-3]
\subsection{bla bla bla}
\lipsum[2-3]
% #############################################################################
\clearpage
\listofappendix
\begin{appendices}
\section{questions}
\lipsum[1]
\section{answers}
\lipsum[1]
\end{appendices}
\end{document}
在此示例中,我希望 LoA 的页码为4
,而不是(假设您使用类的5
参数)。在我看来,LaTeX 总是选择附录第一章的页面。我该如何解决这个问题?a4paper
report
答案1
删除和\addcontentsline{toc}{chapter}{Anhangsverzeichnis}
添加\AtBeginEnvironment{appendices}{...}
\preto{\cftafterapctitle}{%
\addcontentsline{toc}{chapter}{\listappendixname}%
}
你的序言。
\documentclass{report}
\usepackage{setspace}
\usepackage{titlesec}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage{appendix}
\makeatletter % start of
\newcommand{\listappendixname}{Anhangsverzeichnis}
\newlistof{appendix}{apc}{\listappendixname}
\preto{\cftafterapctitle}{%
\addcontentsline{toc}{chapter}{\listappendixname}%
}
\AtBeginEnvironment{appendices}{%
\clearpage
\write\@auxout{%
\string\let\string\latex@tf@toc\string\tf@toc%
\string\let\string\tf@toc\string\tf@apc%
}
}
\AtEndEnvironment{appendices}{%
\write\@auxout{%
\string\let\string\tf@toc\string\latex@tf@toc%
}
}
\makeatother
\usepackage{lipsum}% only for dummy text
\begin{document}
\tableofcontents
\chapter{bla bla}
\lipsum[1]
\section{bla bla bla}
\lipsum[2]
\subsection{bla bla bla}
\lipsum[3-7]
\clearpage
\listofappendix
\begin{appendices}
\section{questions}
\lipsum[1]
\section{answers}
\lipsum[2]
\end{appendices}
\end{document}
结果:
如果您选择将此titles
选项用于软件包,tocloft
则必须进行修补 \listofappendix
才能获得 LOA 的 TOC 条目:
\patchcmd\listofappendix
{\chapter*{\listappendixname}}
{\chapter*{\listappendixname}{\addcontentsline{toc}{chapter}{\listappendixname}}}
{}{\PatchFailed}
如果它应该使用或不使用titles
选项
\documentclass{report}
\usepackage{setspace}
\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\usepackage{appendix}
\newcommand{\listappendixname}{Anhangsverzeichnis}
\newlistof{appendix}{apc}{\listappendixname}
\makeatletter
\if@cftnctoc% with option titles
\patchcmd\listofappendix
{\chapter*{\listappendixname}}
{\chapter*{\listappendixname}{\addcontentsline{toc}{chapter}{\listappendixname}}}
{}{\PatchFailed}
\else% without option titles
\preto{\cftafterapctitle}{%
\addcontentsline{toc}{chapter}{\listappendixname}%
}
\fi
\AtBeginEnvironment{appendices}{%
\clearpage
\write\@auxout{%
\string\let\string\latex@tf@toc\string\tf@toc%
\string\let\string\tf@toc\string\tf@apc%
}
}
\AtEndEnvironment{appendices}{%
\write\@auxout{%
\string\let\string\tf@toc\string\latex@tf@toc%
}
}
\makeatother
\usepackage{lipsum}% only for dummy text
\begin{document}
\tableofcontents
\chapter{bla bla}
\lipsum[1]
\section{bla bla bla}
\lipsum[2]
\subsection{bla bla bla}
\lipsum[3-7]
\clearpage
\listofappendix
\begin{appendices}
\section{questions}
\lipsum[1]
\section{answers}
\lipsum[2]
\end{appendices}
\end{document}