# \autoref and subequations

I have an issue with \autoref and the subequations environment. I have added the () around the equation reference of \autoref (as explained here) and it works fine with normal equations. On the other hand, there is a problem when I use the subequations environment: the letter indicating the subequation is outside the brackets, e.g. I have (3.1)a, both in reference and in the equation.

In the preamble, I have

\makeatletter
\let\oldtheequation\theequation
\renewcommand\tagform@[1]{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\renewcommand\theequation{(\oldtheequation)}
\makeatother


What I would like to have is (3.1a), both for the equation and the reference. I guess I have to modify the code reported above but I am not sure how to.

I decided to modify \autoref instead of \theequation. My approach was to add the format to the name definition, so that one could also implement all the subfigure formats. This is not very efficient, but is more easily modified by users.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xstring}
\usepackage{hyperref}

\def\equationmyref#1{Equation~(\ref*{#1})}% Note: \eqref* not defined
\def\AMSmyref#1{Equation~(\ref*{#1})}% for \tag
\def\figuremyref#1{\figurename~\ref*{#1}}%
\def\tablemyref#1{\tablename~\ref*{#1}}%
\def\partmyref#1{\partname~\ref*{#1}}%
\def\appendixmyref#1{\appendixname~\ref*{#1}}%
\def\theoremmyref#1{Theorem~\ref*{#1}}%

\makeatletter
\newif\ifmyrefstar

\def\myref{\@ifstar{\myrefstartrue\my@ref}{\myrefstarfalse\my@ref}}% handkr \autoref* arguments

\newcommand{\my@ref}[1]{\@ifundefined{r@#1}{??}% #1 = label name
{\my@@ref{#1}{\getrefbykeydefault{#1}{anchor}{Doc-Start}}}}

\newcommand{\my@@ref}[2]% #1 = label, #2 = anchor
{\bgroup
\StrBefore{#2}{.}[\myrefname]% parse anchor before {.}
\@ifundefined{\myrefname myref}{\def\my@@@ref##1{\myrefname~\ref*{##1}}}%
{\expandafter\let\expandafter\my@@@ref\csname\myrefname myref\endcsname}%
\ifmyrefstar
\my@@@ref{#1}%
\else
\fi
\egroup}
\makeatother

\let\autoref=\myref

\begin{document}
\begin{subequations}
$$\label{test} a=b$$
\end{subequations}
\autoref{test}
\autoref*{test}
\end{document}