[Tex/LaTex] Wrap page number in colored box in footer

fancyhdrheader-footerpage-numbering

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

Desired outcome

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}
\usepackage[a4paper,left=4.5cm,right=3cm,top=2.5cm,bottom=2cm,includeheadfoot]{geometry}

% Header and footer
\fancyhf{}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{\thesection.\ #1}{}}
\renewcommand{\headrulewidth}{0.3pt}
\renewcommand{\footrulewidth}{0.3pt}
\chead{\small{\leftmark}}
\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:

Current 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.

Best Answer

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[a4paper,left=4.5cm,right=3cm,top=2.5cm,bottom=2cm,includeheadfoot]{geometry}
\usepackage{xcolor}

% header and footer
\usepackage[automark,headsepline=.3pt,footsepline=.3pt]{scrlayer-scrpage}
\clearpairofpagestyles
\chead{\leftmark}
\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:
\AddLayersToPageStyleBeforeLayer{scrheadings}{pagenumber.bg}{scrheadings.foot.oneside}

% fonts
\addtokomafont{pageheadfoot}{\small\upshape}
\addtokomafont{pagenumber}{\bfseries\color{white}}% <- white pagenumber

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

enter image description here


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[a4paper,left=4.5cm,right=3cm,top=2.5cm,bottom=2cm,includeheadfoot]{geometry}
\usepackage{xcolor}

%Header and footer
\usepackage[automark,headsepline=.3pt,footsepline=.3pt]{scrlayer-scrpage}[2015/02/07]
\clearpairofpagestyles
\chead{\leftmark}
\ifoot{Author name}

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

% fonts
\addtokomafont{pageheadfoot}{\small\upshape}
\addtokomafont{pagenumber}{\bfseries\color{white}}% <- white pagenumber

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