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

Sebastian Rahtz

1994/10/02

Contents

1 Introduction 1

2 History 1

3 Usage and frontend 3

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) 1 ⟨∗package⟩

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

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 a fourth field (empty by default).

 4 \def\strip@period#1.\relax#2\@@@{#1}
 5 \def\label#1{%
 6 \@bsphack
 7 \bgroup
 8 \let\label\@gobble

∗This file has version number v1.5i, last revised 1994/10/02.

 9 \def\ref{\protect\ref}%
10 \protected@write\@auxout{}%
11  {\string\newlabel{#1}{%
12  {\@currentlabel}%
13  {\thepage}%
14  {\expandafter\strip@period\@currentlabelname\relax.\relax\@@@}%
15  {\@currentHref}%
16  }}%
17 \egroup
18 \@esphack

19 }

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

20 \def\@writetocindents{%
21 \begingroup
22 \@for\@tempa:=-1,0,1,2,3\do{%
23  \immediate\write\@auxout{%
24  \string\newlabel{tocindent\@tempa}{%
25  \csname r@tocindent\@tempa\endcsname}{}{}}%
26 }%
27 \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.

28 \let\old@sect\@sect
29 \def\@sect#1#2#3#4#5#6[#7]#8{%
30 \def\@currentlabelname{#7}%
31 \old@sect{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{\Sectionformat{#8}{#2}}%

32 }

and now the unnumbered ones

33 \let\old@ssect\@ssect
34 \def\@ssect#1#2#3#4#5{%
35 \def\@currentlabelname{#5}%
36 \old@ssect{#1}{#2}{#3}{#4}{\Sectionformat{#5}{#1}}%

37 }

Parts and chapters are treated differently. sigh.

38 \let\old@part\@part
39 \def\@part[#1]#2{%
40 \def\@currentlabelname{#1}%
41 \old@part[{#1}]{#2}%

42 }

43 \let\old@chapter\@chapter
44 \def\@chapter[#1]#2{%
45 \def\@currentlabelname{#1}%
46 \old@chapter[{#1}]{#2}%

48 \let\old@schapter\@schapter
49 \def\@schapter#1{%
50 \def\@currentlabelname{#1}%
51 \old@schapter{#1}%

52 }

We default the label and hypertext reference to be empty.

53 \def\@currentlabelname{}
54 \def\@currentHref{}

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

55 \long\def\@firstoffour#1#2#3#4{#1}
56 \long\def\@secondoffour#1#2#3#4{#2}
57 \long\def\@thirdoffour#1#2#3#4{#3}
58 \long\def\@fourthoffour#1#2#3#4{#4}

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

59 \def\ref#1{\expandafter\@setref\csname r@#1\endcsname\@firstoffour{#1}}
60 \def\pageref#1{\expandafter\@setref\csname r@#1\endcsname
61  \@secondoffour{#1}}

3 Usage and frontend

Access the (third) name part with \nameref;

62 \def\nameref#1{\expandafter\@setref\csname r@#1\endcsname \@thirdoffour{#1}}

An extended form which gives title and page number.

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

an example alternative for BNC book for Lou Burnard:

65 \def\LouSectionformat#1#2{%
66 \ifnum#2=1
67  \if@twoside
68  \ifthenelse{\isodd{\thepage}}%
69  {\raggedright}%
70  {\raggedleft}%
71  \else
72  \raggedleft
73  \fi
74  #1 \\[2pt]\rule{\columnwidth}{1.5pt}
75 \else
76  #1
77 \fi

78 }

79 ⟨/package⟩