[Tex/LaTex] How to set colored ruled margins for some pages

colormargins

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.

Best Answer

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