# Wrap page number in colored box in footer

I a looking for a way to achieve the following outcome for the page numnbers in my footer created using fancyhdr:

My current code in a stand-alone example looks like this:

\documentclass[12pt,a4paper,parskip=full]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{lipsum}
\usepackage{fancyhdr}

\fancyhf{}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{\thesection.\ #1}{}}
\renewcommand{\footrulewidth}{0.3pt}
\lfoot{\small{Author name}}
\rfoot{\small{\thepage}}

\begin{document}

\pagestyle{fancy}

\section{Section One}

\lipsum

\clearpage

\section{Section Two}

\lipsum

\end{document}


This of course leads to a pretty much standard outcome:

The closest thing I found in the fancyhdr documenatation are the thumb-indexes however I am not sure how to adapt them for this.

Is there an easy way to do this or would it require a lot of custom drawing with tikz?

I appreciate all hints.

Because of you are using the KOMA-Script class scrartcl I would suggest to use scrlayer-scrpage instead fancyhdr. Then you can define and insert a new layer for the background of the pagenumber.

\documentclass[12pt,numbers=enddot,parskip=full]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{xcolor}

\clearpairofpagestyles
\ifoot{Author name}

\newcommand\pagenumberwidth{1cm}% <- width of the black box
\ofoot{\makebox[\pagenumberwidth]{\pagemark}}

% new layer for the pagenumber background:
\DeclareNewLayer[
clone=scrheadings.foot.oneside,% <- clone the foot layer
contents={\parbox{\layerwidth}{\raggedleft\rule{\pagenumberwidth}{\baselineskip}}}%<- black rule
]{pagenumber.bg}
% add the new layer to the pagestyle before the foot layer:

% fonts

\begin{document}
\Blinddocument
\Blinddocument
\end{document}


Or you could add the black box to the layer of the headsepline and move this layer to the background:

\documentclass[12pt,numbers=enddot,parskip=full]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{xcolor}

\clearpairofpagestyles
\ifoot{Author name}

\newcommand\pagenumberwidth{1cm}% <- width of the black box
\ofoot{\makebox[\pagenumberwidth]{\pagemark}}
\ModifyLayer[
background,
\nobreak\makebox[0pt][r]{\smash{\raisebox{-\height}{%
\parbox{\layerwidth}{\raggedleft\rule{\pagenumberwidth}{\baselineskip}}%
}}}}

% fonts

\begin{document}
\Blinddocument
\Blinddocument
\end{document}


The result is the same as above.

Note, that the option addcontents was implemented in KOMA-Script version 3.16 (2015/02/07). So you need this or a newer version to run the second example.

• In the first version your request a specific version for scrlayer-scrpage. Why? – user3783960 May 25 '15 at 11:48
• Oh, sorry: The first example does not need a specific scrlayer-scrpage version. But the second example needs version 3.16 or newer because addcontents was implemented in this version. – esdd May 25 '15 at 21:10

My first thought would be to use TikZ, but you don't need TikZ if you're willing to load the xcolor package.

Here's my poor man's version:

\documentclass{article}

\usepackage{xcolor}
\usepackage{fancyhdr}
\fancyhf{}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{\thesection.\ #1}{}}
\renewcommand{\footrulewidth}{0.3pt}
\lfoot{\small{Author name}}
\rfoot{\mypage}

\usepackage{lipsum}

\newcommand\mypage{%%
\raisebox{2 pt}[0pt][0pt]{%%
\rule[-1ex]{1cm}{3.5ex}%%
\hspace*{-0.5cm}%%
\makebox[0pt]{\textcolor{white}{\small\thepage}}%%
\hspace*{0.5cm}}%%
}

\begin{document}

\lipsum[1-140]

\end{document}


• @A. Ellett I'm not a tikz expert can you put the code of how will you do the same thing using tikz Thanks!!! – Very23 May 31 '15 at 19:05