10

I can achieve this for the regular article class and the fancyhdr package according to an answer to question: How can I add "page # of ##" on my document?

\usepackage{fancyhdr}
\pagestyle{fancy}
%\renewcommand{\headrulewidth}{0pt}
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}
\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \fancyfoot[C]{\thepage\ of \pageref{LastPage}}}

unfortunately this fancyhdr clashes with the scrartcl class and I get a recommendation to use another KOMA-Script package instead of fancyhdr. Do you know what the analogue of the code above looks like for KOMA-Script?

Faheem Mitha
  • 7,778

2 Answers2

11

With a KOMA-Script class this can be done without a package for header and footer. You only have to redefine the KOMA-Script command \pagemark:

\documentclass[12pt]{scrartcl}
\usepackage{blindtext}
\usepackage{lastpage}
\renewcommand\pagemark{{\usekomafont{pagenumber}\thepage\ of \pageref{LastPage}}}

\author{Author}
\title{Title}
\begin{document}
\maketitle
\section{Testing}
\blinddocument
\end{document}

enter image description here

It is also possible to add lines below the header and above the footer. If you set pagestyle headings you will get the sections in header.

\documentclass[12pt,
  headsepline,footsepline% <- added
]{scrartcl}
\usepackage{blindtext}
\usepackage{lastpage}
\renewcommand\pagemark{{\usekomafont{pagenumber}\thepage\ of \pageref{LastPage}}}
\pagestyle{headings}% <- added

\author{Author}
\title{Title}
\begin{document}
\maketitle
\section{Testing}
\blinddocument
\end{document}

enter image description here


If you want to customize header and footer, you can use package scrlayer-scrpage which is part of the KOMA-Script bundle:

\documentclass[12pt]{scrartcl}
\usepackage{blindtext}
\usepackage{lastpage}
\renewcommand\pagemark{{\usekomafont{pagenumber}\thepage\ of \pageref{LastPage}}}
\usepackage{scrlayer-scrpage}% sets pagestyle scrheadings automatically
\clearpairofpagestyles
\ohead{\headmark}
\ofoot*{\pagemark}

\author{Author}
\title{Title}
\begin{document}
\maketitle
\section{Testing}
\blinddocument
\end{document}

The starred version of \ofoot sets the entry for both scrheadings and plain.

enter image description here

esdd
  • 85,675
9

Please see the following MWE. Important code is marked with <=======.

\documentclass[a4paper, oneside, 12pt]{scrartcl}

\usepackage[%
  footsepline=0.25pt, headsepline=0.25pt,
  % automark places section title in header. Also enables placement in footer.
  automark
]{scrlayer-scrpage} % <=================================================

\usepackage[a4paper,
  vmargin=2cm, hmargin=2cm, % page margins
  includehead, includefoot, % Margins calculated include header and footer
  footskip=2em]
{geometry}

\usepackage{blindtext}
\usepackage{lastpage} % <===============================================

\ihead{\rightmark}
\chead{}
\ohead{\leftmark}
\ifoot{}
\cfoot{\thepage\ of \pageref{LastPage}} % <=============================
\ofoot{}
\pagestyle{scrheadings}

\begin{document}
\section{Testing}
\blinddocument
\end{document}

It uses scrlayer-scrpage and lastpage with the following result:

enter image description here

Mensch
  • 65,388
  • Good answer! Is there anything you can do about the upright 1, it sticks out a little among all of the slanted text in the rest of the header and footer – Au101 Feb 26 '18 at 23:01
  • 1
    @Au101 Ups, I oversaw that. I changed the code and use now \thepage instead of \pagemark, which uses explicitly \usekomafont{pagenumber}, resulting in an different font for the current page number ... – Mensch Feb 27 '18 at 14:43
  • 1
    Good job =) Looks great =) +1 from me – Au101 Feb 27 '18 at 14:52