Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Say there is an equation:

\begin{equation}
r^2 = x^2 + y^2 \text{.} \label{eq:circ}
\end{equation}

I can change the autoref label by using:

\def\equationautorefname{Eq.}

Now, using

\autoref{eq:circ} something something \autoref{eq:circ}.

gives Eq. 1 something something Eq. 1.

However, I want the abbreviated versions to expand if they are at the beginning of a line. Essentially, I want this result here: Equation 1 something something Eq. 1. (all hyperlinked of-course). How do I make it happen?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Unfortunately \autoref can not distinguish whether you are at the beginning of a line or not...

But we can define an uppercase command \Autoref that does the trick (simulating what \cref and \Cref do with the cleveref package):

\newcommand{\Autoref}[1]{%
  \begingroup%
  \renewcommand\equationautorefname{Equation}%
  \autoref{#1}%
  \endgroup%
  }

Notice that I've encased the definition in \begingroup...\endgroup so to define it only locally.

\documentclass{report}
\usepackage{amsmath}

\usepackage[colorlinks]{hyperref}

\renewcommand\equationautorefname{Eq.}

\newcommand{\Autoref}[1]{%
  \begingroup%
  \renewcommand\equationautorefname{Equation}%
  \autoref{#1}%
  \endgroup%
  }

\begin{document}

\begin{equation}
r^2 = x^2 + y^2 \text{.} \label{eq:circ}
\end{equation}

\Autoref{eq:circ} something something \autoref{eq:circ}.

\end{document} 

Output:

enter image description here

share|improve this answer

The basic problem is the detection of the beginning of a sentence (I think, you meant sentence instead of line?). The following heuristics can be used:

  • Vertical mode, that means \autoref would start a new paragraph.
  • \spacefactor: The space factor is greater than thousand, if TeX thinks the preceding character ends a sentence.

The previous heuristics is implemented in \myautoref in the following example. \fullautoref and \autoref allows to specify the full and abbreviated version manually, if the heuristics might fail:

\documentclass[a5paper]{article}
\usepackage{amstext}
\usepackage[colorlinks]{hyperref}

\makeatletter
\newif\ifautorefabbr
\autorefabbrtrue

\renewcommand*{\equationautorefname}{%
  \ifautorefabbr
    Eq.\@%
  \else   
    Equation%
  \fi
}
\newcommand*{\fullautoref}{%
  \@ifstar{\@fullautoref{*}}{\@fullautoref{}}%
}
\newcommand*{\@fullautoref}[2]{%
  \begingroup
    \autorefabbrfalse
    \autoref#1{#2}%  
  \endgroup
}

\newcommand*{\myautoref}{%
  \@ifstar{\@myautoref{*}}{\@myautoref{}}%
}
\newcommand*{\@myautoref}[2]{%
  \begingroup
    \ifvmode % \myautoref starts a new paragraph
      \autorefabbrfalse
    \else
      \ifnum\spacefactor>1000 %
        \autorefabbrfalse
      \fi
    \fi  
    \autoref#1{#2}%
  \endgroup
}
\makeatother

\begin{document}
  \begin{equation}
    r^2 = x^2 + y^2 \text{.} \label{eq:circ}
  \end{equation}   

  \fullautoref{eq:circ} something something \autoref{eq:circ}.
  \fullautoref{eq:circ} something.

  \myautoref{eq:circ} something something \autoref{eq:circ}.
  \myautoref{eq:circ} something.
\end{document}

Result

Remarks:

  • The star form creates a reference without link.
share|improve this answer

You may want to use the cross-referencing commands of the cleveref package. That package's main cross-referencing command is called \cref; by default, the variant command \Cref both changes the first letter of the object to Uppercase and avoids using an abbreviation. (These default settings can be fully customized.)

As you'll see below, \cref and \Cref automatically insert parentheses around cross-referenced equation numbers. This is very much like the \eqref command of the amsmath package. If you don't like this setting, it too can be modified.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage[colorlinks=true]{hyperref}
\def\equationautorefname{Eq.}
\usepackage[nameinlink]{cleveref} % 'nameinlink' used to mimic \autoref's behavior of
                                  % including the cross-reference's name in the link
\setlength\textwidth{4in} % just for this example
\begin{document}
\begin{equation}
r^2 = x^2 + y^2 \text{.} \label{eq:circ}
\end{equation}
\autoref{eq:circ} something something \autoref{eq:circ}. (\verb+\autoref+)

\noindent
\Cref{eq:circ} something something \cref{eq:circ}. 
(\verb+\Cref+ and \verb+\cref+)
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.