2

I would like to get

  1. the page number of the first page of the current chapter
  2. the page number of the first page of the current section

Is it possible?

The goal is to add the chapter/section to a secondary toc, but currectly I get the actual page instead of the page number of the first page of the current chapter

\addcontentsline{todo}{section}%
  {\protect\numberline{\thesection}{\Sectionname}}%

EDIT I will not maintain the labels for chapter and section. Further, the whole thing has to be automatic, that is the program does not know if it has to use the label sec:function or sec:relation; and I do not want to define label of the form sec:1:3.

EDIT 2 One way would be to modify the chapter command in order to save the page number in a variable (I do not know how!), but perhaps there is a more clean way.

PeptideChain
  • 1,335

2 Answers2

4

Redefine \chapter and \section to issue a suitable \label command, that's automatically built from a counter that's stepped at each \chapter or \section command.

The label can be stored in the commands \currentchapterlabel and \currentsectionlabel that any \chapter or \section command will override.

Finally, we can define \currentchapterpage and \currentsectionpage to do the suitable \pageref command.

\documentclass{book}
\usepackage{xparse}
\usepackage{kantlipsum}

\newcounter{currentchaptersection}
\renewcommand{\thecurrentchaptersection}{\roman{currentchaptersection}}
\let\latexchapter\chapter
\let\latexsection\section

\RenewDocumentCommand{\chapter}{sO{#3}m}{%
  \stepcounter{currentchaptersection}%
  \xdef\currentchapterlabel{css-\thecurrentchaptersection}%
  \IfBooleanTF{#1}
    {\latexchapter*{#3}}
    {\latexchapter[#2]{#3}\label{\currentchapterlabel}}%
}
\RenewDocumentCommand{\section}{sO{#3}m}{%
  \stepcounter{currentchaptersection}%
  \xdef\currentsectionlabel{css-\thecurrentchaptersection}%
  \IfBooleanTF{#1}
    {\latexsection*{#3}}
    {\latexsection[#2]{#3}\label{\currentsectionlabel}}%
}

\newcommand{\currentchapterpage}{\pageref{\currentchapterlabel}}
\newcommand{\currentsectionpage}{\pageref{\currentsectionlabel}}

\begin{document}

\chapter{One}

\kant[1-5]

\section{Two}

\kant[6-19]

This chapter started on page~\currentchapterpage.

This section started on page~\currentsectionpage.


\section{Three}

\kant[6-19]

This chapter started on page~\currentchapterpage.

This section started on page~\currentsectionpage.


\chapter{Four}

\kant[1-5]

\section{Five}

\kant[6-19]

This chapter started on page~\currentchapterpage.

This section started on page~\currentsectionpage.

\section{Six}

\kant[6-19]

This chapter started on page~\currentchapterpage.

This section started on page~\currentsectionpage.


\end{document}

enter image description here

egreg
  • 1,121,712
  • Straightforward and clear. Due to the lack of expl-style spacing in the code, though, I almost believed that you had placed the \label commands inside the mandatory argument of \latexchapter or \latexsection. ;-) – frougon Jul 08 '19 at 19:32
  • The output works, but the compilation gives me a bunch of errors of the type: https://imgur.com/69aAqOq – PeptideChain Jul 09 '19 at 08:11
  • @PeptideChain You probably have an outdated version of xparse. – egreg Jul 09 '19 at 08:41
  • I confirm. And overleaf.com has also an old version. – PeptideChain Jul 09 '19 at 12:00
1
\documentclass{book}
\usepackage{xpatch}

\newcounter{pgnumchapter}
\setcounter{pgnumchapter}{0}
\newcounter{pgnumsection}
\setcounter{pgnumsection}{0}
\xapptocmd{\chaptermark}{%
  \setcounter{pgnumchapter}{\thepage}%
}{}{\PatchFailed}
\xapptocmd{\sectionmark}{%
  \setcounter{pgnumsection}{\thepage}%
}{}{\PatchFailed}

\begin{document}
\chapter{ch1}
\section{Introduction}
First page
\vfill
\pagebreak
\section{Conclusion}
Second page
\vfill
\pagebreak
Third page

First page chapter: \thepgnumchapter.

First page section: \thepgnumsection.

Current page: \thepage.
\end{document}
PeptideChain
  • 1,335
  • Interesting, unfortunately I believe it doesn't work for unnumbered chapters or sections (as they don't do \chaptermark resp. \sectionmark). – frougon Jul 08 '19 at 19:41
  • @frougon Thank you for the comment. Probably I will never need it, because the whole thing is for a to do list, and I do not have appendix (just an empty preface). If you like a challenge: https://tex.stackexchange.com/questions/499192 – PeptideChain Jul 09 '19 at 07:08