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
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}
- 41,473
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}
- 14,890
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}
- 1,489
\nopagecoloris 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 08 '11 at 05:35afterpage! (for real now, upvote did not work yesterday?!) – Stephen Mar 13 '12 at 19:17\usepackage[pagecolor=none]{pagecolor}and invoking with\newpagecolor{yellow}\afterpage{\restorepagecolor}works with bothxelatexandlualatex. – chandra Mar 25 '12 at 09:02xelatexissued a warning but gave expected results.xelatexstill appears to be unsupported by the package. – chandra Mar 25 '12 at 11:16