5

I need to be able to change the background color of a document and the pagecolor package seems like a good fit. However, what I also need is a command that allows me to color all pages that start at a certain page number (think about highlighting a document's pages that are over the official page limit of N pages). It is probably a simple newcommand two-liner, but I don't get it. Does anyone know?

5

You could load the atbegshi package and use that package's \AtBeginShipout macro to switch on page coloring from a certain page onwards.

\documentclass{article}
\usepackage{pagecolor}
\usepackage{atbegshi} % for '\AtBeginShipout' macro
% Make page color "light red" *after* page 2
\AtBeginShipout{\ifnum\value{page}=2\pagecolor{red!20}\fi}
\usepackage{lipsum}
\begin{document}
\lipsum[1-20] % filler text
\end{document}
  • Thank you! That works perfectly. I have never heard of the atbegshi package before! – Erdbeere Feb 24 '16 at 13:44
2

Based on my answer at What are the ways to position things absolutely on the page?, but using \AddEverypageHook instead of \AddThispageHook to place a full-page \rule in the background of the page.

\documentclass{article}
\usepackage{everypage}
\usepackage{xcolor}
\usepackage{lipsum}
% THESE ARE LaTeX DEFAULTS; CAN CHANGE IF NEEDED.
\def\PageTopMargin{1in}
\def\PageLeftMargin{1in}
\newcommand\ateveryxy[3]{%
 \AddEverypageHook{\smash{\hspace*{\dimexpr-\PageLeftMargin-\hoffset+#1\relax}%
  \raisebox{\dimexpr\PageTopMargin+\voffset-#2\relax}{#3}}}}
% VERIFIED THAT SETTING \hoffset AND \voffset DO NOT BREAK SOLUTION.
%\hoffset=0.4in
%\voffset=0.2in
\def\BGcolor{red!10}
\begin{document}
\lipsum[1-17]
% COMMENCE COLORED PAGES HERE
\ateveryxy{0in}{\paperheight}{\textcolor{\BGcolor}{\rule{\paperwidth}{\paperheight}}}
\lipsum[17-29]
\def\BGcolor{blue!10}% CHANGE THE COLOR HERE
\lipsum[30-34]
\end{document}
  • Thank you as well. I got it working, though the previous answer is much shorter, I will stick with it. – Erdbeere Feb 24 '16 at 13:45
2

The following suggestion does make sense only if you already use scrlayer-scrpage as package for header and footer. Then you can define a new background layer and add this to all layer page styles including empty.

\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{scrlayer-scrpage}
\DeclareNewLayer[
  background,
  contents={\ifnum\value{page}>3\color{orange!20}\rule{\layerwidth}{\layerheight}\fi}
]{pagecolor}
\AddLayersAtBeginOfPageStyle{@everystyle@}{pagecolor}

\begin{document}
\Blinddocument
\end{document}

Or you could use option onpsinit

\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{scrlayer-scrpage}
\KOMAoptions{onpsinit={\ifnum\value{page}=3\pagecolor{orange!10}\fi}}
\begin{document}
\Blinddocument
\end{document}

In both cases the colored background starts on page 4.

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.