[Tex/LaTex] Latex page numbering by section

page-numbering

I'm working in a report that needs the page number relative to the section.

1-1

1-2

1-3

2-1

2-2

etc

I'm using \numberwithin{page}{section} but two undesirable effects appear:

  1. The pages start from zero: the first page of a chapter is chapternumber.0 , and I'd like them starting from 1.

  2. The separator is a dot, and for legibility I'd prefer a dash

Anybody knows how to do this?

Best Answer

You need to do at least three things to achieve the output you're after:

  1. Make the page number reset with every use of \section. This can be achieved by

    \numberwithin{page}{section}
    

    The above also sets the page number display to be \thesection.\arabic{page} by default.

  2. To correct the above default setting of the page numbering, use

    \renewcommand{\thepage}{\thesection-\arabic{page}}
    
  3. Finally, you want to let the page numbering start from 1. To do this, you need to make sure that the page numbering is adjusted only when you call \section (otherwise it may be reset when calling \subsection, \subsubsection...). Here a patch via etoolbox of \@sect inserts an appropriate page number stepping:

    \makeatletter
    \patchcmd{\@sect}% <cmd>
      {\protected@edef}% <search>
      {\def\arg{#1}\def\arg@{section}%
       \ifx\arg\arg@\stepcounter{page}\fi%
       \protected@edef}% <replace>
      {}{}% <success><failure>
    \makeatother
    

Here is complete, minimal example that shows the output:

enter image description here

\documentclass{article}
\usepackage[paper=a6paper]{geometry}% Just for this example
\usepackage{lipsum}% Just for this example
\usepackage{amsmath,etoolbox}
\numberwithin{page}{section}% Number page by section
\renewcommand{\thepage}{\thesection-\arabic{page}}% Page numbering style
\makeatletter
% Make sure that page starts from 1 with every \section
\patchcmd{\@sect}% <cmd>
  {\protected@edef}% <search>
  {\def\arg{#1}\def\arg@{section}%
   \ifx\arg\arg@\stepcounter{page}\fi%
   \protected@edef}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}
\section{A section}
\lipsum[1-5]
\section{Another section}
\lipsum[6-10]

\end{document}
Related Question