[Tex/LaTex] Custom page numbering display in table of contents

koma-scriptpage-numberingtable 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?

enter image description here

Best 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}

enter image description here


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}

enter image description here

Or you can use a \mbox as tocpagenumberbox.

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

enter image description here

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}
Related Question