47

Is possible to surround a cross-reference with brackets or parentheses? For example when I write a command like some text \ref{some label} then when compiled the output was some text (12) without manually placing parentheses in latex command, like some text (\ref{some label}).

Mico
  • 506,678
  • 17
    In case of equations, there is \eqref of package amsmath that adds parentheses around the equation number. – Heiko Oberdiek Apr 22 '14 at 01:35
  • If I need to apply that for any referencing, not just the equation?? – Mohammad Fajar Apr 22 '14 at 01:41
  • 5
    Have a good look at the cleveref package. It is (a) fully compatible with the hyperref package (and must be loaded after hyperref) and (b) allows full customization of the appearance of cross-references. – Mico Apr 22 '14 at 01:46
  • I've taken the liberty of replacing hyperlink/hyperref with "cross-reference" since that's what your posting is mainly about. (A hyperlink is a special type of a cross-reference.) – Mico Apr 22 '14 at 01:54

4 Answers4

51

Try using \eqref{fig:label} from the amsmath package.

(Taken from here: https://groups.google.com/forum/#!topic/latexusersgroup/e1CHoBfj8pQ)

guest2015
  • 511
  • 1
  • 4
  • 2
  • 1
    Welcome to TeX.SE. The OP has stated in a comment that he (?) needs to parentheses to be placed around all cross-referencing call-outs, not just around call-outs to equations. Thus, your answer doesn't address the query fully. – Mico Jun 16 '15 at 00:15
  • you are right, and I just read some comments about the issue on the OP. I'll remove my answer soon. Sorry and thank you for your comment. – guest2015 Jun 18 '15 at 22:02
  • 1
    Upvoted for the equation-only option. – grfrazee Jul 23 '15 at 01:47
  • 1
    @Mico Haven't tried other stuff yet, but \eqref works well w/ enumeration items (besides equations) – jaam Mar 10 '19 at 22:20
  • 2
    Glad you didn't remove it: despite what the OP requested, it's a very helpful answer – Chris Swan Aug 04 '22 at 06:53
21

You could renew the reference command, perhaps something like:

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

Here's a complete MWE to play with:

% arara: pdflatex
% arara: pdflatex
\documentclass{article}

\let\oldref\ref
\renewcommand{\ref}[1]{(\oldref{#1})}
\begin{document}

\section{Section heading}\label{sec:testlabel}

Reference: \ref{sec:testlabel}
\end{document}

Note: as mentioned by @Mico, this solution isn't compatible with hyperref package.

JKHA
  • 473
cmhughes
  • 100,947
  • 18
    You should probably provide a note that this solution isn't compatible with hyperref: regardless of whether that package is loaded before or after your code, the result is that no parentheses will be placed around the section number. – Mico Apr 22 '14 at 11:09
14

I prefer the autoref function provided in the hyperref package.

Taking examples for tables and figures:

The Table reference is \autoref{tab:VHTRC}.
The equation reference is \autoref{eq:3}.

Next, new reference styles should be renewed at the preamble. Usually I define them just after including the hyperref package.

\usepackage{hyperref}
\def\equationautorefname~#1\null{Equation (#1)\null}

The \def define the auto-reference variable #1 in the style Equation (#1), which is enclosed in (). Then you will get the result like this:

enter image description here

If you want to use curly braces {}, they should be escaped with the \.

\usepackage{hyperref}
\def\equationautorefname~#1\null{Equation \{#1\}\null}
Tawei
  • 1,254
  • 12
  • 15
  • 1
    This is dark magic for me. I usually do things like \newcommand{\lemmaautorefname}{Lemma} to change what comes before the reference number. But I have no idea why ~#1\null works at all. – Apiwat Chantawibul Aug 18 '17 at 16:13
  • 1
    There is already question about \null: macro. It only reserves no space but shows TeX that there is a box which is taken into account for typesetting. – Tawei Aug 24 '17 at 02:13
0

I tried cmhughes' answer in my code, it didn't work (no parentheses -- which is strange, because the minimal example worked). guest2015's worked, but required an extra package. I ended up using \newcommand\pef[1]{(\ref{#1})}

jaam
  • 299