# How can I get cleveref to surround references with parentheses automatically?

I use cleveref for in-document references, and find it to be clever. However, I typically want to refer to figures (for example) within parentheses. What I do now is this

\usepackage{cleveref}
...
X exhibits Y (\cref{fig:foo}).


but what I would like to do is something like

X exhibits Y \cref{fig:foo}. % Parentheses omitted in code but not in output


Ideally I would have some way to omit parentheses in output when needed.

I would not modify the workings of \cref and friends directly. Instead, I would create a new macro called \pcref -- short for "parenthetic \cref", I suppose -- as follows:

\newcommand\pcref[1]{(\cref{#1})}


As you can probably guess, \pcref places round parentheses around the output of \cref. No need for lots of separate \crefformat directives. This approach preserves access to the standard definition of \cref, should the need to do so arise.

For the following screenshot, I loaded the hyperref package and specified the cleveref option nameinlink, in order to make visible what is, and is not, produced by \cref.

\documentclass{article}
\newcommand\pcref[1]{(\cref{#1})}
\begin{document}
\begin{figure}[t!] \caption{foo}\label{fig:foo}\end{figure}
\begin{figure}[h!] \caption{bar}\label{fig:bar}\end{figure}
\begin{table}[h!] \caption{foo}\label{tab:foo}\end{table}

\dots\ \pcref{fig:foo}, \pcref{fig:foo,fig:bar}, \pcref{tab:foo,fig:bar}, \dots

\bigskip vs.

\bigskip
\dots\ \cref{fig:foo}, \cref{fig:foo,fig:bar}, \cref{tab:foo,fig:bar}, \dots

\end{document}


Referencing Section 8.2.1 of the cleveref documention,

Cross-reference formats for single cross-references are defined or redefined using the \crefformat and \Crefformat commands, which are used by the \cref and \Cref commands respectively. These take two arguments: the cross-reference type, and the formatting code: \crefformat{type}{format}

For your purposes, we can use, for example:

\crefformat{figure}{(Figure~#2#1#3)}
\Crefformat{figure}{Figure~#2#1#3}


Note that I've used crefformat for use with the \cref command (not beginning of sentence), and Crefformat for use with the Cref command (beginning of sentence).

This gives

A complete MWE follows

% arara: pdflatex
% arara: pdflatex
\documentclass{article}
\usepackage{cleveref}
\crefformat{figure}{(Figure~#2#1#3)}
\Crefformat{figure}{Figure~#2#1#3}
\begin{document}

\begin{figure}[!h]
\rule{3mm}{2mm}
\caption{August}
\label{fig:foo}
\end{figure}

X exhibits Y \cref{fig:foo}.

\Cref{fig:foo} is a cross reference at the beginning of a sentence
\end{document}

• Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to \crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for \crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref. Apr 10, 2019 at 12:03

As a complement to Mico's idea, the same can be done for \crefrange :

\newcommand\pcrefrange[2]{(\crefrange{#1}{#2})}

However, it is problematic if we want the (1)--(2) notation instead of (1--2), the former being much more widespread.

See those posts :

Obtain (1)‒(3) style when referring to ranges of examples, when default \crefrangelabelformat has no parentheses

Local cross-referencing with Linguex/Philex and Cleveref: Skipping 1st and 2nd level sublabels