我想得到
- 当前章节第一页的页码
- 当前章节第一页的页码
是否可以?
目标是将章节/部分添加到辅助目录中,但目前我得到的是实际页面,而不是当前章节第一页的页码
\addcontentsline{todo}{section}%
{\protect\numberline{\thesection}{\Sectionname}}%
编辑我不会维护章节和节的标签。此外,整个过程必须是自动的,也就是说程序不知道是否必须使用标签sec:function
或sec: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}