How to add a custom color to every page header

colorheader-footer

Here is a typical page from my notes

enter image description here

I would like to add a side coloring unique to every section. For example, for the 3rd section I want to have a blue colour as

enter image description here

Ideally I would also want to be able to have a fixed distance between the text and the colouring (and the sides of the paper).

Note that I have headings for every single page which include the subsection number and the name of the section. For example, in the above we have section "This is sign of a permutation" and the current page consists of the third subsection.

I have so far used fancy header but I can't seem to add this feature. Does anybody have any thoughts on how I can achieve this?

If you need any more information about my current set-up please let me know.

Here is my template

\documentclass[12pt]{article}

 

\usepackage{fancyhdr,lipsum}
\usepackage{eso-pic,graphicx}
\usepackage[a4paper,bindingoffset=0in,%
            left=0.5in,right=0.5in,top=0.8in,bottom=1in,%
            footskip=.5in]{geometry}

\newcommand{\newsection}[1]{%
  \fancyhead[C]{\bfseries\LARGE \thesection~#1}% Clear
}

\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\usepackage{seqsplit}
\usepackage{libertine}
\usepackage{amsmath} % Imports amsmath
\usepackage{blindtext}
\usepackage{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{physics} %Allows for differentials
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}
\usepackage{enumitem}
\usepackage{empheq}
\usepackage{graphicx,wrapfig,lipsum}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{color}
\usepackage{tikz}
\usepackage{eso-pic,graphicx}
\usepackage{xcolor,soul}
\usepackage{xpatch}

\makeatletter
\AtBeginDocument{\xpatchcmd{\@thm}{\thm@headpunct{.}}{\thm@headpunct{}}{}{}}
\makeatother


%Defines the Customr Colors
\definecolor{bl2}{HTML}{0b4973} %These are the blue colours
\definecolor{bl1}{HTML}{dce8eb}
\definecolor{g1}{HTML}{dcebdd} %These are the green colours
\definecolor{g2}{HTML}{0b731c}
\definecolor{r1}{HTML}{ebdcdc} %These are the red colours
\definecolor{r2}{HTML}{730b0b}
\definecolor{br1}{HTML}{ebe8dc} %These are the brown colours
\definecolor{br2}{HTML}{734b0b}
\definecolor{br3}{HTML}{4c3c16}

\definecolor{p2}{HTML}{520b73}
\definecolor{p1}{HTML}{664f71}

%Defines the Custom Color Boxes
%1.1 Theorem - Blue
\newtcolorbox{bluetheorem}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=bl2,
title code={
\path[fill=bl1] (title.south west) rectangle (title.north east);
\path[draw=bl2,solid,line width=0.75mm]
([xshift=5mm]title.south west) -- ([xshift=-5mm]title.south east);
},
boxrule=0pt,frame hidden,
borderline west={4pt}{0pt}{bl2},
colback=bl1,
colbacktitle=bl1,
opacityback=0.6
}

%1.2 Definition - Blue
\newtcolorbox{bluedefinition}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=bl2,
boxrule=0pt,frame hidden,
opacityback=0,
%borderline west={10pt}{5pt}{blue,opacity=0.5},
overlay={
      \draw[line width=10pt,bl2,opacity=0.5]
          ([shift={(0mm,-2mm)}]interior.north west)
          --([shift={(0mm,2mm)}]interior.south west);
        }
}

%2.1 Theorem - Green
\newtcolorbox{greentheorem}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=g2,
title code={
\path[fill=g1] (title.south west) rectangle (title.north east);
\path[draw=g2,solid,line width=0.75mm]
([xshift=5mm]title.south west) -- ([xshift=-5mm]title.south east);
},
boxrule=0pt,frame hidden,
borderline west={4pt}{0pt}{g2},
colback=g1,
colbacktitle=g1,
opacityback=0.6
}

%2.2 Definition - Green
\newtcolorbox{greendefinition}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=g2,
boxrule=0pt,frame hidden,
opacityback=0,
%borderline west={10pt}{5pt}{blue,opacity=0.5},
overlay={
      \draw[line width=10pt,g2,opacity=0.5]
          ([shift={(0mm,-2mm)}]interior.north west)
          --([shift={(0mm,2mm)}]interior.south west);
        }
}

%3.1 Theorem - Red
\newtcolorbox{redtheorem}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=r2,
title code={
\path[fill=r1] (title.south west) rectangle (title.north east);
\path[draw=r2,solid,line width=0.75mm]
([xshift=5mm]title.south west) -- ([xshift=-5mm]title.south east);
},
boxrule=0pt,frame hidden,
borderline west={4pt}{0pt}{r2},
colback=r1,
colbacktitle=r1,
opacityback=0.6
}

%3.2 Definition - Red
\newtcolorbox{reddefinition}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=r2,
boxrule=0pt,frame hidden,
opacityback=0,
%borderline west={10pt}{5pt}{blue,opacity=0.5},
overlay={
      \draw[line width=10pt,r2,opacity=0.5]
          ([shift={(0mm,-2mm)}]interior.north west)
          --([shift={(0mm,2mm)}]interior.south west);
        }
}

%4.1 Theorem - Brown
\newtcolorbox{browntheorem}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=br2,
title code={
\path[fill=br1] (title.south west) rectangle (title.north east);
\path[draw=br2,solid,line width=0.75mm]
([xshift=5mm]title.south west) -- ([xshift=-5mm]title.south east);
},
boxrule=0pt,frame hidden,
borderline west={4pt}{0pt}{br2},
colback=br1,
colbacktitle=br1,
opacityback=0.6
}

%4.2 Definition - Brown
\newtcolorbox{browndefinition}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=br2,
boxrule=0pt,frame hidden,
opacityback=0,
%borderline west={10pt}{5pt}{blue,opacity=0.5},
overlay={
      \draw[line width=10pt,br2,opacity=0.5]
          ([shift={(0mm,-2mm)}]interior.north west)
          --([shift={(0mm,2mm)}]interior.south west);
        }
}

%5.1 Theorem - Purple
\newtcolorbox{purpletheorem}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=p2,
title code={
\path[fill=p1] (title.south west) rectangle (title.north east);
\path[draw=p2,solid,line width=0.75mm]
([xshift=5mm]title.south west) -- ([xshift=-5mm]title.south east);
},
boxrule=0pt,frame hidden,
borderline west={4pt}{0pt}{p2},
colback=p1,
colbacktitle=p1,
opacityback=0.6
}

%5.2 Definition - Purple
\newtcolorbox{purpledefinition}{
enhanced,
fonttitle=\sffamily\bfseries,
coltitle=p2,
boxrule=0pt,frame hidden,
opacityback=0,
%borderline west={10pt}{5pt}{blue,opacity=0.5},
overlay={
      \draw[line width=10pt,p2,opacity=0.5]
          ([shift={(0mm,-2mm)}]interior.north west)
          --([shift={(0mm,2mm)}]interior.south west);
        }
}

\newtheoremstyle{break}
  {\topsep}{\topsep}%
  {\upshape}{}%
  {\bfseries}{}%
  {\newline}{}%

\theoremstyle{break}

\newtheorem{theorem}{Theorem}[section] %This defines the \begin{Theorem} commands
\newtheorem{corollary}{Corollary}[theorem] %This defines the \begin{Corollary} commands
\newtheorem{lemma}[theorem]{Lemma} %This defines the \begin{Lemma} commaands and formating


\theoremstyle{definition} %This changes the theorem style. In this case, we want the following theorem to have style of a definition as it won't change the formating.
\newtheorem*{example}{Example}
\newtheorem*{remark}{Remark}

\renewcommand{\baselinestretch}{0.9} % This deals with the dimentions of the page.



\title{Algebra and Geometry} % Title

\usepackage{hyperref}

\hypersetup{
    colorlinks=false, %set true if you want colored links
  linktoc=all,     %set to all if you want both sections and subsections linked
    linkcolor=blue,  %choose some color if you want links to stand out
}

\newcommand{\tcb}[1]{\textcolor{bl2}{#1}} %Use this for colouring blue text
\newcommand{\inc}[2]{\includegraphis[#1]{#2}} %shorthand for includegraphis


\begin{document}


\AddToShipoutPictureBG*{\includegraphics[width=\paperwidth,height=\paperheight]{whitelinesa1.png}}

\maketitle

\tableofcontents
\newpage
\newsection{Template} 
\sethlcolor{bl1} %For the first chapter (here in the commands we have \newsection) we will use blue.

\AddToShipoutPictureBG*{\includegraphics[width=\paperwidth,height=\paperheight]{whitelinesblue.png}}


\section{Commands}

\begin{flushleft}

 
testing

\end{flushleft}



\end{document}

End Result with Bellow Answer
With the bellow solution, here is a screen shot from my notes

enter image description here

Best Answer

The gaps are currently set to 0.1in each. Note the extra 0.1in gaps at the edges to accommodate the oversized border art.

\documentclass[]{article}
\usepackage{xcolor}
\usepackage[a4paper,bindingoffset=0in,%
            left=0.5in,right=0.5in,top=0.8in,bottom=1in,%
            footskip=.5in]{geometry}
\usepackage{graphicx}
\usepackage{lipsum}

\newsavebox{\titlebox}

\makeatletter
\def\ps@mystyle{\def\@oddfoot{\hfill\thepage\hfill}% fancyhdr is for sissies
  \let\@evenfoot\@oddfoot
  \def\@evenhead{\hspace{0.1in}\hfill
    \textcolor{blue}{\rule{\dimexpr 0.5\textwidth-0.3in-0.5\wd\titlebox}{\ht\titlebox}}\hfill
    \usebox\titlebox\hfill
    \textcolor{blue}{\rule{\dimexpr 0.5\textwidth-0.3in-0.5\wd\titlebox}{\ht\titlebox}}\hfill
    \hspace{0.1in}}%
  \let\@oddhead=\@evenhead
  \def\sectionmark##1{\global\setbox\titlebox=\hbox{\bfseries\LARGE \thesection~##1}}%
}
\makeatother
\pagestyle{mystyle}
\AddToHook{shipout/background}{% no package needed (other than graphicx)
  \put(0.1in,-0.6in){\includegraphics[width=0.5in,height=0.5in]{example-image-a}}
  \put(\dimexpr \paperwidth-0.6in,-0.6in){\includegraphics[width=0.5in,height=0.5in]{example-image-b}}
}

\begin{document}
\section{This is sign of permutation}
\lipsum[1-4]
\end{document}

This version uses TikZ. The tricky bit was trying to align the baselines instead of the centers. It also uses \rightmark instead of a global \titlebox. If you have two \sections on one page, \rightmark will show the first while \titlebox will show the second.

\autocolor selects a different color for each page (until it runs out of \ors).

\documentclass[]{article}
\usepackage{xcolor}
\usepackage[a4paper,bindingoffset=0in,%
            left=0.5in,right=0.5in,top=0.8in,bottom=1in,%
            footskip=.5in]{geometry}
\usepackage{tikz}
\usepackage{lipsum}

\newsavebox{\titlebox}
\newcommand{\autocolor}{\ifcase\value{page}\relax white\or red\or green\or blue\else white\fi}

\makeatletter
\def\ps@mystyle{\def\@oddfoot{\hfill\thepage\hfill}% fancyhdr is for sissies
  \let\@evenfoot\@oddfoot
  \def\@evenhead{\savebox\titlebox{\rightmark}\hfill\tikz
    [every node/.style={inner sep=0pt}]
    {\node[anchor=base] (title) {\usebox\titlebox};
    \node[fill={\autocolor},opacity=0.5,
      minimum height=\ht\titlebox,
      minimum width={0.5\textwidth-0.3in-0.5\wd\titlebox},
      left=0.5em, yshift=0.5\dp\titlebox] at (title.west){};
    \node[fill={\autocolor},opacity=0.5,
      minimum height=\ht\titlebox,
      minimum width={0.5\textwidth-0.3in-0.5\wd\titlebox},
      right=0.5em, yshift=0.5\dp\titlebox] at (title.east){};
    }\hfill}% end of \@evenhead
  \let\@oddhead=\@evenhead
  \def\sectionmark##1{\markright{\bfseries\LARGE \thesection~##1}}%
}
\makeatother
\pagestyle{mystyle}
\AddToHook{shipout/background}{% no package needed (other than graphicx)
  \put(0.1in,-0.6in){\includegraphics[width=0.5in,height=0.5in]{example-image-a}}
  \put(\dimexpr \paperwidth-0.6in,-0.6in){\includegraphics[width=0.5in,height=0.5in]{example-image-b}}
}

\begin{document}
\section{This is sign of permutation}
\lipsum[1-4]
\end{document}