Ted Pavlic
2006-04-28 17:16:32 UTC
Right now I am using something similar to the script below to do what I
want. I'm wondering if there is a better way.
The script below combines features from the prettyref and hyperref
packages. What I really want is something like \autoref that allows me
to have more control over the reference format. I know that with
\autoref I can modify \*name and \*autoref name in order to change
"Figure" to "Fig.", but I want more control over that. I want the
ability, for example, to place parentheses around equation references
("Eq. (4)") but not around table references ("Table 1").
I also want the ability to occassionally have a reference expand to a
plural format rather than a singular format. For example, sometimes I
might have a "Figs. 3 and 4" where "Figs. 3" links to a figure and "4"
links to a figure.
So what I did was create these commands:
\shortref{ reflabel }
\longref{ reflabel }
\longrefs{ reflabel }
All three hyperlink their text to the correct place. However, they
expand different text:
\shortref{ eq:my_equation } expands to "(1)"
\longref{ eq:my_equation } expands to "Eq. (1)"
\longrefs{ eq:my_equation } expands to "Eqs. (1)"
and similarly for a figure: (notice the lack of parentheses in the
expanded text)
\shortref{ fig:my_figure } expands to "1"
\longref{ fig:my_figure } expands to "Fig. 1"
\longrefs{ fig:my_figure } expands to "Figs. 1"
Downsides:
a) I REQUIRE that the first section of the label is something like
"eq:" or "fig:". A true \autoref would figure these things out
automatically without needing a special label prefix. I don't know how
to do this. I don't know if it's easy to do.
b) This method uses the prettyref package, but it looks like prettyref
is getting deprecated.
c) I'm almost positive there's a better way to do exactly this.
Any hints?
The code I'm currently using: (I've removed a few formats for brevity)
%=====
% These reference formats work with \prettyref to associate a label
name
% prefix (e.g. "fig:") with a reference format. \hyperref is used
within
% each format in order to link the entire reference to its target.
%
% \autoref could be used instead of \prettyref. In that case, the
\*name
% and/or \*autorefname macros would need to be redefined in a similar
% fashion and all \prettyref would have to be changed to \autoref.
% However, if this was done, it may be difficult to automatically place
% parentheses around equation numbers.
\usepackage{prettyref,hyperref,ifthen}
\newboolean{prettyrefname} \setboolean{prettyrefname}{false}
\newboolean{prettyrefplural} \setboolean{prettyrefplural}{false}
\newcommand{\prettyrefends}{\ifthenelse{\boolean{prettyrefplural}}{s}{}}
\newrefformat{lem}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Lemma\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{thm}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Theorem\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{cor}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Corollary\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{prop}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Proposition\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{fig}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Fig\prettyrefends{}.~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{tab}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Table\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{eq}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Eq\prettyrefends{}.~}
{}%
\textup{(\ref*{#1})}}}
% The following implements a generalized hyperlinked \eqref
%
% \longref and \longrefs cause \prettyref to make a hyperlinked
% reference with a name (e.g. ``Fig. 1'' and ``Figs. 1'' respectively)
%
% \shortref makes a hypyerlinked reference with no name (e.g. ``(1)''
% for an equation and ``1'' for a figure
\newcommand{\shortref}[1]{\setboolean{prettyrefname}{false}%
\prettyref{#1}}
\newcommand{\longref}[1]{\setboolean{prettyrefname}{true}%
\setboolean{prettyrefplural}{false}%
\prettyref{#1}}
\newcommand{\longrefs}[1]{\setboolean{prettyrefname}{true}%
\setboolean{prettyrefplural}{true}%
\prettyref{#1}}
%=====
want. I'm wondering if there is a better way.
The script below combines features from the prettyref and hyperref
packages. What I really want is something like \autoref that allows me
to have more control over the reference format. I know that with
\autoref I can modify \*name and \*autoref name in order to change
"Figure" to "Fig.", but I want more control over that. I want the
ability, for example, to place parentheses around equation references
("Eq. (4)") but not around table references ("Table 1").
I also want the ability to occassionally have a reference expand to a
plural format rather than a singular format. For example, sometimes I
might have a "Figs. 3 and 4" where "Figs. 3" links to a figure and "4"
links to a figure.
So what I did was create these commands:
\shortref{ reflabel }
\longref{ reflabel }
\longrefs{ reflabel }
All three hyperlink their text to the correct place. However, they
expand different text:
\shortref{ eq:my_equation } expands to "(1)"
\longref{ eq:my_equation } expands to "Eq. (1)"
\longrefs{ eq:my_equation } expands to "Eqs. (1)"
and similarly for a figure: (notice the lack of parentheses in the
expanded text)
\shortref{ fig:my_figure } expands to "1"
\longref{ fig:my_figure } expands to "Fig. 1"
\longrefs{ fig:my_figure } expands to "Figs. 1"
Downsides:
a) I REQUIRE that the first section of the label is something like
"eq:" or "fig:". A true \autoref would figure these things out
automatically without needing a special label prefix. I don't know how
to do this. I don't know if it's easy to do.
b) This method uses the prettyref package, but it looks like prettyref
is getting deprecated.
c) I'm almost positive there's a better way to do exactly this.
Any hints?
The code I'm currently using: (I've removed a few formats for brevity)
%=====
% These reference formats work with \prettyref to associate a label
name
% prefix (e.g. "fig:") with a reference format. \hyperref is used
within
% each format in order to link the entire reference to its target.
%
% \autoref could be used instead of \prettyref. In that case, the
\*name
% and/or \*autorefname macros would need to be redefined in a similar
% fashion and all \prettyref would have to be changed to \autoref.
% However, if this was done, it may be difficult to automatically place
% parentheses around equation numbers.
\usepackage{prettyref,hyperref,ifthen}
\newboolean{prettyrefname} \setboolean{prettyrefname}{false}
\newboolean{prettyrefplural} \setboolean{prettyrefplural}{false}
\newcommand{\prettyrefends}{\ifthenelse{\boolean{prettyrefplural}}{s}{}}
\newrefformat{lem}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Lemma\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{thm}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Theorem\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{cor}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Corollary\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{prop}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Proposition\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{fig}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Fig\prettyrefends{}.~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{tab}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Table\prettyrefends{}~}
{}%
\textup{\ref*{#1}}}}
\newrefformat{eq}
{\hyperref[#1]{\ifthenelse{\boolean{prettyrefname}}
{Eq\prettyrefends{}.~}
{}%
\textup{(\ref*{#1})}}}
% The following implements a generalized hyperlinked \eqref
%
% \longref and \longrefs cause \prettyref to make a hyperlinked
% reference with a name (e.g. ``Fig. 1'' and ``Figs. 1'' respectively)
%
% \shortref makes a hypyerlinked reference with no name (e.g. ``(1)''
% for an equation and ``1'' for a figure
\newcommand{\shortref}[1]{\setboolean{prettyrefname}{false}%
\prettyref{#1}}
\newcommand{\longref}[1]{\setboolean{prettyrefname}{true}%
\setboolean{prettyrefplural}{false}%
\prettyref{#1}}
\newcommand{\longrefs}[1]{\setboolean{prettyrefname}{true}%
\setboolean{prettyrefplural}{true}%
\prettyref{#1}}
%=====