[Tex/LaTex] Resets page numbering after table of contents

page-numberingtable of contents

I am numbering my index pages with roman numerals, but I'm writing the document using restructuredText that is parsed into LaTeX.

I have a preamble that contains all the necessary setup, but after the table of contents, it resets the page numbering back to 1, so \listoftables and \listoffigures show up at page (i) and (ii) respectively instead of (iii) and (iv) like they should. Since my table of contents could change, I don't want to use fixed page numbering.

There is a question that suggests rewriting \tableofcontents to storing the page number, but I have tried that and it didn't work. Since I am new here, I cannot comment on that one.

I would be willing to accept that I cannot fix this as long as I could disable page numbering for the TOC.

The following are the used classes and packages from the generated output:

% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt, openany, oneside, english]{sphinxmanual}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{00A0}{\nobreakspace}
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{times}
\usepackage[Sonny]{fncychap}
\usepackage{longtable}
\usepackage{sphinx}
\usepackage{multirow}
% Include packages
\usepackage{lastpage}
\usepackage[table]{xcolor}
\usepackage{csvsimple}
\usepackage[hypcap]{caption}
\usepackage{booktabs}
\usepackage{wrapfig}
\usepackage{hyperref}
\usepackage[nottoc]{tocbibind}
\hypersetup{colorlinks,%
    citecolor=black,%
    filecolor=black,%
    linkcolor=black,%
    urlcolor=blue,%
pdftex}
\definecolor{lightgray}{gray}{0.9}
\let\oldtabular\tabular
\let\endoldtabular\endtabular

Since I have no control over when the table of contents will be called on, I also redefined the table of contents like this:

% Redefine table of contents to include the tables and figures
\newcounter{mypageno}
\let\oldtableofcontents\tableofcontents
\renewcommand{\tableofcontents}{%
  \oldtableofcontents%
  \cleardoublepage%
  \setcounter{mypageno}{\value{page}}%
  \pagenumbering{roman}%
  \setcounter{page}{\value{mypageno}}%
  \listoftables%
  \listoffigures%
  \cleardoublepage%
  \pagenumbering{arabic}%
}

Best Answer

First of all, \pagenumbering resets the page counter to 1, so don't use it if you don't want it reset. Instead just change the numbering representation using something like

\renewcommand{\thepage}{\roman{page}}

Now, I notice you're using the sphinxmanual document class. Looking at the code, we see a redefinition of \tableofcontents that includes a \pagenumbering:

% This wraps the \tableofcontents macro with all the magic to get the spacing
% right and have the right number of pages if the 'openright' option has been
% used.  This eliminates a fair amount of crud in the individual document files.
%
\let\py@OldTableofcontents=\tableofcontents
\renewcommand{\tableofcontents}{%
  \setcounter{page}{1}%
  \pagebreak%
  \pagestyle{plain}%
  {%
    \parskip = 0mm%
    \py@OldTableofcontents%
    \if@openright%
      \ifodd\value{page}%
        \typeout{Adding blank page after the table of contents.}%
        \pagebreak\hspace{0pt}%
      \fi%
    \fi%
    \cleardoublepage%
  }%
  \pagenumbering{arabic}% <--------------- This resets the page to 1
  \@ifundefined{fancyhf}{}{\pagestyle{normal}}%
}

You can remove this reset of the page number by adding the following to your preamble before you redefine \tableofcontents:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\tableofcontents}{\pagenumbering{arabic}}{}{}{}

Note that the page style may also be adjusted by sphinxmanual's definition of \tableofcontents. But for now this should get where you want.