[Tex/LaTex] Renew \ref command doesn’t work

cross-referencing

I'm trying renew \ref command to auto detect equation and figure. All my labels of equations start with "eq:" and all labels of figures with "fig:".

So I try this way, but it isn't works for me:

\documentclass[a4paper, 12pt]{article}
\usepackage{graphicx}
\usepackage[colorlinks=true]{hyperref}

\makeatletter
% Is it equation? => add brackets
\newcommand\myref[1]{\in@{eq:}{#1} \ifin@ (\ref{#1}) \else \ref{#1} \fi }
\makeatother

%\let\ref\myref
\renewcommand{\ref}[1]{\myref{#1}}
\renewcommand{\cite}[1]{\myref{#1}}

\begin{document}
Cite to eq: \cite{eq:test}, cite to fig: \cite{fig:test} % WORK FINE, add brackets
Ref to eq: \ref{eq:test}, ref to fig: \ref{fig:test}     % DOESN'T WORK :-(

\begin{figure}
 \includegraphics[width=13cm]{img/test.png}
 \caption{test}
 \label{fig:test}
\end{figure}

\begin{equation}
 a = b
 \label{eq:test}
\end{equation}

\end{document}

The output is, that \cite behave as I want (add brackets, if label starts with "eq"), but \ref doesn't. It is ignoring me:-(

(I inspired in thread \renewcommand{\cite} does not work ; so I tried command \let too)

I know about \eqref, but I have in many documents \ref and now I want to highlight equations with brackets.

Some idea?

Thanks.

Best Answer

Something (eg hyperref) is likely redefining \ref in the \AtBeginDocument hook. You can try moving your definitions out of the preamble. For the kind of thing you are trying to do I would recommend checking into the cleveref package, which can do many intelligent things with referencing commands.

Related Question