# warning (pdf backend): pop empty color page stack 0

I've been receiving this warning and for the past several hours (erh, something like 12 hours now). I've been trying to narrow down what's been causing it, but to no avail. I managed to cut down the project to ~1000 lines of code and still produce the warning, but I've tried cutting out tens of different things from that ~1000 lines, and so far, whatever I try to cut beyond that makes the warning go away, making it incredibly difficult to debug. There's also of course no line the warning points to and no visual indication in the document that something is wrong to point me in the right direction.

Here is a link to the actual project that produces the warning. So far I've managed to deduce that \includeonly{basics,apd} reproduces the error, but if you remove either basics or apd here, the warning goes away. Furthermore, while I haven't tested ever two-chapter combination, it is the only two-chapter combination I have found that will reproduce the warning (and no chapter by itself will reproduce it).

I am also able to remove ~half the lines from either of these files to still retain the warning, but, besides the actual amount of code I'm removing, I don't see any pattern in the code that does and does not reproduce the warning. I've also been able to remove the title page, table of contents, references, and index, and still retain the warning.

I apologize for not being able to give any sort of MWE, but I really wasn't able to get it down much more than \includeonly{basics,apd} even after hours and hours of experimenting. Hopefully at least someone has an idea as to what this error even means so that I have a better chance of debugging it?

(FWIW, the pdf backend I've been using is LuaLaTeX, though I've confirmed that pdfLaTeX produces the warning just the same (from the TeX Live 2016 distro). Perhaps it's also worth noting that Texmaker (my current IDE) does not report this warning and I have to go into the log file to find it.)

• I fear this question will be closed, but please, could you upload a .zip-file of the project? Couldn't find a way to download the whole project, without adding it to my Google-account first. – Runar Jun 26 '16 at 13:29
• No chance you might post a MWE nonetheless? I sense there is a legitimate question, and think that you might get a better response if you post one. – A Feldman Jun 26 '16 at 13:33
• @RunarTrollet I've updated the link in the question to now point to a .zip instead. – Jonathan Gleason Jun 26 '16 at 13:36
• @AFeldman Unforutnately I cannot. "Body is limited to 30000. You entered 70000+." And that was just one of the two files . . . – Jonathan Gleason Jun 26 '16 at 13:40
• Ok, I see what you mean. – A Feldman Jun 26 '16 at 13:42

Well what it means is that's you've corrupted the colour stack. Unlike font changes colour is not an intrinsic attribute of a tex box, so colour changes are managed by nodes marking the change of state whatsit, Originally it was very hard to maintain colour correctly over page breaks, as the macro package needed to normalise colour in the page headings and could not reset the current colour for the text at the start of a page as the page breaking routine can not easily insert a special there.

dvips introduced the notion of a colour stack where the driver maintains a stack of colours that are handled correctly at a page break, and the macro layer just needs to push and pop colours on to this stack rather than trying to set the "correct" colour at points over which it has little control.

pdftex (and luatex) extend this model further and have specific push/pop commands

more or less:

 \pdfcolorstack  push {red}
...
\pdfcolorstack pop


so you get the error if you have a \colorstack pop without a matching push, or in latex macro wrappers, if you have a \reset@color without a \set@color

I guess this fixes it, modify \tcbtitle after loading tcolorbox in your structure file:

\usepackage{tcolorbox} % Required for creating the theorem, definition, exercise and corollary boxes.

\makeatletter
\renewcommand\tcbtitle{\ifx\tcbtitletext\@empty\else%
{\leavevmode\color{tcbcol@title}\kvtcb@fonttitle
\kvtcb@haligntitle\kvtcb@before@title
\tcbtitletext\kvtcb@after@title}\fi}
\makeatother


or perhaps better

\makeatletter
\renewcommand\tcbtitle{\ifx\tcbtitletext\@empty\else%
{\kvtcb@fonttitle\kvtcb@haligntitle\kvtcb@before@title
\leavevmode\color{tcbcol@title}\tcbtitletext\kvtcb@after@title}\fi}
\makeatother


so you delay the color until after \kvtcb@before@title

• I can confirm that this does indeed fix the warning. It is just a tiny bug in the tcolorbox package then? How on earth did you figure that out anyways? TeXnician? More like TeXNinja! – Jonathan Gleason Jun 27 '16 at 3:02
• @JonathanGleason I've used TeX before:-) – David Carlisle Jun 27 '16 at 6:35
• the warning is shown on a page that has a tcbox coloured environment at the top of the page, which was the main hint, plus a history of various packages dropping specials when doing page breaks or vsplits as (depending how you look) a zero sized box just containing a colour whatsit can look empty, so I suspected the package was dropping a whatsit at a page break leaving matching restore. so in your code and tcolorbox replaced every \color by \leavevmode\color (not in general a good idea) but the warning went so it was just a matter of finding which one. – David Carlisle Jun 27 '16 at 6:44
• I will add a \leavevmode to \tcbtitle as you have suggested. Hopefully, there is no user code with \tcbtitle where leaving the vertical mode introduces problems. :-) – Thomas F. Sturm Jun 27 '16 at 14:34
• @ThomasF.Sturm as we both know, you can never win all the time here, just a matter of avoiding being tripped up as often as possible:-) – David Carlisle Jun 27 '16 at 14:43

The warning "pop empty color stack" is usually caused by color stack commands out of order. A typical scenario is the use of low level box set command \setbox instead of LaTeX box set macros (\sbox, environment lrbox, ...). When LaTeX sets a color, then the color reset command is issued after the current group. Therefore LaTeX box macros add a group layer around the contents of the box. Without this additional group, the color reset (pop operation of the color stack) leaks out of the box and is executed at the wrong time:

\documentclass{article}
\usepackage{color}
\begin{document}
\setbox0=\hbox{\color{red}Red}
\null
\end{document}


At the begin of the box the color red is put on the stack. Then the box is closed and the pop operation leaks out of the box and is executed at the time of the box definition, before the box is used. Push and pop are out of order (in the example the box is not used, only the pop special).

Solution: A group keeps the pop special inside:

\setbox0{\begingroup\color{red}Red\endgroup}


because it is inserted after \endgroup before the box closes. LaTeX's \sbox does this automatically:

\sbox0{\color{red}Red}

• Just to be on the safe side, I added a \begingroup \endgroup pair after every appearance of the command \color in my document, and the warning still appeared. Must it then be a package that's causing this somehow behind the scenes? – Jonathan Gleason Jun 26 '16 at 13:54
• @JonathanGleason Yes, this can be in a package. The low level \setbox is seldom used in user code. Packages or classes are more likely. – Heiko Oberdiek Jun 26 '16 at 14:49
• @HeikoOberdiek tcolorbox: (it's not our fault:-) – David Carlisle Jun 26 '16 at 15:03