6

How this can be done for ALL pages is explained in this thread: How to set colored ruled margins for different pages?

But I would like to know if it is possible to color the margins only for some pages? How would you do that?

So pages 1 to x no colors, then y pages of color and then no colors again.

3 Answers 3

7

You can define a pagestyle and use it when needed.

\documentclass[twoside]{article}

\usepackage{tikz}               % you know what this does!
\usetikzlibrary{calc}

\usepackage{fancyhdr}         % put things headers and footers and we plan misuse it ;)
\usepackage{lipsum}           % for sample text 

\fancypagestyle{mypage}{%
    \fancyhf{}
    \fancyhead[LO]{%
        \begin{tikzpicture}[overlay,remember picture]
            \fill [color=blue] (current page.north west) rectangle
                ($ (current page.south west) + (1cm,0cm) $);
        \end{tikzpicture}
        }
    \fancyhead[RE]{%
        \begin{tikzpicture}[overlay,remember picture]
            \fill [color=orange](current page.north east) rectangle
                ($ (current page.south east) + (-1cm,0cm) $);
        \end{tikzpicture}
        }
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

\begin{document}
\pagestyle{mypage}   % activate colored margins
\lipsum[1-8]

\clearpage
\pagestyle{plain}    % deactivate colored margins
\lipsum[1-8]

\clearpage
\pagestyle{mypage}   % activate colored margins
\lipsum[1-8]

\end{document}

enter image description here

1
  • This works best ! Commented Feb 8, 2021 at 12:43
6

Using the settings of the answer to the question linked, you can use a conditional to activate or deactivate the margins at will:

\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{ifthen}
\usepackage[contents={},opacity=1,scale=1.485]{background}

\newif\ifBgMat

\AddEverypageHook{%
\ifBgMat
\ifthenelse{\isodd{\thepage}}%
{\backgroundsetup{angle=0,position={0.9\textwidth,-
.7\textheight},%
contents={\tikz[remember picture,overlay]{ %
\coordinate (x) at (current page marginpar area.south east|-current page.south east);
\draw[draw=none,fill=magenta!20]([xshift=-\textwidth]x)rectangle(current page.north west);}}}}%
{\backgroundsetup{angle=0,position={0.9\textwidth,-
.7\textheight},%
contents={\tikz[remember picture,overlay]{ %
\coordinate (x) at (current page marginpar area.south east|-current page.south east);
\draw[draw=none,fill=orange!20](x)rectangle(current page.north east);}}}}%
\BgMaterial
\else
\fi}

\usepackage{lipsum} % dummy text
\BgMattrue% activate colored margins
\begin{document}
\lipsum[1-8]

\clearpage
\BgMatfalse% deactivate colored margins
\lipsum[1-8]

\clearpage
\BgMattrue% activate colored margins
\lipsum[1-8]
\end{document}

enter image description here

1
  • Thank you the example works perfectly for the article document class but not for the revtex4-1 document class which I have to use.
    – Enders
    Commented Jul 31, 2013 at 7:02
1

I like the answer of Gonzalo but I prefer etoolbox (instead of ifthen, which is obsolete) and eso-pic (instead of background).

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{etoolbox} % provides if-else-conditionals
\usepackage{eso-pic} % provides the \AddToShipoutPictureBG command which adds content to background of every page

\newcommand{\marginLeft}{%
    \begin{tikzpicture}[remember picture,overlay]
        \fill [magenta!20] ($ (current page.north west) + (-0.3cm,0.3cm) $) rectangle ($ (current page.south west) + (1cm,-0.3cm) $);
    \end{tikzpicture}%
}
\newcommand{\marginRight}{%
    \begin{tikzpicture}[remember picture,overlay]
        \fill [orange!20] ($ (current page.north east) + (0.3cm,0.3cm) $) rectangle($ (current page.south east) + (-1cm,-0.3cm) $);
    \end{tikzpicture}%
}

\newtoggle{margins} % default: deactivated

\AddToShipoutPictureBG{% this command adds content to background of every page
    \iftoggle{margins}{% if margins-toggle is activated...
        \ifnumodd{\thepage}{% if page number is odd
            \marginLeft % print margin on the left side
        }{% else (if page number is even)
            \marginRight % print margin on the right side
        }
    }{ % else (if margins-toggle is deactivated)
       % print nothing
     }
}

\usepackage{lipsum} % dummy text

\begin{document}

\toggletrue{margins} % activate colored margins
\lipsum[1-8]

\clearpage
\togglefalse{margins} % deactivate colored margins
\lipsum[1-8]

\clearpage
\toggletrue{margins} % activate colored margins
\lipsum[1-8]

\end{document}

You must log in to answer this question.

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