Custom page numbering display in table of contents

Using a KOMA-script class, I would like to customize the presentation of the pagenumbers in the table of contents.

My pagenumbers are reset every section and are represented like 2-3 which means page 3 of section 2. This is not achieved by changing the \thepage command but by simply writing in the footer {\thesection-\thepage}.

When creating the toc, obviously only \thepage-number is shown. What I would like to achieve is a number format like this:

1 Sectiontitle
1.1 Subsectiontitle .............. 1-1
1.2 Other title .................. 1-2

2 Next Section
2.1 This Section ................. 2-1


I would like to keep the definition of \thepage as it's default, since I might be needing the number for other applications.

I already found various posts (here or here or here) regarding the formatting of the toc, but I didn't find anything regarding the format of the page numbers.

Edit 1:

I believe, that using the pagenumberbox command from the tocstyle-package should do the job of putting something in front of the pagenumber. However, I'm still clueless on how achieving the right section number to put into the pagenumberbox

Edit 2:

MWE:

\documentclass[english,footsepline,listof=totoc]{scrartcl}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{float}

\makeatletter

\usepackage{geometry}
\geometry{a4paper,verbose=true,nomarginpar,tmargin=4cm,bmargin=2.2cm,footskip=1.1cm,lmargin=3cm,rmargin=2.5cm,headheight=3cm}

\setlength{\footheight}{1cm}

%% Begin of way to indent all body but no headings
\newcommand\LeftMargin{.5cm}
\renewcommand*\sectionlinesformat[4]{ \ifstr{#3}{}{}{\hspace*{-\LeftMargin}}#3#4}
\usepackage[  headwidth=\the\textwidth+\LeftMargin:-\LeftMargin:\LeftMargin,footwidth=\the\textwidth+\LeftMargin:-\LeftMargin:\LeftMargin]{scrlayer-scrpage}

%% Define how deep the numeration in the document should be.
\setcounter{secnumdepth}{5}
%% Define how deep the numeration in the table of contents should be.
\setcounter{tocdepth}{3}

\pagestyle{scrheadings} %same as above

%% Define customized pagenumbering
\let\stdsection\section
\renewcommand\section{%
\clearpage%
\setcounter{page}{1}%
\stdsection%
}

\clearscrheadfoot
\ofoot{\raisebox{-2mm}{\thesection-\thepage}}
\cfoot{\raisebox{-2mm}{Footer}}
\setkomafont{pageheadfoot}{\normalfont}

\usepackage[nohyperlinks]{acronym}

\begin{document}

\tableofcontents{}

\section{First}

\newpage

\section{Second}

Content

\end{document}


Edit 3:

Based on the below-stated answer, I was able to achieve the page-numbering I wanted. Only problem is, that the page numbers in the toc seem to be somehow aligned (see image below).

This looks obviously strange, is there some way of aligning the page-numbers right rather than left?

• You are not even sharing with us, which documentclass you are using. Once you talk about chapter, few lines later you are talking about sections. A minimal working example is neded to even have a starting point in answering your question. – Johannes_B May 4 '16 at 7:00
• @Johannes_B You're right, I was just creating a MWE. Edited the question accordingly – PSC May 4 '16 at 7:15
• You are doing some really strange stuff. And like i thought, you are using the wrong class. You would have been better of with scrreprt, as it really provides chapters, starting on a new page. – Johannes_B May 4 '16 at 7:17
• I'm open for all comments and suggestions. I'll have a look into scrrept but I can't really see, how that would fix my initial issue? – PSC May 4 '16 at 7:21

1 Answer

Disclaimer: I do not recommend such a strange page numbering, certainly not without changing \thepage.

But it is possible using the current prerelease of KOMA-Script 3.20.

\documentclass[english,footsepline,plainfootsepline,listof=totoc,
footheight=1cm,% <- added
oneside,% <- added
headings=small% <- added
]{scrbook}[2016/04/01]% <- changed
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{float}
\usepackage{geometry}
\geometry{
a4paper,verbose=true,nomarginpar,
tmargin=4cm,bmargin=2.2cm,
footskip=1.3cm,% <- changed
lmargin=3cm,rmargin=2.5cm,
headheight=3cm}
%% Define how deep the numeration in the document should be.
\setcounter{secnumdepth}{5}
%% Define how deep the numeration in the table of contents should be.
\setcounter{tocdepth}{3}

%% Begin of way to indent all body but no headings
\newcommand\LeftMargin{.5cm}
\renewcommand*\chapterlinesformat[3]{% <- added
\ifstr{#2}{}{}{\hspace*{-\LeftMargin}}#2#3%
}
\renewcommand*\sectionlinesformat[4]{%
\ifstr{#3}{}{}{\hspace*{-\LeftMargin}}#3#4%
}
\usepackage[
headwidth=\the\textwidth+\LeftMargin:-\LeftMargin:\LeftMargin,
footwidth=\the\textwidth+\LeftMargin:-\LeftMargin:\LeftMargin
]{scrlayer-scrpage}
\clearpairofpagestyles
\ModifyLayer[addvoffset=-2mm]{scrheadings.foot.above.line}% <- added
\ModifyLayer[addvoffset=-2mm]{plain.scrheadings.foot.above.line}% <- added
\ofoot*{\pagemark}% <- changed
\cfoot*{Footer}% <- changed
\setkomafont{pageheadfoot}{\normalfont}

%% strange page numbering (added)
\usepackage{xpatch}
\makeatletter
\renewcommand\pagemark{\if@mainmatter\thechapter-\thepage\else\thepage\fi}
\xpretocmd{\chapterheadstartvskip}
{\if@mainmatter\setcounter{page}{1}\fi}{}{}
\renewcommand\addchaptertocentry[2]{%
\if@mainmatter
{\addtocontents{toc}{\protect\chapterinpagenumber{#1-}}}
\else
{\addtocontents{toc}{\protect\chapterinpagenumber{}}}
\fi%
\addtocentrydefault{chapter}{#1}{#2}%
}
\newcommand\@chapterinpagenumber{}
\newcommand\chapterinpagenumber[1]{%
\renewcommand\@chapterinpagenumber{#1}%
}
\newcommand\pagenumbertocformat[1]{\@chapterinpagenumber#1}
\makeatother

\RedeclareSectionCommands[
tocpagenumberformat=\pagenumbertocformat%
]{chapter,section,subsection,subsubsection,paragraph,subparagraph}
\RedeclareSectionCommand[
beforeskip=1sp
]{chapter}

\usepackage{blindtext}% dummy text
\begin{document}
\frontmatter
\tableofcontents
\listoffigures
\listoftables
\mainmatter
\blinddocument
\blinddocument
\end{document}


Update regarding the alignment of the page numbers in TOC

A page number of an entry is printed right aligned in a box of width \@pnumwidth. So maybe you want to enlarge \@pnumwidth using for example

\renewcommand\@pnumwidth{3em}


Or you can use a \mbox as tocpagenumberbox.

\RedeclareSectionCommands[
tocpagenumberformat=\pagenumbertocformat,%
tocpagenumberbox=\mbox% <- added
]{chapter,section,subsection,subsubsection,paragraph,subparagraph}


Code:

\documentclass[english,footsepline,plainfootsepline,listof=totoc,
footheight=1cm,% <- added
oneside,% <- added
headings=small% <- added
]{scrbook}[2016/04/01]% <- changed
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{scrhack}
\usepackage{float}
\usepackage{geometry}
\geometry{
a4paper,verbose=true,nomarginpar,
tmargin=4cm,bmargin=2.2cm,
footskip=1.3cm,% <- changed
lmargin=3cm,rmargin=2.5cm,
headheight=3cm}
%% Define how deep the numeration in the document should be.
\setcounter{secnumdepth}{5}
%% Define how deep the numeration in the table of contents should be.
\setcounter{tocdepth}{3}

%% Begin of way to indent all body but no headings
\newcommand\LeftMargin{.5cm}
\renewcommand*\chapterlinesformat[3]{% <- added
\ifstr{#2}{}{}{\hspace*{-\LeftMargin}}#2#3%
}
\renewcommand*\sectionlinesformat[4]{%
\ifstr{#3}{}{}{\hspace*{-\LeftMargin}}#3#4%
}
\usepackage[
headwidth=\the\textwidth+\LeftMargin:-\LeftMargin:\LeftMargin,
footwidth=\the\textwidth+\LeftMargin:-\LeftMargin:\LeftMargin
]{scrlayer-scrpage}
\clearpairofpagestyles
\ModifyLayer[addvoffset=-2mm]{scrheadings.foot.above.line}% <- added
\ModifyLayer[addvoffset=-2mm]{plain.scrheadings.foot.above.line}% <- added
\ofoot*{\pagemark}% <- changed
\cfoot*{Footer}% <- changed
\setkomafont{pageheadfoot}{\normalfont}

%% strange page numbering (added)
\usepackage{xpatch}
\makeatletter
\renewcommand\pagemark{\if@mainmatter\thechapter-\thepage\else\thepage\fi}
\xpretocmd{\chapterheadstartvskip}
{\if@mainmatter\setcounter{page}{1}\fi}{}{}
\renewcommand\addchaptertocentry[2]{%
\if@mainmatter
{\addtocontents{toc}{\protect\chapterinpagenumber{#1-}}}
\else
{\addtocontents{toc}{\protect\chapterinpagenumber{}}}
\fi%
\addtocentrydefault{chapter}{#1}{#2}%
}
\newcommand\@chapterinpagenumber{}
\newcommand\chapterinpagenumber[1]{%
\renewcommand\@chapterinpagenumber{#1}%
}
\newcommand\pagenumbertocformat[1]{\@chapterinpagenumber#1}
\makeatother

\RedeclareSectionCommands[
tocpagenumberformat=\pagenumbertocformat,%
tocpagenumberbox=\mbox%
]{chapter,section,subsection,subsubsection,paragraph,subparagraph}
\RedeclareSectionCommand[
beforeskip=1sp
]{chapter}

\usepackage{blindtext}% dummy text
\begin{document}
\frontmatter
\tableofcontents
\listoffigures
\listoftables
\mainmatter
\blinddocument
\setcounter{chapter}{9}% only for this example
\blinddocument
\clearpage
\setcounter{page}{100}% % only for this example
\section{Only for testing}
\end{document}

• Thank you for your input, this looks exactly like the thing I need. However, when I try to compile your MWE, it stops at the error ! Undefined control sequence. l.2 \chapterinpagenumber – PSC May 9 '16 at 7:18
• Found the error: I had to delete the .toc file by hand since it was built using the old koma-version. – PSC May 9 '16 at 7:44
• I just found another minor problem: With the line {\if@mainmatter\setcounter{page}{1}\fi}{}{} you reset the page-counter when starting the mainmatter. This is fine with me, but for some reason I don't yet understand, the page-counter is reset every chapter which - as stated in my original question - I'd like to avoid. – PSC May 9 '16 at 10:20
• I modified the question to show one other problem, I discovered – PSC May 9 '16 at 11:21
• Note that in your MWE the page counter is reset every section in \let\stdsection\section \renewcommand\section{\clearpage\setcounter{page}{1}\stdsection}. Regarding the alignment see my updated answer. – esdd May 9 '16 at 20:58