11

I have a one page document like below. I would like the background color would be green, then I would like change it to yellow. Is it possible?

\documentstyle{article}

\begin{document}

\pagecolor{green}
I would like the background color of the page until here would be green.\\

\pagecolor{yellow}
Then from now on, I would like the background would be yellow.

\end{document}
  • 2
    No. You can use a tikzpicture with a filled rectangle. – user11232 Apr 5 '15 at 15:31
  • Don't use \documentstyle. It is a LaTeX 2.09 command from more than 2 decades ago! – cfr Apr 5 '15 at 15:54
8

A command, which changes the page color from the current position does not need to know the exact location. Instead a large colored rectangle can be drawn. The following example moves \paperwidth to the left and draws a rectangle with width 2\paperwidth and height \paperheight. This covers the whole of the remaining page regardless of the current position on the page. An additional LaTeX run is not needed.

After drawing the rectangle, \pagecolor is used inside \afterpage to also get the real page color changed.

The command \changepagecolor supports the full syntax of \color or \pagecolor including the optional argument.

\documentclass{article}
\usepackage[a6paper, hmargin=12mm, vmargin=12mm, includefoot]{geometry}
\usepackage{lipsum}

\usepackage{color}
\usepackage{afterpage}

\makeatletter
% Macro \changepagecolor has the same syntax as \pagecolor or \color
% with an optional argument and a mandatory argument.
\newcommand*{\changepagecolor}{%
  \@ifnextchar[\@changepagecolor@i\@changepagecolor@ii
}
% Case: \changepagecolor[...]{...}
\def\@changepagecolor@i[#1]#2{%
  \@changepagecolor@do{[{#1}]{#2}}%
}
% Case: \changepagecolor{...}
\newcommand*{\@changepagecolor@ii}[1]{%
  \@changepagecolor@do{{#1}}%
}
\newcommand*{\@changepagecolor@do}[1]{%
  % Fill the remaining space with a colored rule
  \begingroup
    \offinterlineskip
    \hbox to 0pt{%
      \kern-\paperwidth
      \vtop to 0pt{%
        \color#1%
        \hrule width 2\paperwidth height \paperheight
        \vss
      }%
      \hss
    }%
  \endgroup
  % Set page color for the next page
  \afterpage{\pagecolor#1}%
}
\makeatother

\begin{document}
\pagecolor{green}
\lipsum[2]
\changepagecolor{yellow}
\lipsum[3]
\lipsum[4]
\end{document}

Page 1 Page 2

9

Here's a method which uses tikzmark and avoids the need to specify the height of each rectangle manually.

\documentclass{article}
\usepackage{tikz,kantlipsum}
\usetikzlibrary{tikzmark}
\begin{document}

\pagecolor{green}
\kant[1]

\tikzmark{here}
\begin{tikzpicture}[overlay, remember picture, inner sep=0pt, outer sep=0pt]
  \fill [yellow] (current page.west |- {pic cs:here}) rectangle (current page.south east);
\end{tikzpicture}

\kant[2]

\end{document}

green/yellow

7
\documentclass{article}
\usepackage{tikz}
\newcommand{\amount}{6in}   %%<---- adjust
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
  \fill[green] ([yshift=-\amount]current page.north west) rectangle (current page.north east);
  \fill[yellow] (current page.south west) rectangle ([yshift=-\amount]current page.north east);
\end{tikzpicture}
I would like the background color of the page until here would be green.\\

\vspace{4in}
Then from now on, I would like the background would be yellow.

\end{document}

enter image description here

4

Normally, a tikz picture has to be drawn AFTER all the \tikzmarks are defined, in which case they are drawn on top of the text, so for this problem one must be careful to work from top to bottom. However, using a page hook one can draw the tikz picture after the page is laid out but BEFORE it is shipped out, which puts the text on top.

\documentclass{article}
\usepackage{everypage}
\usepackage{tikzpagenodes}

\begin{document}
\tikz[remember picture,overlay]{\path coordinate(A);}%
I would like the background color of the page until here would be green.\\

Then from now on, I would like the background would be yellow.

\AddThispageHook{\begin{tikzpicture}[remember picture,overlay]
  \fill[color=green] (A -| current page.west) rectangle (current page.north east);
  \fill[color=yellow] (A -| current page.west) rectangle (current page.south east);
\end{tikzpicture}}
\end{document}

color background

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.