如何获取当前章节/节的首页页码?

如何获取当前章节/节的首页页码?

我想得到

  1. 当前章节第一页的页码
  2. 当前章节第一页的页码

是否可以?

目标是将章节/部分添加到辅助目录中,但目前我得到的是实际页面,而不是当前章节第一页的页码

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

编辑我不会维护章节和节的标签。此外,整个过程必须是自动的,也就是说程序不知道是否必须使用标签sec:functionsec:relation;并且我不想定义形式为的标签sec:1:3

编辑2一种方法是修改命令chapter以便将页码保存在变量中(我不知道如何做!),但也许有一种更干净的方法。

答案1

重新定义\chapter\section发出合适的命令,该命令是根据每个命令\label的计数器自动构建的。\chapter\section

标签可以存储在命令中\currentchapterlabel,并且\currentsectionlabel任何\chapter\section命令都会覆盖。

最后,我们可以定义\currentchapterpage\currentsectionpage执行适当的\pageref命令。

\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}

在此处输入图片描述

答案2

\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}

相关内容