I'm trying to find a more elegant solution for ToC, LoT, and LoF formatting for my university's thesis/dissertation style. The short form of their requirements means I need one page style for the first page of the ToC, a second page style for following pages up to where the appendix listings start, and yet a third style from the appendix listing through the end.
It looks like I've run into a bug, or else I'm just missing a better solution.
Minimal example 1
\documentclass{report}
\usepackage{lipsum}
\begin{document}
\addtocontents{toc}{\protect\thispagestyle{empty}\protect\pagestyle{plain}}
\tableofcontents
\input{twenty-chapters}
\appendix
\addtocontents{toc}{\protect\pagestyle{headings}\protect\thispagestyle{plain}}
\input{twenty-chapters}
\chapter{Foo} \include{a-chapter}
\addtocontents{toc}{\protect\thispagestyle{headings}}
\end{document}
Though I don't think it's relevant, the contents of twenty-chapters.tex is 20 lines of
\chapter{Foo} \include{a-chapter}
and a-chapter.tex is just:
\lipsum[1]
\section{Bar}
\lipsum[2-7]
\begin{figure}[tbp] \caption{Figure N} \end{figure}
\begin{figure}[tbp] \caption{Figure N} \end{figure}
\begin{figure}[tbp] \caption{Figure N} \end{figure}
\section{Baz}
\lipsum[8-9]
\begin{figure}[tbp] \caption{Figure N} \end{figure}
\begin{figure}[tbp] \caption{Figure N} \end{figure}
\begin{figure}[tbp] \caption{Figure N} \end{figure}
Here, I'm using the empty page style as a stand-in for the first page format, plain for the second page format, and headings for the third. This works fine on all but the last page of the ToC, which ends up with a plain page style instead of headings.
Minimal example 2:
\documentclass{report}
\usepackage{lipsum}
\begin{document}
\addtocontents{toc}{\protect\thispagestyle{empty}\protect\pagestyle{plain}}
\tableofcontents
\input{twenty-chapters}
\appendix
\addtocontents{toc}{\protect\pagestyle{headings}\protect\thispagestyle{plain}}
\input{twenty-chapters}
\addtocontents{toc}{\protect\thispagestyle{headings}}
\chapter{Foo} \include{a-chapter}
\end{document}
Exactly the same as the first document, but now my final \addtocontents is before the last chapter. On this one, everything works perfectly. Empty page style on ToC page 1, plain on ToC page 2, and headings on ToC pages 3-4.
Why would this make a difference? From looking at the .toc files for each example, my final \thispagestyle command is missing on the example that fails. Why would my \addcontents command not take effect in some situations?