1

I want to create a list of appendices at the end of my document, and add the corresponding entry and page number to the table of contents.

The following code is a simplified version of my code (MWE), with dummy text applied.

\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}

In this example I expect the page number for the LoA to be 4, not 5 (assuming you use the parameter a4paper for the report class). It seems to me that LaTeX is always selecting the page for the first chapter of the appendix. How can I fix that?

0

Remove \addcontentsline{toc}{chapter}{Anhangsverzeichnis} from \AtBeginEnvironment{appendices}{...} and add

\preto{\cftafterapctitle}{%
    \addcontentsline{toc}{chapter}{\listappendixname}%
}

to your preamble.

\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}

Result:

enter image description here

enter image description here


If you are option titles is used for package tocloft you have to patch \listofappendix to get a TOC entry for the LOA:

\patchcmd\listofappendix
  {\chapter*{\listappendixname}}
  {\chapter*{\listappendixname}{\addcontentsline{toc}{chapter}{\listappendixname}}}
  {}{\PatchFailed}

If it should work with and without option titles use

\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}
  • Excellent answer. One small question still comes to my mind though. I need to alter the prefix for elements in LoT and LoF, but for this I need to add the titles parameter to the tocloft package. If I do so the entry for the LoA gets removed from the LoT. What can I do here? – neuronalbit Nov 19 '17 at 16:07
  • See my updated answer. – esdd Nov 19 '17 at 17:17
  • If I could I would give u multiple upvotes for your great help. Everything works as expected. – neuronalbit Nov 19 '17 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.