有没有办法获取标题[短]和编号以前的和下一个部分?
对于当前部分,我可以访问:
\sectiontitle % where is it defined I wonder
\thesection
因此,我可以计算例如上一节的计数器:
\the\numexpr\thesection-1\relax
我缺少的是
\prevsectiontitle, \nextsectiontitle ?
答案1
该版本将自动生成的标签(包含章节标题信息)保存到.aux
文件中,并提取上一个、当前和下一个章节标题。
计数器sectioncntr
永远不会自动重置,它会继续计算文档中的节数,并且在每个\refstepcounter
计数器上\zlabel
设置一个,它存储sectiontitle
之前用定义的属性\zref@newprop
。
短名称\Xtitle
代表,或不插入指向节标题的链接,而长名称则X
插入链接。请参阅应用程序的页脚示例。previous
current
next
\Xsectiontitle
\documentclass{article}
\usepackage[user,hyperref]{zref}
\usepackage{fancyhdr}
\usepackage{blindtext}
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[L]{\previoussectiontitle}
\fancyfoot[C]{\currenttitle}
\fancyfoot[R]{\nextsectiontitle}
}
\usepackage{xpatch}
\makeatletter
\zref@newprop{sectiontitle}[unknown]{\@currentsectiontitle}
\zref@addprop{main}{sectiontitle}
\newcounter{sectioncntr}
\newcommand{\previoustitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section-1}{}{%
\zref@extract{sectiontitle:\the\numexpr\c@section-1}{sectiontitle}%
}%
}
\newcommand{\currenttitle}{%
\zref@ifrefundefined{sectiontitle:\number\value{section}}{}{%
\zref@extract{sectiontitle:\number\value{section}}{sectiontitle}%
}%
}
\newcommand{\nexttitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section+1}{}{%
\zref@extract{sectiontitle:\the\numexpr\c@section+1}{sectiontitle}%
}%
}
\newcommand{\previoussectiontitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section-1}{}{%
\hyperlink{\zref@extract{sectiontitle:\the\numexpr\c@section-1}{anchor}}{\zref@extract{sectiontitle:\the\numexpr\c@section-1}{sectiontitle}}
}%
}
\newcommand{\currentsectiontitle}{%
\zref@ifrefundefined{sectiontitle:\number\value{section}}{}{%
\hyperlink{\zref@extract{sectiontitle:\number\value{section}}{anchor}}{\zref@extract{sectiontitle:\number\value{section}}{sectiontitle}}%
}%
}
\newcommand{\nextsectiontitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section+1}{}{%
\hyperlink{\zref@extract{sectiontitle:\the\numexpr\c@section+1}{anchor}}{\zref@extract{sectiontitle:\the\numexpr\c@section+1}{sectiontitle}}
}%
}
\xpatchcmd{\@sect}{%
\@tempskipa #5\relax
}{%
\def\@currentsectiontitle{#7}
\ifnum0=\pdfstrcmp{#1}{section}
\refstepcounter{sectioncntr}\zlabel{sectiontitle:\the\c@section}%
\fi
\@tempskipa #5\relax
}{%
\typeout{Patch Success}
}{%
\typeout{Patch Failure}
}
\makeatother
\usepackage{hyperref}
\pagestyle{plain}
\begin{document}
\section{Foo}
\subsection{Foo subsection}
\blindtext[20]
\section{Foobar}
\blindtext[20]
\section{Next Foo}
\blindtext[20]
\end{document}