7

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?

migrated from stackoverflow.com Nov 6 '14 at 19:20

This question came from our site for professional and enthusiast programmers.

  • The first thing you can do is use \setcounter{page}{1} to force the counter to start at 1 instead of 0. I know how to alter the separator for Figures/Tables (e.g., see subcaption package documentation), so I imagine it's similar for page numbers. – honeste_vivere Oct 11 '14 at 19:01
  • Okay, so I did some digging and you can use commands like \thechapter, \thesection, and then \leftmark and \rightmark to figure this out. For details, you can check out the following link. Though, I should note that this question should be in the LaTeX forum, not Overflow. – honeste_vivere Oct 11 '14 at 19:07
  • Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Nov 6 '14 at 19:23
7

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}
  • How would one need to modify this solution for koma script classes? – FlorianL Jul 18 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy