这是一本书的骨架 .tex 文件
\documentclass[11pt,a4paper]{book}
\usepackage{makeidx}
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\makeindex
\begin{document}
\tableofcontents
\mainmatter
\chapter{First chapter}
\cite{darwin1859origin}
\index{Darwin}
The Index is on page ?. The bibliography is on page ?.
\backmatter
\bibliographystyle{plain}
\bibliography{references}
\printindex
\end{document}
这是 references.bib
@article{darwin1859origin,
title={On the origin of species: facsimile of the first edition},
author={Darwin, Charles},
year={1859},
publisher={LONDON: JOHN MURRAY, ALBEMARLE STREET.}
}
我如何获取参考书目和索引的页码?显然,它们是以某种方式可用的,因为生成目录的代码知道它们是什么。
答案1
对于索引而言,这并不是特别强大,但只要索引没有进一步定制并且tocbibbind
没有以某种不兼容的方式更新,它就会起作用。
鉴于多分支代码提供的功能,很难知道在索引的情况下将标签插入到哪里tocbibbind
。显然,您可以进行适当的测试和修补,但如果情况发生变化,这仍然会失败。所以我选择了\thispagestyle
插入触发器,无论条件如何,它都会执行。我们为这个命令在环境中添加了一个一次性钩子theindex
,因为将标签直接添加到环境中使用的钩子要么太早,要么太晚。
\begin{filecontents}[overwrite]{\jobname.bib}
@article{darwin1859origin,
title={On the origin of species: facsimile of the first edition},
author={Darwin, Charles},
year={1859},
publisher={LONDON: JOHN MURRAY, ALBEMARLE STREET.}
}
\end{filecontents}
\documentclass[11pt,a4paper]{book}
\usepackage{makeidx}
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\makeindex
\AddToHook{env/thebibitemlist/begin} {\label{ch:bibstartshere}}
\AddToHook{env/theindex/begin} {%
\AddToHookNext{cmd/thispagestyle/after}{\label{ch:indexstartshere}}%
}
\begin{document}
\tableofcontents
\mainmatter
\chapter{First chapter}
\cite{darwin1859origin}
\index{Darwin}
The Index is on page \pageref{ch:indexstartshere}. The bibliography is on page \pageref{ch:bibstartshere}.
\backmatter
\bibliographystyle{plain}
\bibliography{\jobname}
\printindex
\end{document}