1

I am currently working on a document which needs a header which contains the page number and page count within the current chapter.

\documentclass[a4paper,12pt]{scrreprt}
\usepackage{scrlayer-scrpage}
\usepackage[english]{babel}
\usepackage{blindtext}

\renewcommand\chapterpagestyle{scrheadings}
\renewcommand\chaptermark[1]{\markboth{#1}{}}

\clearscrheadfoot
\pagestyle{scrheadings}
\chead{\leftmark\ (N/M)}
\cfoot{\thepage}

\begin{document}

\chapter{Lorem ipsum}
\blindtext[15]

\chapter{Dolor sit amet}
\blindtext[5]

\chapter{Consectetur}
\blindtext[15]

\chapter{Adipiscing elit}
\blindtext[10]

\end{document}

Expected output:

Lorem ipsum (1/3)
...

Lorem ipsum (2/3)
...

Lorem ipsum (3/3)
...

Dolor sit amet
...

Consectetur (1/3)
...

...

I did not find any suitable question to the problem in the StackOverflow network with some short and working example code so I decided to create a new question instead of hijacking an old one.

Since I managed to get this done very recently with some custom counters and pageref arithmetic I'm also posting my own answer to this problem. I really wonder if there is a better way to do this.

1

I managed to get this done by renewing the \chapter command which increments two counters. One counter for the current ChapterIndex and another for the NextChapterIndex. Every chapter creates a \label with the key ch:\theChapterIndex which can then be referenced when the head or foot is rendered. The chapter relative page number can be calculated using the current page number and the page number of the beginning of the chapter. The count can be calculated by the difference of the chapter page number and the next chapter page number. The last chapter is a bit tricky as there is no next chapter. For that case I just added that label to the end of the document and added some \ifnum logic which compares the page of the next chapter to the last page number.

It took me quite a lot of trial and error with \ifthenelse which does not seam to work within a \numexpr so I had to fall back to \ifnum and write it a little different.

Long story short - I managed to get this done and here is the resulting LaTeX code:

\documentclass[a4paper,12pt]{scrreprt}
\usepackage{scrlayer-scrpage}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{lastpage}
\usepackage{refcount}
\usepackage{ifthen}

% Per chapter page numbers (N/M)
\newcounter{ChapterIndex}
\newcounter{NextChapterIndex}
\stepcounter{NextChapterIndex}
\let\chapterOrig\chapter
\renewcommand{\chapter}[1]{
\chapterOrig{#1}
\stepcounter{ChapterIndex}
\stepcounter{NextChapterIndex}
\label{ch:\theChapterIndex}
}
\newcommand{\chapterPage}[0]{\number\numexpr\thepage-\getpagerefnumber{ch:\theChapterIndex}+1\relax}
\newcommand{\chapterPageCount}[0]{\number\ifnum\getpagerefnumber{ch:\theNextChapterIndex}=\numexpr\getpagerefnumber{LastPage}\numexpr\getpagerefnumber{LastPage}-\getpagerefnumber{ch:\theChapterIndex}+1\relax\else\number\numexpr\getpagerefnumber{ch:\theNextChapterIndex}-\getpagerefnumber{ch:\theChapterIndex}\relax\fi}
\newcommand{\chapterPaginator}[0]{\ifnum\chapterPageCount>1(\chapterPage/\chapterPageCount)\fi}

\renewcommand\chapterpagestyle{scrheadings}
\renewcommand\chaptermark[1]{\markboth{#1}{}}

\clearscrheadfoot
\pagestyle{scrheadings}
\chead{\leftmark\ \chapterPaginator}
\cfoot{\thepage}

\begin{document}

\chapter{Lorem ipsum}
\blindtext[15]

\chapter{Dolor sit amet}
\blindtext[5]

\chapter{Consectetur}
\blindtext[15]

\chapter{Adipiscing elit}
\blindtext[10]

\label{ch:\theNextChapterIndex}

\end{document}

Screenshot

Screenshot of the resulting document

  • Perhaps a screen shot would complete this answer? ;-) – user31729 May 27 '15 at 21:24
  • @ChristianHupfer TeX.SX does not allow uploading of PDF files. :-( I took a screenshot of the resulting head. That should provide an idea how it is supposed to look. – bikeshedder May 27 '15 at 21:58
  • That's why I wrote screen shot and not .pdf file! Thanks – user31729 May 27 '15 at 22:05
0

The following solution uses

  • assoccnt to provide a per-chapter page counter. That is, a counter perchpage (reset with every new \chapter) that steps in sync with the page counter;

  • zref to write only the perchpage to the .aux at the end of every \chapter. zref also provides expandable checking against references together with some default if the reference doesn't exist (similar in nature to what refcount provides.;

  • atveryend to write a reference at the end of the last \chapter before the .aux file is closed.

enter image description here

\documentclass{scrreprt}
\usepackage{scrlayer-scrpage}
\usepackage[english]{babel}
\usepackage{blindtext,assoccnt,zref,atveryend}

\makeatletter
\newcounter{perchpage}[chapter]
\DeclareAssociatedCounters{page}{perchpage}
\zref@newprop{perchpage}[0]{\number\value{perchpage}}

\let\oldchapter\chapter
\renewcommand{\chapter}{%
  \cleardoublepage
  \ifnum\value{chapter}>0\zref@labelbyprops{ch:end:lbl\thechapter}{perchpage}\fi
  \oldchapter
}
\newcommand{\chapterPaginator}{%
  \ifnum\zref@extract{ch:end:lbl\thechapter}{perchpage}>1
    (\number\numexpr\value{perchpage}+1\relax/\zref@extract{ch:end:lbl\thechapter}{perchpage})
  \fi}
\AfterLastShipout{\zref@labelbyprops{ch:end:lbl\thechapter}{perchpage}}
\makeatother

\renewcommand\chapterpagestyle{scrheadings}
\renewcommand\chaptermark[1]{\markboth{#1}{}}

\clearscrheadfoot
\pagestyle{scrheadings}
\chead{\leftmark\ \chapterPaginator}
\cfoot{\thepage}

\begin{document}

\chapter{Lorem ipsum}
\blindtext[15]

\chapter{Dolor sit amet}
\blindtext[5]

\chapter{Consectetur}
\blindtext[15]

\chapter{Adipiscing elit}
\blindtext[10]

\end{document}

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.