Is it possible to format the \ref
and \subref
of a subcaption completely independently?
The manual suggests changing thesubfigure
to change the the label of a subfigure and hence the output of \ref
, but then this affect \subref
as well.
For example, the following MWE
\documentclass{article}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage{tikz}
% and example figure
\def\exfigure#1#2{
\begin{subfigure}[b]{0.3\linewidth}
\centering
\begin{tikzpicture}\path[draw=#1,fill=#1!20] (0,0) rectangle
(4,4);\end{tikzpicture}
\renewcommand{\thesubfigure}{#1}%
\phantomsubcaption%
\label{#2}%
\end{subfigure}\hfill}
\DeclareCaptionLabelFormat{subreffig}{(\emph{#2})}
\captionsetup{subrefformat=subreffig}
\begin{document}
\begin{figure}
\exfigure{red}{subfig1}
\exfigure{green}{subfig2}
\exfigure{blue}{subfig3}
\caption{Three squares. Left: \subref{subfig1}. Center: \subref{subfig2}.
Right: \subref{subfig3}.}
\label{fig:all}
\end{figure}
In \cref{fig:all}, we have three subfigures \cref{subfig1}
\cref{subfig2} and \cref{subfig3}.
\end{document}
The figure has the following caption
Figure 1: Three squares. Left: (red). Center: (green). Right: (blue).
This is the output I am after. However, the text afterwards has
In fig. 1, we have three subfigures fig. 1red fig. 1green and fig. 1blue.
Is it possible to change it to
In fig. 1, we have three subfigures fig. 1-[red] fig. 1-[green] and fig. 1-[blue].
Changing the definition of \thesubfigure
to
\renewcommand{\thesubfigure}{-[#1]}%
gives the correct output in text, but the wrong output in the caption.
\renewcommand{\thesubfigure}{-[#1]}
,\cref
seems to work as expected (so that the reference has the correct format).