[Tex/LaTex] Modifying the Page Numbers in the Table of Contents

page-numberingtable of contents

Is there a way how I could make a section/chapter/any-heading which occurs on page x in my document to appear in the table of contents with the page x-1? (I am using scrbook)

I'd like this result not for the entire document but only for a part of it.

General Example:

  • Heading 1 of page 3 is listed in the ToC as starting on page 3. (x)
  • Heading 2 of page 11 is listed in the ToC as starting on page 11. (x)
  • Heading 3 of page 23 is listed in the ToC as starting on page 22. (x-1)
  • Heading 4 of page 47 is listed in the ToC as starting on page 46. (x-1)
  • Heading 4 of page 69 is listed in the ToC as starting on page 69. (x)

It might sound strange but I have good reasons for doing this:

Particular Context:

In my particular case I think I'll use this in the appendix (A) of the backmatter. The headings I am talking about there are sections:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x                              x                              x
x              \section*{فصل}  x  A.1 \section{Heading}       x
x                              x                              x
x                              x                              x
x                              x                              x
x                              x                              x
x                              x                              x
x                              x                              x
x                              x                              x
x 22                           x                           23 x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On page 22 I use \section*{Heading} for creating a section heading that is not listed in the ToC. On page 23 I use \section{Heading} to create a section heading that is listed in the ToC but which I would like to apeare there as starting on page 22.
The reason why I do this is that the text on page 22 is an Arabic text whose English translation is on page 23 but I want the english title in the ToC.

Best Answer

you can suppress the automatically generated toc entry and replace it.

to suppress, add this command to your preamble:

\DeclareRobustCommand{\SkipTocEntry}[4]{}

if you are using hyperref, change the [4] to [5] -- this is the number of elements in the toc entry to be suppressed.

immediately before the line that generates the toc entry, insert the line

\addtocontents{toc}{\SkipTocEntry}

if applying it to a chapter, and you use \include to insert your chapters, it must be in the same file as the \chapter command; if you put it in the driver file, the timing will be off, and the chapter entry from the next chapter will be suppressed instead.

a replacement entry should be inserted immediately after the line that generated the toc entry, in order to ensure that the page number has been resolved correctly. define a counter \newcounter{prevpage} in the preamble. when setting up the replacement toc entry,

\setcounter{prevpage}{\value{page}}
\addtocounter{prevpage}{-1}

then use \addtocontents{toc}{xxx} where xxx is the line that was automatically generated (and suppressed) that you can copy from the .toc file, except that you should replace the assigned page number by {\theprevpage}. if you had to \protect some commands in the text of the original heading, you should do the same in the replacement toc entry.

say this line appears in the toc (the example is from a test using amsbook):

\contentsline {section}{\tocsection {}{5}{Section title}}{10}

the page number is {10}. what you'd put in your file as a replacement is

\addtocontents{toc}{\protect\contentsline {section}{\protect\tocsection {}{5}{Section title}}{\theprevpage}}

observe that \contentsline and \tocsection do need to be \protected. (i took this example from a test using amsart.cls, but the principle should be the same regardless of the document class. just copy what's in the .toc file from your first run, and modify it appropriately.)

EDIT: the OP created an admirable macro, \transsection to handle this. what follows is a self-contained example file demonstrating the use of his macro with the book class.

\documentclass{book}
\usepackage{verbatim}

\DeclareRobustCommand{\SkipTocEntry}[4]{}
\newcounter{prevpage}

\newcommand{\transsection}[1]{%
  \addtocontents{toc}{\SkipTocEntry}%
  \section{#1}%
  \setcounter{prevpage}{\value{page}}\addtocounter{prevpage}{-1}%
  \addtocontents{toc}{%
    \protect\contentsline {section}{%
    \protect\numberline {\thesection}#1}{\theprevpage}}}

\begin{document}
\frontmatter
\tableofcontents

\mainmatter
\chapter{First}
\section{First section}
some text

\clearpage
\transsection{Test section}

We want this section to have a page number one less than the
real one.  This is from question 55645 in TeX.SX, where the
author is setting an Arabic page on the page before the
English translation page, and wants the TOC to show that
page number.

\vspace{1\baselineskip}
\verbatiminput{\jobname.tex}
\end{document}