# Background color for documents generated by the preview package

My question is very simple: is it possible to use \pagecolor from xcolor (or related packages) to change the background color of a document generated by the preview package?

I've tried to add \pagecolor{cyan} to the preamble, to the document body, and within the different preview environments, with little success. I've also tried digging into the internals of both the preview and the xcolor packages — I suspect that when a page is built by preview, it interferes with xcolor changing the background color, but I haven't been able to pinpoint where the color suppression happens…

I'll keep searching in the following days and keep you updated. In the meantime, if anyone is also willing to look into this, I'll be extremely grateful!

Here is my M-non-WE, tested on Overleaf using pdfLaTeX and running TeXLive 2020:

\documentclass{standalone} % changed to standalone because cropping wasn't good
\usepackage{mwe}
\usepackage[active]{preview}
\usepackage{xcolor}

\pagecolor{cyan} % Doesn't change the preview page color.
% (I wouldn't expect it to, since it's not inside a preview environment.)

\begin{document}
%% The following isn't typeset, since preview is active.
\pagecolor{cyan} % Doesn't change the preview page color either.
% (Again, this is expected.)
\lipsum[1]

%% What's inside of preview environments gets typeset when preview is active.
\begin{preview}
\pagecolor{cyan} % Still doesn't change the preview page color...
% I would expect this one to work :-/
\lipsum[2]

\pdftexbanner
\end{preview}

\end{document}


With the following output:

### Edit:

I tried running the same code using XeLaTeX and the page color was in cyan! Unfortunately, due to restrictions on my side, I cannot use something other than pdfLaTeX.

My question now becomes: is it possible to do this consistently using any of pdfLaTeX, XeLaTeX and LuaLaTeX?

• I get cyan. How are you compiling? And is your tex system up-to-date? Dec 1 '20 at 21:53
• Hey there @Ulrike Fischer, thanks for your quick answer! I've updated my question and added an image to illustrate. I just tried it out on my system and it gives me the same white background… Dec 2 '20 at 1:29

With a current LaTeX (2020-10-01) your document works fine with pdflatex as with this format the \pagecolor command has been changed to use new LaTeX shipout hooks and doesn't get lost when preview is used (but there could be other problems with preview, e.g. https://lists.gnu.org/archive/html/bug-auctex/2020-11/msg00000.html).

With older formats you could try this.

\documentclass{standalone} % changed to standalone because cropping wasn't good
\usepackage{mwe}

\usepackage[active]{preview}
\usepackage{xcolor}
\newsavebox\pagecolorbox
\savebox\pagecolorbox{%
\makebox[0pt]{\raisebox{-\paperheight}[0pt][0pt]{%
\textcolor{cyan}{\rule{2\paperwidth}{\paperheight}}}}}

\AtBeginDvi{\box\pagecolorbox}

\begin{document}
\lipsum[1]

\begin{preview}
\lipsum[2]

\pdftexbanner
\end{preview}

\end{document}

• Wow, this works like a charm, thank you so much! Dec 2 '20 at 10:34