# How to set colored ruled margins for different pages?

I want to write a LaTeX document, and I want to have document rules (colored margins distinguishing sections and/or even/odd pages).

Here is a model of what I have in mind:

How can this be accomplished?

I have seen this question which is the most relevant question to what I have in mind, however it doesn't quite accomplish my goal.

I have also searched around in Google, and the best I have come up with is used "framed" package, which again, does not accomplish this task. I am sure that this can be done, though.

-

Here is a possible solution based on the following packages:

Notice that two compilation runs are necessary to successfully color the margins.

Code:

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

\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}

\usepackage{lipsum} % dummy text

\begin{document}
\lipsum[1-30]
\end{document}


Result (just the first two pages are displayed):

-
When I run LaTeX, I get "/usr/local/texlive/2012/texmf-dist/tex/latex/background/background.sty:68: LaTeX Error: Unknown option opacity=1' for package background'." I am using MacTeX 2012, and running xelatex. –  Milad Naseri Sep 21 '12 at 14:16
@MiladNaseri I don't get any errors with Claudio's code (I am using TeX Live2012) using pdflatex or xelatex. Are you getting this error with the exact code he posted? –  Gonzalo Medina Sep 22 '12 at 1:32
@MiladNaseri: I also tried to compile my example with xelatex, but I don't have any errors. Is you MacTeX updated? –  Claudio Fiandrino Sep 22 '12 at 8:26

I think the tikz solution is great, and probably allows for lots of complicated patterns, but if you just want a simple bar then there's no need for it.

The solution below uses:

MWE

\documentclass{article}

\usepackage{xcolor}                 % to have colors
\usepackage{eso-pic}                % put things into background
\usepackage{lipsum}                 % for sample text
\usepackage{ifthen}

\AddToShipoutPicture{% from package eso-pic: put something to the background
\ifthenelse{\isodd{\thepage}}{
% ODD page: left bar
\AtPageLowerLeft{% start the bar at the left bottom of the page
\color{blue}\rule{2cm}{\LenToUnit\paperheight}%
}%
}%
{%
% EVEN page: right bar
\AtPageLowerLeft{% start the bar at the bottom right of the page
\put(\LenToUnit{\dimexpr\paperwidth-3cm},0){% move it to the top right
\color{orange}\rule{3cm}{\LenToUnit\paperheight}%
}%
}%
}%
}

\begin{document}
\lipsum

\lipsum

\lipsum

\end{document}


Here's a useful reference for the future:

Fixed marginpars with gray background in KOMA-Script

-

No answers with fancyhdr and tikz!. Let me jump in:

\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

\pagestyle{fancy}
\fancyhf{}
\begin{tikzpicture}[overlay,remember picture]
\fill [color=blue]
(current page.north west)
rectangle
($(current page.south west) + (1cm,0cm)$);
\end{tikzpicture}
}
\begin{tikzpicture}[overlay,remember picture]
\fill [color=orange]
(current page.north east)
rectangle
($(current page.south east) + (-1cm,0cm)$);
\end{tikzpicture}
}
\fancyfoot[C]{\thepage}