10

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.

4
  • 1
    Please provide some context -- doing this for a \chapter, especially one that starts a new odd page, might be easier than for a \section.
    – lockstep
    Commented May 13, 2012 at 13:24
  • @lockstep : I did provide some context. Commented May 13, 2012 at 13:43
  • Which document class are you using? Commented May 13, 2012 at 16:54
  • @GonzaloMedina : scrbook on LuaLaTeX Commented May 13, 2012 at 17:05

3 Answers 3

11

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}
10
  • I like your idea of creating the prevpage-counter but I don't know how I can "replace the assigned page number by {\theprevpage}" when I use \addtocontents{toc}{xxx} to create the entry. Commented May 13, 2012 at 17:18
  • @ClintEastwood -- i've added an example to the answer. i hope it's clear now. Commented May 13, 2012 at 17:53
  • Thanks. I tried it now, but unfortunately it does not compile without error. I always get "the control sequence ... was never def'ed". When I click on "go to error" it shows me this passage of my *.toc-file: \contentsline {section}{\tocsection {}{5}{Section title}}{53}. the "53" is the correct prevpage, but it seems that he doesn't get the sectionnumber right. it should be \numberline {B.2} instead of \tocsection {}{4}. What I did is just copy and paste your example line in the document and added the lines for prevpage. Commented May 13, 2012 at 18:54
  • 1
    @ClintEastwood -- okay. run the job without the \SkipTocEntry. look in the .toc file, and copy out whatever line corresponds to the section whose number you want to change; that's the line that will have the correct information for the document class you're using. (not all classes generate the same commands for the .toc, and you really need to match the one you're using.) change the page number to {\prevpage}, and you may have to add \protect in front of \numberline. then insert the \SkipTocEntry and rerun (twice). Commented May 13, 2012 at 20:08
  • 1
    @ClintEastwood -- i'm afraid i don't understand the problem you describe; i can't reproduce the wrong page number. i've added a full self-contained example to the answer that shows that your macro (a very nice one!) works. i omitted the last {section.\thesection} since that really isn't needed in the toc. Commented May 14, 2012 at 14:15
4

The following example shows a way to do this for section.

To use the special section I define the command \specialsection. It's important to use \makeatletter for the table of contents.

\documentclass{scrbook}

\makeatletter
\renewcommand\@pnumwidth{3.55em}
\let\@dottedtoclineorig\@dottedtocline
\def\local@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor \the\numexpr#5-1\relax~(x-1)}%
     \par}%
  \fi}
\newcommand\specialsection[2][]{%
\addtocontents{toc}{\protect\let\protect\@dottedtocline\protect\local@dottedtocline}
   \ifx\relax#1\relax
       \section{#2}
   \else
      \section[#1]{#2}
  \fi
\addtocontents{toc}{\protect\let\protect\@dottedtocline\protect\@dottedtoclineorig}
}
\makeatother


\begin{document}
\makeatletter
\tableofcontents
\makeatother
\chapter{foo}
\backmatter
\chapter{foo}
\section*{arabic}
text in arabix
\clearpage
\specialsection{english}
text in English

\end{document}

Result:

enter image description here

3
  • while you were answering I added some context to my initial question. So I 'd like t ask: does this also work with scrbook and sections? your solution seems nice! Commented May 13, 2012 at 13:45
  • 1
    @ClintEastwood: I edited my answer. Commented May 13, 2012 at 14:05
  • @MarcoDaniel, I am very interested to use your solution, to @ClintEastwood's question, on the article documentclass. I looked at your initial answer from 13:41, where you used the report class, but I can't seem to get it to work. Would you be able to make an example using the report class? Thanks!
    – Eric Fail
    Commented May 3, 2014 at 13:14
3

You can fill in your table of contents manually for each section like this:

% When you create a new section in your text

\section*{Your section name string} % the asterisk needed.

\addtocontents
{toc}
{  \protect\contentsline{section}
   {Your section name string}
   {\number\numexpr\the\value{page} - 1\relax} % There is \thepage-1 value.
}

% The text of your section's here...

The code is the slightly changed code of \addcontentsline command. Its original code can be seen in terminal like this:

texdef -t latex addcontentsline
1
  • Sweet! Finally resolved my issue. Thanks. But I found out the compiler hints an error in sharelatex, I appended an empty {} within the second curly braces. Now the code looks like: \addtocontents {toc} { \protect\contentsline{section} {Your section name string} {\number\numexpr\the\value{page} - 1\relax} {} } Commented Mar 24, 2018 at 5:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .