1
\documentclass{article}
\usepackage[colorlinks, colorlinks, linktocpage=false]{hyperref}
\usepackage[nameinlink]{cleveref}
\hypersetup{%
    colorlinks,
    citecolor=blue,
    linkcolor=blue}
\newcommand\pcref[1]{(\cref{#1})}
\newcommand\qcref[1]{\cref({#1})}
\crefformat{figure}{(figure~#2#1#3)}
\Crefformat{figure}{figure~#2#1#3}
\crefformat{table}{(table~#2#1#3)}
\Crefformat{table}{table~#2#1#3}
\begin{document}
\begin{figure}[h!] \caption{bar}\label{fig:bar}\end{figure}
\Cref{fig:bar}, 
\Cref{fig:bar},
\cref{fig:bar},
\cref{fig:bar}
\end{document}

1 Answer 1

2

If you use the low-level commands \crefformat and \Crefformat, the markers #2 and #3 delimit the extent of the material that is turned into a hypertarget. In order to achieve your formatting goal, you'll need to change \crefformat{figure}{(figure~#2#1#3)} to \crefformat{figure}{#2(figure~#1)#3} to make the hypertarget include the parentheses (and the string "figure", of course). If you do not wish to make the parentheses form part of the hypertarget, just use \crefformat{figure}{(#2figure~#1#3)} instead.

Observe that because \crefformat and \Crefformat give you total control over the formatting, the package option nameinlink doesn't have an effect. Of course, the option will have an effect for cross-references to other objects, such as equation environments.

enter image description here

\documentclass{article}
\usepackage[colorlinks, allcolors=blue]{hyperref}
\usepackage[nameinlink]{cleveref}
\crefformat{figure}{#2(figure~#1)#3}
\Crefformat{figure}{#2Figure~#1#3}
\crefformat{table}{#2(table~#1)#3}
\Crefformat{table}{#2Table~#1#3}

\begin{document}
\begin{figure}[h!] \caption{bar}\label{fig:bar}\end{figure}
\begin{table}[h!]  \caption{foo}\label{tab:foo}\end{table}

\Cref{fig:bar}, \Cref{tab:foo}, \cref{fig:bar}, \cref{tab:foo}.
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .