1

How to color the background of the paper in latex?

I want my document to have every other page coloring i.e., say I want the papers to be light blue and light pink alternatively, how can I do this?

Note that, I did not start my document yet, my document size will be about 6 or 8 pages. I prefer using document class book or article.

2
  • 1
    You can use \pagecolor (needs a color package). Sep 13, 2021 at 17:00
  • Can you provide details please @UlrikeFischer?
    – Happy
    Sep 13, 2021 at 17:26

1 Answer 1

4

You can tap into the shipout routine and change the colour based on the value of the page number:

enter image description here

\documentclass{article}

\usepackage{kantlipsum}
\usepackage{xcolor}

\AddToHook{shipout/before}{%
  \ifodd\value{page}
    \pagecolor{blue!5!white}% Odd page colour (light blue)
  \else
    \pagecolor{red!5!white}% Even page colour (light red)
  \fi
}

\begin{document}

\kant[1-50]

\end{document}
11
  • What is the purpose of shipout?
    – SolidMark
    Sep 13, 2021 at 17:16
  • What if I wanted to use the document class exam? I think the code should be the same also, am I correct?
    – Happy
    Sep 13, 2021 at 17:24
  • 1
    @MarkA.Bromuela: The OP wanted to switch between page colours. The shipout process is one that can be tapped into in order to condition on changing the colour.
    – Werner
    Sep 13, 2021 at 17:52
  • 1
    @Happy: The code should work with any document class as the process only relies on tapping into the page shipout procedure (which is class independent). You can change the colour to whatever you want. red!5!white means 5% red and 95% white. You can mix colours to suit your needs. Try, for example, red!10!white for a more red background colour. Also see the xcolor documentation for some more options and palettes.
    – Werner
    Sep 13, 2021 at 17:56
  • 1
    @BlackMild: This is typically because of the incorporation of some LaTeX3 code into the LaTeX kernel as of Oct 2020 that has not been updated on OverLeaf's side. Add \usepackage{xparse} to the preamble circumvent this. Check the lthooks documentation.
    – Werner
    Sep 14, 2021 at 16:09

You must log in to answer this question.

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