Improved reference formatting for L^{A}TEX2e

Kevin S. Ruland

April 23, 1995

This package has been completely reimplemented (since v1.1). Please note the user level commands have changed syntax. This was done because most of the packages providing hypertext/www functionality (such as, hyper, hyperref and latex2html) also modify the \newlabel structure. In order to make this package compatable with those, I have decided not to change the \newlabel structure, but have the refence type stored in the label name.

This package provides additional functionality to L^{A}TEX2e label–reference mechanism. It allows the author to “preformat” all types of labels.

A long standing problem with L^{A}TEX \ref command is that it only provides a raw number. The author is responsible for properly formatting the number correctly. For example, in order to correctly format a reference to an equation, the author must use \textup{(\ref{eq:1})}. Or similarly for a figure, Figure~(\ref{fig:2}). This way the “format” of the reference is hard coded into the paper. If the author decides to change figure references to Figure~\ref{fig:2}, she must search and replace all the strings in the tex source.

AMSL^{A}TEX has partially addressed this problem by providing the package upref and command \eqref in amsmath. This is a partial solution for equation numbers and forces equation numbers in upright style.

This package provides a more comprehensive solution by allowing the author to define various formats for the labels. For example, to label a table the author would use \label{tab:1}. To access the formatted reference the author uses

\prettyref{tab:1}. \pageref{tab:1} and \ref{tab:1} work as usual.

\prettyref is robust enough to be used within \caption and in theorem optional arguements.

Labels in the document must be of the form format:name where the string format is used to determine the format. Do not use the character : anywhere within the label except to seperate format from name. format:name must be unique for it is used as the label.

 1 \ProvidesPackage{prettyref}[1995/03/10 v2.0]

\newrefformat The command \newrefformat defines formats for pretty references.

   Usage: \newrefformat{NAME}{FORMAT}

The NAME arguement is the name of the refernece type.

The FORMAT argument is interpreted as the replacement text for an internal two-argument function. The #1 will be replaced with the count and #2 with the page number.

 2 \def\newrefformat#1#2{%
 3 \@namedef{pr@#1}##1##2{#2}}

These define the default formats for table, eq, lemma, thm, section, and figure. They also demonstrate the useage of \newrefformat.

 4 \newrefformat{eq}{\textup{(#1)}}
 5 \newrefformat{lem}{Lemma #1}
 6 \newrefformat{thm}{Theorem #1}
 7 \newrefformat{cha}{Chapter #1}
 8 \newrefformat{sec}{Section #1}
 9 \newrefformat{tab}{Table #1 on page #2}
10 \newrefformat{fig}{Figure #1 on page #2}

\prettyref The character : is used as a seperator. It must be appended to the label string to terminate the name portion.

11 \def\prettyref#1{\@prettyref#1:}

\@prettyref The internal macro \@prettyref does all the work. It takes two arguements delimited by :. The first arguement is the format name. If the format has not been defined, a warning is issued and \ref is used. Otherwise, the reference is formatted. \@prettyref uses the L^{A}TEX macros \ref and \pageref to access the \newlabel data structure. Hopefully this makes the package robust enough to use with various other pacakges.

12 \def\@prettyref#1:#2:{%
13 \expandafter\ifx\csname pr@#1\endcsname\relax%
14  \PackageWarning{prettyref}{Reference format #1\space undefined}%
15  \ref{#1:#2}%
16 \else%
17  \csname pr@#1\endcsname{\ref{#1:#2}}{\pageref{#1:#2}}%
18 \fi%

19 }