68

How can I change the background color only for the current page? I found that I can change the color with \pagecolor{color} but that unfortunately changes the color for all pages after this command. Any ideas how to limit this command only to the current page?

3 Answers 3

68

You could use the \afterpage command from the afterpage package to change the color back.

Here's an example:

\documentclass{article}
\usepackage{lipsum}% for auto generating text
\usepackage{afterpage}
\usepackage{xcolor}
\begin{document}
\lipsum[1-12]
\pagecolor{yellow}\afterpage{\nopagecolor}
\lipsum[22-30]
\end{document}
5
  • 7
    @Lev: \nopagecolor is only available for pdfTeX and LuaTeX. However, dvips, dvipdfm(x) and XeTeX do not support it. So I don't think it is very good.
    – Leo Liu
    Aug 8, 2011 at 5:35
  • 2
    +1 for afterpage! (for real now, upvote did not work yesterday?!)
    – Stephen
    Mar 13, 2012 at 19:17
  • 1
    @Leo Liu: I have found that loading \usepackage[pagecolor=none]{pagecolor} and invoking with \newpagecolor{yellow}\afterpage{\restorepagecolor} works with both xelatex and lualatex.
    – chandra
    Mar 25, 2012 at 9:02
  • @Leo Liu: Oops! Looks like xelatex issued a warning but gave expected results. xelatex still appears to be unsupported by the package.
    – chandra
    Mar 25, 2012 at 11:16
  • @chandra: Yes, I wanted to state that with my answer. Sorry if it was not clear.
    – Stephen
    Jan 23, 2013 at 19:17
29

To circumvent the issue of \nopagecolor being only available for pdfTeX and LuaTeX, you could use the pagecolor package and \newpagecolor{yellow} to set the new page color and \restorepagecolor to restore the pagecolor to the one it was before \newpagecolor{yellow} was used. If that color was white, it is white again, if it was none, it is none again, and if it was e.g. green, it is green again. To make the switch back to the old page colour automatically, \afterpage as suggested by frabjous can be used.

\documentclass{article}
\usepackage{lipsum}% for auto generating text
\usepackage{afterpage}% for "\afterpage"
\usepackage{xcolor}
\usepackage{pagecolor}% With option pagecolor={somecolor or none}
% you can set a page colour here.
% "none" is the default, but if \nopagecolor is not available, "white" will be used.
\begin{document}
\lipsum[1-12]
\newpagecolor{yellow}\afterpage{\restorepagecolor}
\lipsum[22-30]
\end{document}
12

Another option is to just use \pagecolor{yellow} followed by \newpage followed by another \pagecolor{white} (or whatever color you want).

\documentclass{article}
\usepackage{lipsum}% for auto generating text
\usepackage{xcolor}
\begin{document}
\lipsum[1-12]
\pagecolor{yellow}
\newpage
\pagecolor{white}
\lipsum[22-30]
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .