3

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.

  • 1
    A quick (not good) solution: Redefining \cref on itself. – user156344 Apr 10 at 10:23
  • Welcome to the site! :) – cmhughes Apr 10 at 11:19
  • @JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original \cref for parenthesis-less refs. Is there any good reason against? – August Janse Apr 10 at 12:06
  • 1
    @AugustJanse If you want, this is the solution: \makeatletter \let\my@command\cref \renewcommand\cref[1]{[\my@command{#1}]} \makeatother – user156344 Apr 10 at 12:33
3

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.

enter image description here

\documentclass{article}
\usepackage[colorlinks,allcolors=blue]{hyperref} % optional
\usepackage[noabbrev,nameinlink]{cleveref}
\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} 
5

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

enter image description here

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. – August Janse Apr 10 at 12:03

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.