[Tex/LaTex] Putting all cross references in brackets in LyX

cross-referencinglyx

I have many equation occurrences in a long document, where LyX automatically cross-referenced its equations with \ref{} rather than \eqref{}. I want all references to be in parenthesis.

A few solutions occur to me, and I need help with a smart solution.

  1. The tedious solution is to go to every equation and click on it, and choose the Format to be ("<"reference">").

  2. A smarter solution would be to open the lyx source and replace all \ref with \eqref. I'm avoiding this for now because it's easy but stupid.

  3. The smartest solution I'm looking for is a command like \renewcommand{\ref}[1]{(\eqref{#1})} to put in the preamble. But this doesn't work.

Is there something to put in the preamble to make all \ref{} be put in parentheses?

Best Answer

If the numbers of all cross-refernces, and not just those to equations, should be encased in round parentheses, you may achieve by inserting the following instructions in your document's preamble.

\AtBeginDocument{\let\origref\ref
   \renewcommand{\ref}[1]{(\origref{#1})}}}

Here's a full MWE (but without screenshot);

\documentclass{article}
\AtBeginDocument{\let\origref\ref
   \renewcommand{\ref}[1]{(\origref{#1})}}
\begin{document}
\begin{equation}\label{eq:pyth} a^2+b^2=c^2 \end{equation}
\begin{figure}\caption{hello}\label{fig:hello}\end{figure}

Here's a cross-reference to equation \ref{eq:pyth}, and here's a 
cross-reference to figure \ref{fig:hello}.
\end{document}