# \autoref does not expand abbreviation automatically at beginning of line

Say there is an equation:

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


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?

-

## 3 Answers

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}

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

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

\end{document}


Output:

-

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}
$$r^2 = x^2 + y^2 \text{.} \label{eq:circ}$$

\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}


Remarks:

• The star form creates a reference without link.
-

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.

\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}
$$r^2 = x^2 + y^2 \text{.} \label{eq:circ}$$
\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}

-