5

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.

5

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.

2
  • In the first version your request a specific version for scrlayer-scrpage. Why? – user3783960 May 25 '15 at 11:48
  • 1
    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
4

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{\headrulewidth}{0.3pt}
\renewcommand{\footrulewidth}{0.3pt}
\chead{\small{\leftmark}}
\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}

enter image description here

1
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.