# Putting all cross references in brackets in LyX

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?

• Do you want cross-references to figures, tables, sections, footnotes, and what-have-you-more also to be encased in parentheses, or just cross-references to equations? – Mico Jul 20 '14 at 21:21
• @Mico Thank you for responding. All kinds of references have to be in brackets. – The Quantum Physicist Jul 20 '14 at 21:26

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}
$$\label{eq:pyth} a^2+b^2=c^2$$
\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}

• Thank you. But this does not work on my document. I added it to my preamble and it doesn't change anything. Any ideas? – The Quantum Physicist Jul 20 '14 at 22:09
• I fixed your solution. One has to use "\AtBeginDocument{\renewcommand{\ref}[1]{(\origref{#1})}}". Thanks for your help. – The Quantum Physicist Jul 20 '14 at 22:30
• One cannot add stuff to the preamble with ERTs, they will only be added within the document environment. Adding to the preamble is done in Document --> Settings --> LaTeX preamble. – Torbjørn T. Jul 21 '14 at 5:58
• @TorbjørnT. - Thanks for pointing this out. I've corrected the write-up. – Mico Jul 21 '14 at 7:41
• @TheQuantumPhysicist - Thanks for pointing out the need to place the instructions in an \AtBeginDocument{...} wrapper. – Mico Jul 21 '14 at 7:41