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.

2
  • Use the built in label/ref-mechanism.
    – Johannes_B
    Commented Jul 8, 2019 at 9:04
  • @Johannes_B thank you for the comment. I forgot to say that I do not want to maintain the labels. I edit the post. Commented Jul 8, 2019 at 9:05

2 Answers 2

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

4
  • 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
    Commented Jul 8, 2019 at 19:32
  • The output works, but the compilation gives me a bunch of errors of the type: imgur.com/69aAqOq Commented Jul 9, 2019 at 8:11
  • @PeptideChain You probably have an outdated version of xparse.
    – egreg
    Commented Jul 9, 2019 at 8:41
  • I confirm. And overleaf.com has also an old version. Commented Jul 9, 2019 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}
2
  • Interesting, unfortunately I believe it doesn't work for unnumbered chapters or sections (as they don't do \chaptermark resp. \sectionmark).
    – frougon
    Commented Jul 8, 2019 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: tex.stackexchange.com/questions/499192 Commented Jul 9, 2019 at 7:08

You must log in to answer this question.

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