# Why does the changebar package create a page full of “spsp” and how can this be fixed?

To place bars into the "Table of Contents" I use the changebar package, version 2005/09/18 v3.5c, and compile the document with pdflatex (TeXLive 2013, updated 2014/02/25) at least four times. (With latex, dvi output, the issue does not occur.) This works OK, but it also creates a page full of "spsp" (presumably from small point-lengthes) after the last page of the document (known problem). Using the afterpage and atbegshi packages I can get rid of that page by placing \afterpage{\AtBeginShipoutNext{\AtBeginShipoutDiscard}} before \end{document}, but that neither tells me what is wrong nor how to correct it.

The MWE is a bit longer, because a "critical toc length" is needed for the error to occur. Even a lot of \cbend\pagebreak\cbstart in the toc (to prevent the changebars to span a pagebreak) did not solve the problem. Increasing \cb@maxpoint neither solved the problem.

\documentclass[12pt]{report}
\usepackage[c5paper]{geometry}
\usepackage[color]{changebar}
\makeatletter
\newcommand{\cbgreen}{%
\write\@auxout{\string\@writefile{toc}{\string\cbend{}}}
\write\@auxout{\string\@writefile{toc}{\string\cbcolor{green}}}
\write\@auxout{\string\@writefile{toc}{\string\cbstart}}}
\newcommand{\cbred}{%
\write\@auxout{\string\@writefile{toc}{\string\cbend{}}}
\write\@auxout{\string\@writefile{toc}{\string\cbcolor{red}}}
\write\@auxout{\string\@writefile{toc}{\string\cbstart}}}
\begin{document}
\tableofcontents
\part{w}
\write\@auxout{\string\@writefile{toc}{\string\cbcolor{red}}}
\write\@auxout{\string\@writefile{toc}{\string\cbstart}}
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\part{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\subsubsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\subsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\subsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\section{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\paragraph{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsection{w}
\subsection{w}
\section{w}
\part{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\subsection{w}
\subsection{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\subsubsection{w}
\paragraph{w}
\paragraph{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\write\@auxout{\string\@writefile{toc}{\string\newpage}}
\part{w}
\cbgreen
\chapter{w}
\section{w}
\part{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\part{w}
\cbgreen
\chapter{w}
\cbred
\chapter{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\write\@auxout{\string\@writefile{toc}{\string\newpage}}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\part{w}
\cbred
\chapter{w}
\cbgreen
\chapter{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\cbred
\chapter{w}
\section{w}
\section{w}
\cbgreen
\chapter{w}
\section{w}
\write\@auxout{\string\@writefile{toc}{\string\cbend{}}}
\makeatother
\end{document}

-
I see it with TeXLive 2010 and pdflatex, but not with latex (dvi output). –  jfbu Feb 25 at 18:06
there seems to be a bug in the changebar package. A trace just before \end{document} gives \cb@pdfxy #1#2#3#4#5->\cb@@findpdfpoint {#1}{#2}\ifnum #3=\cb@pdfx \relax \ifnu m #4=\cb@pdfy \relax \ifdim #5=\cb@pdfz \relax \else \cb@error \fi \else \cb@er ror \fi \else \cb@error \fi  but when it does the \ifnum \cb@pdfx expands to 4531218sp and \cb@pdfy expands to 29548831sp. No doubt the spsp come from that. –  jfbu Feb 25 at 21:05
@jfbu: Thanks! +1 for your answer and accepted. Could you please move your comment into your answer? I think this is essential information to the "Why?" part of my question. –  Stephen Feb 26 at 11:49

The problem appears to be solved with the following set-up in the preamble:

\makeatletter
\AtEndDocument{%
\def\cb@checkPdfxy#1#2#3#4#5{%
\cb@@findpdfpoint{#1}{#2}%
\ifdim#3sp=\cb@pdfx\relax      % <<-- original has \ifnum#3=\cb@pdfx
\ifdim#4sp=\cb@pdfy\relax      % <<-- original has \ifnum#4=\cb@pdfy
\ifdim#5=\cb@pdfz\relax
\else
\cb@error
\fi
\else
\cb@error
\fi
\else
\cb@error
\fi
}}
\makeatother
\usepackage[color]{changebar}


Please note that the order of things must be as above.

Indeed adding \tracingmacros1 just before \end{document} and examining the log one finds a \let\cb@pdfxy\cb@checkPdfxy where \cb@checkPdfxy is as above except that it uses \ifnum#3=\cb@pdfx, \ifnum#4=\cb@pdfy, but when this is later expanded \cb@pdfx and \cb@pdfy turn out to be some number followed by sp.

I chose to transform the test in an \ifdim but another way would be possibly to modify the part of the code defining the scratch macros \cb@pdfx and \cb@pdfy and avoid the extra sp (one sees in the log that the sp is added by the code). Not having read anything else from the changebar code I don't know if this alternative would break something else.

The patched macro is put in \AtEndDocument before the loading of the package in order to have the \let\cb@pdfxy\cb@checkPdfxy executed after it.

There should be a way to do a patch using etoolbox but I noticed in the doc of changebar that \cb@checkPdfxy was defined as a \gdef inside some other macro (perhaps an option processing) and I did not investigate more. Anyway, with this the infamous page of spsp's is gone.

-

I cannot see it with the current TeXLive 2013 and using

\immediate\write\@auxout{...}


or use

\addtocontents{toc}{\string\cbend{}}


It works with the dvips driver and the sequence latex->dvips->ps2pdf but not with the pdftex driver.

-
try to run the document with latex->dvips->ps2pdf –  Herbert Feb 25 at 18:51
latex->dvips->ps2pdf would work indeed, but I need pdflatex instead of latex for the original (not MWE) document (and latex->dvips->ps2pdf does not tell me what is going wrong). –  Stephen Feb 26 at 12:10