Section name references in L^{A}TEX^{∗}

Sebastian Rahtz

1997/11/24

Contents

1 Introduction

Cross-referencing to include the name of the section, rather than just the number or page. This works by redefining some of the inside L^{A}TEX macros, so it is vulnerable to future changes.

2 History

Sebastian Rahtz, for Lou Burnard, March 15th 1994; SPQR CERN July 1994; Last mod. Sept. 3th MG; Sept. 19th SPQR; April 11th 1995 SPQR (added section formatting hook); April 14th 1995 SPQR (compatibility with hyperref); June 22 1995 SPQR (removed typeout from Sectionformat); September 22 1997 added varioref hacks by Corey Minyard; September 28th changes by David Carlisle.

1 ⟨∗package⟩

 2 \ProvidesPackage{nameref}[\filedate\space Cross-referencing by name of section]

We redefine \label so that it also writes the name of the current section to the .aux file; if the name ends in a dot, we zap it. To allow for the hyperref package, also write fourth and fifth fields (empty by default; used for cross-ref name, and file).

 3 \def\strip@period#1.\relax#2\@@@{#1}
 4 \def\label#1{%
 5 \@bsphack
 6 \bgroup
 7 \let\label\@gobble
 8 \def\ref{\protect\ref}%
 9 \edef\@currentlabstr{\expandafter\strip@prefix\meaning\@currentlabelname}%
10 \protected@write\@auxout{}%
11  {\string\newlabel{#1}{%
12  {\@currentlabel}%

∗This file has version number 2.10, last revised 1997/11/24.

13  {\thepage}%
14  {\expandafter\strip@period\@currentlabstr\relax.\relax\@@@}%
15  {\@currentHref}{}%
16  }}%
17 \egroup
18 \@esphack

19 }

20 \let\ltx@label\label

Needed for the subeqnarray package.

21 \@ifundefined{slabel}{}{%
22 \def\slabel#1{%
23 \@bsphack
24 \if@filesw
25  {\let\label\@gobble
26  \def\ref{\protect\ref}%
27  \edef\@currentlabstr{\expandafter\strip@prefix\meaning\@currentlabelname}%
28  \let\thepage\relax
29  \def\protect{\noexpand\noexpand\noexpand}%
30  \edef\@tempa{\write\@auxout{\string
31  \newlabel{#1}{%
32  {\thesubequation}%
33  {\thepage}%
34  {\expandafter\strip@period\@currentlabstr\relax.\relax\@@@}%
35  {\@currentHref}{}%
36  }}}%
37  \expandafter}\@tempa
38  \if@nobreak \ifvmode\nobreak\fi\fi
39 \fi\@esphack}}

Overload an AMS L^{A}TEX command, which uses \newlabel. Sigh!

40 \def\@writetocindents@{%
41 \begingroup
42 \@for\@tempa:=-1,0,1,2,3\do{%
43  \immediate\write\@auxout{%
44  \string\newlabel{tocindent\@tempa}{%
45  \csname r@tocindent\@tempa\endcsname{}{}{}{}}}%
46 }%
47 \endgroup}

Add to the underlying section heading macros so that they note the section name for use by label.

If a section heading or the like has a \label in it, we need to extract it, or subsequent processing breaks. This is done by the label-writing routine.

First the numbered sections. While we are about it, put in a useful section formatting macro.

48 \let\old@sect\@sect
49 \def\@sect#1#2#3#4#5#6[#7]#8{%
50 \def\@currentlabelname{#7}%
51 \old@sect{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{\protect\Sectionformat{#8}{#2}}%

52 }

and now the unnumbered ones

53 \let\old@ssect\@ssect
54 \def\@ssect#1#2#3#4#5{%
55 \def\@currentlabelname{#5}%
56 \old@ssect{#1}{#2}{#3}{#4}{\Sectionformat{#5}{#1}}%

57 }

Parts and chapters are treated differently. sigh.

58 \let\old@part\@part
59 \def\@part[#1]#2{%
60 \def\@currentlabelname{#1}%
61 \old@part[{#1}]{#2}%

62 }

63 \let\old@chapter\@chapter
64 \def\@chapter[#1]#2{%
65 \def\@currentlabelname{#1}%
66 \old@chapter[{#1}]{#2}%

67 }

68 \let\old@schapter\@schapter
69 \def\@schapter#1{%
70 \def\@currentlabelname{#1}%
71 \old@schapter{#1}%

72 }

We default the label and hypertext reference to be empty.

73 \def\@currentlabelname{}
74 \def\@currentHref{}

Since the second part of the preserved reference now has five parts (number, page, name, hypertext reference and file), we need extra utility macros:

75 \long\def\@firstoffive#1#2#3#4#5{#1}
76 \long\def\@secondoffive#1#2#3#4#5{#2}
77 \long\def\@thirdoffive#1#2#3#4#5{#3}
78 \long\def\@fourthoffive#1#2#3#4#5{#4}
79 \long\def\@fifthoffive#1#2#3#4#5{#5}

We have to redefine \ref and \pageref to know about extra reference elements. Make them robust.

80 \def\T@ref#1{\expandafter\@setref\csname r@#1\endcsname\@firstoffive{#1}}
81 \def\T@pageref#1{\expandafter\@setref\csname r@#1\endcsname
82  \@secondoffive{#1}}
83 \def\ref{\protect\T@ref}
84 \def\pageref{\protect\T@pageref}

3 Usage and frontend

Access the (third) name part with \nameref;

85 \def\T@nameref#1{\expandafter\@setref\csname r@#1\endcsname
86 \@thirdoffive{#1}}
87 \def\nameref{\protect\T@nameref}

An extended form which gives title and page number.

 88 \def\Nameref#1{`\nameref{#1}' on page~\pageref{#1}}
    The default for \Sectionformat
 89 \def\Sectionformat#1#2{#1}

3.1 Compatibility with varioref

[This section was derived by Corey Minyard minyard@acm.org from the varioref package]

 90 \ifx\csname @@vpageref\endcsname\relax\else
 91 \def\@@vpageref#1[#2]#3{%
 92 \leavevmode\unskip
 93 \global\advance\c@vrcnt\@ne
 94 \@ifundefined{r@\the\c@vrcnt @vr}%
 95  {\@namedef{r@\the\c@vrcnt @vr}{{??}{??}{??}{??}{??}}}{}%
 96 \edef\@tempa{\expandafter\expandafter\expandafter
 97  \@secondoffive\csname r@\the\c@vrcnt @vr\endcsname}%
 98 \@ifundefined{r@\the\c@vrcnt @xvr}%
 99  {\@namedef{r@\the\c@vrcnt @xvr}{{??}{??}{??}{??}{??}}}{}%
100 \edef\@tempb{\expandafter\expandafter\expandafter
101  \@secondoffive\csname r@\the\c@vrcnt @xvr\endcsname}%
102 {\let\@currentlabel\@empty
103 \expandafter\label\expandafter{\the\c@vrcnt @xvr}}%
104 \ifx\@tempa\@tempb\else
105 \vref@err{\noexpand\vref at page boundary
106  \@tempb-\@tempa\space (may loop)%

107 }%

108 \fi
109 \@ifundefined{r@#3}{\@namedef{r@#3}{{??}{??}{??}{??}{??}}}{}%
110 \edef\@tempb{\expandafter\expandafter\expandafter
111  \@secondoffive\csname r@#3\endcsname}%
112 \space
113 \ifx\@tempa\@tempb
114  #1%
115 \else
116  #2%
117  \is@pos@number\@tempb

118 {%

119  \is@pos@number\@tempa
120  {\@tempcnta\@tempa
121  \advance\@tempcnta\@ne

122 }%

123  {\@tempcnta\maxdimen}%
124  \ifnum \@tempb =\@tempcnta
125  \ifodd\@tempcnta
126  \if@twoside
127  \reftextfaceafter
128  \else

129  \reftextafter
130  \fi
131  \else
132  \reftextafter
133  \fi
134  \else
135  \advance\@tempcnta-2
136  \ifnum \@tempb =\@tempcnta
137  \ifodd\@tempcnta
138  \reftextbefore
139  \else
140  \if@twoside
141  \reftextfacebefore
142  \else
143  \reftextbefore
144  \fi
145  \fi
146  \else
147  \reftextfaraway{#3}%
148  \fi
149  \fi

150 }%

151  {\reftextfaraway{#3}}%
152 \fi
153 {\let\@currentlabel\@empty
154 \expandafter\label\expandafter{\the\c@vrcnt @vr}}%

155 }%

156 \DeclareRobustCommand\vnameref[1]{%
157 \unskip~\nameref{#1}%
158 \@vpageref[\unskip]{#1}%
159 } \fi

160 ⟨/package⟩