1

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.

  • 1
    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
  • 2
    Why not \cref for everything and configure the equation type? – gusbrs May 29 '18 at 13:48
  • 1
    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
  • 1
    Redefining \cref to act like \eqref (like Mico said) would work as well. – soer9606 May 29 '18 at 13:54
  • 2
    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
6

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.

enter image description here

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

\begin{document}
\section{First}\label{sec:first}
\begin{equation}\label{eq:pyth}a^2+b^2=c^2\end{equation}
\begin{equation}\label{eq:triv}1+1=2\end{equation}
\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} 
1

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:
  %%......................................................................
  %% \UD@CheckWhetherLeadingTokens{<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>
  %%                             as leading tokens>}%
  %%                           {<tokens to be delivered in case <argument
  %%                             which is to be checked> does not have
  %%                             <token sequence> as leading tokens>}%
  \newcommand\UD@CheckWhetherLeadingTokens[4]{%
    \romannumeral0\UD@CheckWhetherNull{#4}%
    {\expandafter\expandafter\@firstoftwo{ }{}\@secondoftwo}%
    {\expandafter\@secondoftwo\string{#3#2#4#1}{}}%
  }%
  \newcommand\UD@@CheckWhetherLeadingTokens[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
  %% \UD@@CheckWhetherLeadingTokens.
  %%......................................................................
  \newcommand\UD@internaltokencheckdefiner[2]{%
    \newcommand#1{}\long\def#1##1#2{\UD@@CheckWhetherLeadingTokens{##1}}%
  }%
  \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]{%
    \UD@CheckWhetherLeadingTokens{eq:}{.}{\@CheckEq}{#1}{%
      \eqref %<- name of label starts with 'eq:'
    }{%
      \UD@CheckWhetherLeadingTokens{sec:}{.}{\@CheckSec}{#1}{%
        \cref %<- name of label starts with 'sec:'
      }{%
        \UD@CheckWhetherLeadingTokens{fig:}{.}{\@CheckFig}{#1}{%
          \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}

\begin{equation}
a^2+b^2=c^2\label{eq:1}
\end{equation}


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}

jpg-image of pdf-output when compiling the above example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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