2

I have plenty of equations in my Tex which were labeled with (\ref{eqn: bla}) which I now want to change to \eqref{eqn: bla}. Is there a fast way to do this rather than manually doing it?

  • 3
    Welcome to TeX.SX! Suggestion: Use your editor and search/replace: \ref{eqn: by \eqref{eqn:} if your equation labels follow this pattern. A dirty way (not recommended (really)): \let\ref\eqref (Don't do this!!!) – user31729 Sep 25 '15 at 17:59
  • The space in eqn: bla isn't recommended too ;-) – user31729 Sep 25 '15 at 18:01
  • @ChristianHupfer that may not work because of the parenthesis surrounding \ref{} – bissi Sep 25 '15 at 18:04
  • 1
    there still another way: search replace as @ChristianHupfer first comment (\ref{eq->\eqref{eq and than redefine \eqref to eat the 2nd parenthese) – touhami Sep 25 '15 at 18:44
  • 1
    @touhami: That's another way yes, but I still think that using regexp would be easier? With sed or perl... – user31729 Sep 25 '15 at 18:50
2

If there wouldn't be manual (...) parentheses, one could use a \ref wrapper command that checks whether the reference label name contains eqn: and use \eqref accordingly. But alas,... this is no solution for the (...) case.

Note I am not sure this will work in conjunction with cleveref or hyperref.

\documentclass{article}

\usepackage{xstring}
\usepackage{amsmath}


\let\origref\ref  % Prevent recursive definition

\makeatletter
\renewcommand{\eqref}[1]{%
  \textup{\tagform@{\origref{#1}}}%
}
\makeatother

\renewcommand{\ref}[1]{%
  \IfSubStr{#1}{eqn:}{%
    \eqref{#1}%
  }{%
    \origref{#1}%
  }%
}

\begin{document}

\section{First} \label{sec:first}



\begin{equation}
  E=mc^2 \label{eqn: bla}
\end{equation}

\section{Second} \label{sec:second}

In section \ref{sec:first} we have equation \ref{eqn: bla}
\end{document}

enter image description here

  • 2
    Thank you. I will just let it pass and "settle" for my fate with \ref. – bissi Sep 25 '15 at 18:51
  • @bissi -- i don't know whether this will cause you to rethink, but it's worth mentioning that \eqref enforces \upshape, including the parentheses, so that they will not be sloped in xrefs in theorems and other italic environments. how \ref is styled is dependent on the document class. and may or may not be compatible in appearance. – barbara beeton Sep 25 '15 at 19:40

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.