I understand one can easily change the background color for text portion easily, but I would like to do the same for an entire portion of the page. How does one change background color for only a section of the page?
-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.– karlkoellerCommented Dec 31, 2013 at 8:43
-
3Do the answers in Setting background colour for part of a page help?– karlkoellerCommented Dec 31, 2013 at 8:48
-
1Depending on how you wanted to use it, the different colored portion of the page might be done with a colorbox'ed minipage. It would help if your question showed an example of what you are trying to achieve, and what you've attempted so far.– Steven B. SegletesCommented Jan 8, 2014 at 14:18
-
1please see the answer below if it suits the requirement -- adapted from my answer here -- tex.stackexchange.com/a/527919/197451– js bibraCommented Feb 27, 2021 at 1:21
4 Answers
\documentclass{article}
\pagestyle{empty} % Suppress page numbers
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\path (current page.north west)
node[
below right,
fill=blue!40,
minimum width=\paperwidth,
minimum height=3cm
]
(box){};
\path (current page.south west)
node[
above right,
fill=red!40,
minimum width=\paperwidth,
minimum height=3cm
]
(box1){};
\path ($(current page.west)+(1cm,0)$)
node[
%above right,
fill=black!40,
%minimum width=\paperwidth,
minimum height=1cm
]
(box2){};
\path ($(box2)+(1cm,0)$)
node[
%above right,
fill=green!40,
%minimum width=\paperwidth,
minimum height=1cm
]
(box3){};
%\path (box.west) node[right=5mm,align=left] %<distance can be changed to suit
%{{\fontsize{45pt}{65pt}\color{white}\textbf{JS Bibra}}\\[2mm]
%{\fontsize{30pt}{20pt}\color{cyan}Advisor to TEX}\\[2mm]
%{\fontsize{10pt}{10pt}\color{white}\LaTeX\ development}};
\end{tikzpicture}
\end{document}
The following example uses this approach:
Mark or save the top and bottom positions of the band you want to highlight using
zref
'ssavepos
module (via\zsaveposy{<label>}
);Insert content into the
B
ackG
round of the current page only usingeso-pic
's\AddToShipoutPictureBG*
;That content is a rectangle based on
\rule[<raise>]{<width>}{<height>}
.
\documentclass{article}
\usepackage{zref-savepos,eso-pic,xcolor}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\AddToShipoutPictureBG*{% Add the following in the background of the current page only
\AtPageLowerLeft{% Starting from the lower left corner of the page
\color{blue!30!white}% Colour content this colour
\rule% Insert a rule (technically a filled rectangle)...
[\dimexpr\zposy{bottom}sp-.3\baselineskip]% ...raised to the "bottom" marker
{\paperwidth}% ...stretching across the entire page
{\dimexpr\zposy{top}sp-\zposy{bottom}sp}% ...with height "top"-"bottom"
}%
}%
\zsaveposy{top}%
\lipsum*[2]%
\zsaveposy{bottom}%
\lipsum[3]
\end{document}
Since \zsaveposy
uses the \label
-\ref
system, you'll have to compile at least twice with every change in the vertical position of top
and/or bottom
.
You can automate the process into an environment colourband
:
\documentclass{article}
\usepackage{zref-savepos,eso-pic,xcolor}
\usepackage{lipsum}
%\usepackage{xparse}% Only if you're running LaTeX older than 2020-10
\newcounter{colourband}%
\NewDocumentEnvironment{colourband}{m}{%
\stepcounter{colourband}% New band
\leavevmode\zsaveposy{top-\thecolourband}% Start new paragraph and save y-position of top
\ignorespaces
}{%
\unskip
\zsaveposy{bottom-\thecolourband}% Save y-position of bottom
% Add colour band to BackGround of current page only
\edef\x{\noexpand\AddToShipoutPictureBG*{%
\noexpand\AtPageLowerLeft{% Starting from the lower left corner of the page
\noexpand\color{#1}% Colour content this colour
\noexpand\rule% Insert a rule (technically a filled rectangle)...
[\dimexpr\zposy{bottom-\thecolourband}sp-.3\baselineskip]% ...raised to the "bottom" marker
{\paperwidth}% ...stretching across the entire page
{\dimexpr\zposy{top-\thecolourband}sp-\zposy{bottom-\thecolourband}sp+\baselineskip}% ...with height "top"-"bottom"
}%
}}\x%
}
\begin{document}
\lipsum[1]
\begin{colourband}{blue!20!white}
\lipsum*[2]%
\end{colourband}%
\begin{colourband}{green!15!white}
\lipsum*[3]
\end{colourband}
\end{document}
If the colored background is related with a particular text, you can use a tcolorbox
for this.
\documentclass[a4paper, 12pt]{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox{colbackbox}[2][]{%
tile,
colback=#2,
size=minimal, left=\oddsidemargin+1in,right=\oddsidemargin+1in, spread sidewards,
parbox=false, before upper=\indent, after=\par, #1}
\begin{document}
\lipsum[1]
\begin{colbackbox}{red!30}
\lipsum[2]
\end{colbackbox}
\begin{colbackbox}[breakable]{blue!15}
\lipsum[3-6]
\end{colbackbox}
\end{document}
But if it's just a frame on any position you can use eso-pic
package:
\documentclass{report}
\usepackage{xcolor}
\usepackage{eso-pic}
\usepackage{lipsum}
\begin{document}
\AddToShipoutPictureBG{\AtPageLowerLeft{%
\put(0,50mm){%
\color{blue!20}\rule{\paperwidth}{.3\paperheight}}}}
\lipsum[1-15]
\end{document}
You can change the background color of a page using the \pagecolor{color name} command. The code to change the color of background is given below:
\documentclass[12pt]{article}
\usepackage{xcolor, soul}
\begin{document}
\fcolorbox{red}{white}{The text is the simple \LaTeX\ text}\\ \\
\pagecolor{lightgray}
\fcolorbox{blue}{cyan}{text is here}
\end{document}
If you want to revert back to original pagecolor, Just add this command
\pagecolor{white}