# Redefining \ref such that it is \eqref when you use \ref{eq: …} and then it is \cref else

Pretty much what the title says. I want to use only \ref in my latex document, but when I write \ref{eq: ...} I want the \ref to be typeset as \eqref, and when I use \ref{sec: ...} or \ref{fig: ...} or anything else, I want it to by typeset as \cref.

• There would be a problem if \eqref or \cref use \ref internally. You will almost certainly need a new name. – John Kormylo May 29 '18 at 13:46
• Why not \cref for everything and configure the equation type? – gusbrs May 29 '18 at 13:48
• Rather than redefine \ref, would you be willing to redefine \cref to that it acts like \eqref if the object being cross-referenced is associated with a counter named equation? – Mico May 29 '18 at 13:49
• Redefining \cref to act like \eqref (like Mico said) would work as well. – soer9606 May 29 '18 at 13:54
• I would really strongly advise you not to to this, changing the semantics of such a core latex feature will mean that your documents will be hard to translate to other formats and fragments of your document can't be moved to normal latex documents and more generally, things will break. – David Carlisle May 29 '18 at 14:51

Rather than redefine a low-level command such as \ref, I would redefine \cref so that it doesn't output a label (typically, "equation" or "equations") when its argument is some item that's associated with the counter equation. All you'd need to do is to run the directive

\crefname{equation}{}{}


after loading the cleveref package. Optionally, if you happen to load the hyperref package as well and do not wish the parentheses that surround a cross-referenced equation number to be part of the hyperlink, you may also want to run the directive

\creflabelformat{equation}{\textup{(#2#1#3)}}


The default is \creflabelformat{equation}{\textup{#2(#1)#3}}. Note the difference in where the opening and closing parentheses are placed.

In the following MWE (minimum working example), the hyperref package is loaded with the option nameinlink and the cleveref package is loaded with the option nameinlink to highlight what is being produced by \cref. The text mode is deliberately set to italics to demonstrate that both \eqref and \cref will typeset cross-referenced equation numbers (and their associated parentheses) in upright, i.e., "Roman" mode. This is a standard typographic convention.

\documentclass{article}
\usepackage{amsmath}              % for "\eqref" command
\usepackage[colorlinks]{hyperref} % just for this example
\crefname{equation}{}{}
\creflabelformat{equation}{\textup{(#2#1#3)}} % optional

\begin{document}
\section{First}\label{sec:first}
$$\label{eq:pyth}a^2+b^2=c^2$$
$$\label{eq:triv}1+1=2$$
\begin{figure}\caption{A figure}\label{fig:test}\end{figure}

\section{Second}

\itshape % Just to verify that equation numbers will be typeset
% in upright ("Roman") mode by both \eqref and \cref.

%% An example with "\eqref"
Cross-references to equations \eqref{eq:pyth} and \eqref{eq:triv}.

\noindent%% An example with "\cref"
Cross-references to equations \cref{eq:pyth,eq:triv,sec:first,fig:test}.
\end{document}


I don't recommend redefining the \ref-macro as packages like hyperref that modify that macro might rely on its definition.

How about defining a macro with a name which is not already in use, e.g., \MyRefChooseCommand which examines whether its argument has leading phrases 'eq:' or 'fig:' or 'sec:' or whatsoever and then applies the appropriate referencing-command on that argument?

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}

\begingroup
\makeatletter
\@firstofone{%
\endgroup
%%----------------------------------------------------------------------
%% Paraphernalia:
%%......................................................................
\newcommand\UD@Exchange[2]{#2#1}%
%%----------------------------------------------------------------------
%% Check whether argument is empty:
%%......................................................................
%% \UD@CheckWhetherNull{<argument which is to be checked>}%
%%                   {<tokens to be delivered in case that argument
%%                     which is to be checked is empty>}%
%%                   {<tokens to be delivered in case that argument
%%                     which is to be checked is not empty>}%
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}%
\@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}%
}%
%%----------------------------------------------------------------------
%% Check whether argument's leading tokens form a specific token-sequence:
%%......................................................................
%%                           {<a single non space token> that does
%%                             _not_ occur in <token sequence>>}%
%%                           {<internal token-check-macro>}%
%%                           {<argument which is to be checked>}%
%%                           {<tokens to be delivered in case <argument
%%                             which is to be checked> has <token sequence>
%%                           {<tokens to be delivered in case <argument
%%                             which is to be checked> does not have
%%                             <token sequence> as leading tokens>}%
\romannumeral0\UD@CheckWhetherNull{#4}%
{\expandafter\expandafter\@firstoftwo{ }{}\@secondoftwo}%
{\expandafter\@secondoftwo\string{#3#2#4#1}{}}%
}%
\expandafter\UD@CheckWhetherNull\expandafter{\@firstoftwo{}#1}%
{\UD@Exchange{\@firstoftwo}}{\UD@Exchange{\@secondoftwo}}%
{\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter}\expandafter\expandafter
\expandafter}\expandafter\@secondoftwo\expandafter{\string}%
}%
%%----------------------------------------------------------------------
%% \UD@internaltokencheckdefiner{<internal token-check-macro>}%
%%                              {<token sequence>}%
%% Defines <internal token-check-macro> to snap everything
%% until reaching <token sequence>-sequence and pass that to
%%......................................................................
\newcommand\UD@internaltokencheckdefiner[2]{%
}%
\UD@internaltokencheckdefiner{\@CheckEq}{eq:}%
\UD@internaltokencheckdefiner{\@CheckSec}{sec:}%
\UD@internaltokencheckdefiner{\@CheckFig}{fig:}%
%%----------------------------------------------------------------------
%% \MyRefChooseCommand{<label>}
%%
%% Examines the <label>-argument.
%% Yields \eqref{<label>} in case <label> begins with 'eq:'
%% Yields \cref{<label>} in case <label> begins with 'sec:'
%% Yields \cref{<label>} in case <label> begins with 'fig:'
%% Yields \cref{<label>} in case <label> begins with 'something else.
%%
%% The branches for 'sec:' and 'fig:' are somewhat pointless.
%% They are just there for exhibiting how forther forking
%% can be implemented.
%%......................................................................
\newcommand\MyRefChooseCommand[1]{%
\eqref %<- name of label starts with 'eq:'
}{%
\cref %<- name of label starts with 'sec:'
}{%
\cref %<- name of label starts with 'fig:'
}{%
\cref %<- name of label starts with something else.
}%
}%
}{#1}%
}%
}%

\begin{document}

\section{A nice section}\label{sec:nice}

$$a^2+b^2=c^2\label{eq:1}$$

Referencing via \verb|\ref|-command yields:

\verb|\ref{sec:nice}| $$\to$$ \ref{sec:nice}

\verb|\ref{eq:1}| $$\to$$ \ref{eq:1}

Referencing via \verb|\MyRefChooseCommand|-command yields:

\verb|\MyRefChooseCommand{sec:nice}| $$\to$$ \MyRefChooseCommand{sec:nice}

\verb|\MyRefChooseCommand{eq:1}| $$\to$$ \MyRefChooseCommand{eq:1}

\end{document}