Document ClassesHelp with "\autoref" of "hyperref"

Information and discussion about specific document classes and how to create your own document classes.
User avatar
renno
Posts: 54
Joined: Thu Jun 07, 2007 7:16 pm

Help with "\autoref" of "hyperref"

Post by renno »

Hello all,

I am using "\autoref" from "hyperref" which is a great command. I have couple of things on mind that I cannot implement. I will set my examples for referencing an equation. The items start with the most important...

1. The other thing, and it mainly applies to Equation. How can I change it to produce the equation number between parentheses. In other word, I would like "\autoref{eq_1}" to produce "Equation (1)", not "Equation 1"...

2. I would like to be able to set an options that if the "\autoref" is used at the beginning of a sentence, "Equation" would be generated. Otherwise, generate "Eq.". This is standard practice in many scientific journals, but I imagine this would be hard to fix...

I would appreciate your help...

Jamil M. Renno
Virginia Tech
If there is no way, we will make one...
Hanibal

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
countbela666
Posts: 64
Joined: Thu Apr 26, 2007 2:44 pm

Help with "\autoref" of "hyperref"

Post by countbela666 »

Hello Jamil,
renno wrote: 1. How can I change it to produce the equation number between parentheses. In other word, I would like "\autoref{eq_1}" to produce "Equation (1)", not "Equation 1"...
If you are using amsmath, you can put the following code into your preamble (after loading amsmath and hyperref):

Code: Select all

\makeatletter
\def\tagform@#1{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\let\orgtheequation\theequation
\def\theequation{(\orgtheequation)}
\makeatother
renno wrote: 2. I would like to be able to set an options that if the "\autoref" is used at the beginning of a sentence, "Equation" would be generated. Otherwise, generate "Eq.".
This is really hard to implement. Another possibility would be to use two different commands: \Autoref{...} for the beginning of a sentence and \autoref{...} for a reference inside a sentence. You could implement this as follows:

Code: Select all

\let\orgautoref\autoref
\providecommand{\Autoref}[1]{\def\equationautorefname{Equation}\orgautoref{#1}}
\renewcommand{\autoref}[1]{\def\equationautorefname{Eq.}\orgautoref{#1}}
Here a minimal working example (MWE) that implements both features:

Code: Select all

\documentclass{report}
\usepackage{amsmath,hyperref}
\makeatletter
\def\tagform@#1{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\let\orgtheequation\theequation
\def\theequation{(\orgtheequation)}
\makeatother
\let\orgautoref\autoref
\providecommand{\Autoref}[1]{\def\equationautorefname{Equation}\orgautoref{#1}}
\renewcommand{\autoref}[1]{\def\equationautorefname{Eq.}\orgautoref{#1}}
\begin{document}
  \chapter{Example}
  \begin{equation}a^2+b^2=c^2 \label{foo}\end{equation}
  \Autoref{foo} shows \dots
  \begin{equation}a^2+b^2=c^2 \label{bar}\end{equation}
  As shown in \autoref{bar} \dots
\end{document}
Regards
Marcel
Listen to me children of the night, beyond the doors of darkness you will find
a thousand worlds for you to see here, take my hand and follow me...
User avatar
renno
Posts: 54
Joined: Thu Jun 07, 2007 7:16 pm

Help with "\autoref" of "hyperref"

Post by renno »

Dear Marcel,

Thank you very much. The code works perfectly and this produces exactly what I wanted. I augmented the code to produce "Figure" and "Fig." for figures and sub-figures at the beginning of a sentence and in a sentence respectively.

I am including the modified code for everybody's convenience...

% ======================================================
% \Autoref is for the beginning of the sentence
\let\orgautoref\autoref
\providecommand{\Autoref}[1]
{
\def\equationautorefname{Equation}
\def\figureautorefname{Figure}
\def\subfigureautorefname{Figure}
\orgautoref{#1}
}
% \autoref is used inside the sentence to produce Fig., and Eq. for figures, subfigures, and equations
\renewcommand{\autoref}[1]
{
\def\equationautorefname{Eq.}
\def\figureautorefname{Fig.}
\def\subfigureautorefname{Fig.}
\orgautoref{#1}
}

% ====================================================

Thanks again Marcel for the great response...

Have a good one...
If there is no way, we will make one...
Hanibal
User avatar
renno
Posts: 54
Joined: Thu Jun 07, 2007 7:16 pm

Help with "\autoref" of "hyperref"

Post by renno »

Hello all,

Just a follow up on my last follow up. The last code I posted causes unnecessary spaces before Figure, Fig., Equation, Eq. because of having each definition on a new line. The following works...

% ====================================================
% \Autoref is for the beginning of the sentence
\let\orgautoref\autoref
\providecommand{\Autoref}[1]{\def\equationautorefname{Equation}
\def\figureautorefname{Figure}\def\subfigureautorefname{Figure}\orgautoref{#1}}
% \autoref is used inside the sentence to produce Fig., and Eq. for figures, subfigures, and equations
\renewcommand{\autoref}[1]{\def\equationautorefname{Eq.}
\def\figureautorefname{Fig.}\def\subfigureautorefname{Fig.}\orgautoref{#1}}

% ====================================================

Sorry for any confusion...

:oops:

I will be more careful from now on...
If there is no way, we will make one...
Hanibal
User avatar
countbela666
Posts: 64
Joined: Thu Apr 26, 2007 2:44 pm

Help with "\autoref" of "hyperref"

Post by countbela666 »

If you mask every end of a line with a comment inside the new macros, it should be working as well:

Code: Select all

% \Autoref is for the beginning of the sentence
\let\orgautoref\autoref
\providecommand{\Autoref}[1]
{%
\def\equationautorefname{Equation}%
\def\figureautorefname{Figure}%
\def\subfigureautorefname{Figure}%
\orgautoref{#1}%
}
% \autoref is used inside the sentence to produce Fig., and Eq. for figures, subfigures, and equations
\renewcommand{\autoref}[1]
{%
\def\equationautorefname{Eq.}%
\def\figureautorefname{Fig.}%
\def\subfigureautorefname{Fig.}%
\orgautoref{#1}%
}
Regards
Marcel
Listen to me children of the night, beyond the doors of darkness you will find
a thousand worlds for you to see here, take my hand and follow me...
User avatar
renno
Posts: 54
Joined: Thu Jun 07, 2007 7:16 pm

Re: Help with "\autoref" of "hyperref"

Post by renno »

Dear Marcel,

Thank you very much for the valuable comment. I was not aware of this at all...

Have a good day...
If there is no way, we will make one...
Hanibal
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Re: Help with "\autoref" of "hyperref"

Post by Ted »

Three quick things...

1. Just to verify, it looks like the parentheses modification should give the same result as surrounding the equation number in an \eqref. That is, the parentheses will always show up in roman normal shape, right?

2. Another idea. You could use these ideas to implement macros \autorefs and \Autorefs. Then you could do something like...

\Autorefs{eq_1} and \eqref{eq_2} show stuff.

which would produce:

Equations (1) and (2) show stuff.

3. Does anyone know if \eqref* acts like \ref*? If not, would a good way to create an \eqref* be to define the command as eqref surrounded in a NoHyper environment?

Thanks!
--Ted
-- Ted [home/blog]
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Help with "\autoref" of "hyperref"

Post by Ted »

countbela666 wrote: If you are using amsmath, you can put the following code into your preamble (after loading amsmath and hyperref):

Code: Select all

\makeatletter
\def\tagform@#1{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\let\orgtheequation\theequation
\def\theequation{(\orgtheequation)}
\makeatother


Maybe this is a better idea:

Code: Select all

\usepackage{varioref}
...
\labelformat{equation}{\textup{(#1)}}
This seems cleaner to me (and possibly more acceptable to journal editors).

Additionally, you can do this for enumeration counters too. Consider this example (that makes use of paralist):

Code: Select all

\usepackage{paralist}
\usepackage{varioref}
...
\labelformat{enumi}{\textup{(#1)}}
...
\begin{enumerate}[(i)]
\item This is a test. \label{item:1}
\item This is another test. \label{item:2}
\end{enumerate}
...
Now we refer to \autoref{item:1} and \ref{item:2}.
The last line will produce:

Code: Select all

Now we refer to item (i) and (ii).
varioref is an old package put together by Frank Mittelbach, the first author of TLC2.
-- Ted [home/blog]
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Help with "\autoref" of "hyperref"

Post by Ted »

It is better to drop the [1] and #1 stuff since \orgautoref handles that by itself. Consider the following, which defines a command that takes NO ARGUMENTS:

Code: Select all

% \Autoref is for the beginning of the sentence
\let\orgautoref\autoref
\providecommand{\Autoref}{%
\def\equationautorefname{Equation}%
\def\figureautorefname{Figure}%
\def\subfigureautorefname{Figure}%
\orgautoref}
% \autoref is used inside the sentence to produce Fig., and Eq. for figures, subfigures, and equations
\renewcommand{\autoref}{%
\def\equationautorefname{Eq.}%
\def\figureautorefname{Fig.}%
\def\subfigureautorefname{Fig.}%
\orgautoref}
This works exactly the same as above. HOWEVER, it ALSO allows for the starred forms of \*utoref to work. For example,

Code: Select all

\Autoref*{eq:1} is below.
will produce an UNLINKED version (and yet still expanded) version of:

Code: Select all

\Autoref{eq:1} is below.
That is, the former produces
Equation (1) is below.
and the latter produces (where blue indicates where a hyperlink would be)
Equation (1) is below.
-- Ted [home/blog]
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Re: Help with "\autoref" of "hyperref"

Post by Ted »

The preamble I've posted at (scroll the page until you see the LaTeX code):

http://www.tedpavlic.com/post_preamble_ ... th_bio.php

uses a combination of all of these things. In fact, this thread was motivation for much of it. It was customized for a particular journal, but the first couple lines can be removed and it can be used elsewhere.
-- Ted [home/blog]
Post Reply