# new command for including a custom string into \cref

In this thread here: Subfigure labelling for a single figure I suggested a solution how to include a custom string into the hyperlink created by the \cref command.

I'd like to create a compact new command which would take the label and the custom string. Sth. like: \crefSugFigRef{\fig:label}{customstring}

However,

\newcommand{\crefSugFigRef}[2]{\crefformat{figure}{fig.~#2#1{(#2)}#3}\cref{#1}\crefformat{figure}{fig.~#2#1#3}}

cannot work because both, \newcommand and \crefformat use the string #1 and #2.

Has anybody an idea how to make this work?

Using the concepts you pointed to in your question at Subfigure labelling for a single figure, I think this is what you intended for automating the \cref change.

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{mwe}
\usepackage{graphicx}
\newcommand{\crefSubFigRef}[2]{\crefformat{figure}{fig.~##2##1{(#2)}##3}%
\cref{#1}\crefformat{figure}{fig.~##2##1##3}}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{Main caption here}
\label{fig:main}
\end{figure}

CREF CHANGED BY HAND:

The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be%
\crefformat{figure}{fig.~#2#1{(a)}#3} % change the format to include (a)
\cref{fig:main} or%
\crefformat{figure}{fig.~#2#1{(b)}#3} % change the format to include (b)
\cref{fig:main}
\crefformat{figure}{fig.~#2#1#3}% restore the default format

AUTO-CHANGED:

The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be
\crefSubFigRef{fig:main}{a}
or
\crefSubFigRef{fig:main}{b}

\end{document}


And I see that one nice thing about this approach is that one can build their subfigures using any hyperref NON-compliant method, and still hyperlink to the figure this way, as shown below:

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{mwe}
\usepackage{graphicx}
\usepackage{stackengine}
\newcommand{\crefSubFigRef}[2]{\crefformat{figure}{fig.~##2##1{(#2)}##3}%
\cref{#1}\crefformat{figure}{fig.~##2##1##3}}
\begin{document}
\begin{figure}[ht]
\centering
\stackunder{\includegraphics[width=2in]{example-image-a}}{(a)}~~
\stackunder{\includegraphics[width=2in]{example-image-a}}{(b)}
\caption{Main caption here for (a) first subfigure and (b) second
subfigure\label{fig:main}}
\end{figure}
\clearpage
The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be
\crefSubFigRef{fig:main}{a}
or
\crefSubFigRef{fig:main}{b}

• @AVJ No problem. I'll do anything to plug my stackengine code ;^) – Steven B. Segletes Jul 23 '13 at 13:24