[Tex/LaTex] Labeling subcaption

labelssubcaption

The reference to my sub caption doesn't work

\documentclass{article}

\usepackage{mwe}
\usepackage{subcaption}
\usepackage{cleveref}

\begin{document}

\section{Section}
\begin{center}
\captionsetup{type=figure}
\begin{subfigure}{0.4\textwidth}
\includegraphics[width=\textwidth]{example-image-a.pdf}
\caption*{Text 0}
\end{subfigure}
\begin{subfigure}{0.4\textwidth}
\includegraphics[width=\textwidth]{example-image-b.pdf}
\caption*{Text 1\label{here}}
\end{subfigure}
\captionof{figure}{Description}
\end{center}

Look at \Cref{here}.
\end{document}

Output:
enter image description here

Maybe also see here


UPDATE:

It should produce Look at Text 1

Actually it's always the same text, so I would really like a solution where my counter is 0,1, ... at the and of the caption.
Or (maybe to use with \caption{\empty} where my counter is Text 0, Text 1, ...

Best Answer

Since counters are reset to zero, you can't use \refstepcounter to set up the \label. OTOH, you need \refstepcounter for cleverref and hyperref. So I generate the label before \refstepcounter then reset \@currentlabel after.

I see that \subcaption defines skip=6pt, but I can't figure out where skip is used. Also, \caption somehow knows whether it is at the top or bottom. I had to set up \mycaption for bottom only.

\documentclass{article}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{hyperref}

\newcounter{subfig}[figure]

\makeatletter
\newcommand{\mycaption}{\edef\@templabel{Text \thesubfig}%
  \refstepcounter{subfig}%
  \let\@currentlabel=\@templabel% \global?
  \vspace{6pt}%
  \makebox[\textwidth]{\@currentlabel}}
\makeatother

\newcommand{\topanchor}[1]% #1 = counter
  {\refstepcounter{#1}\addtocounter{#1}{-1}\par}

\begin{document}

\section{Section}
\begin{center}
\topanchor{figure}%
\begin{minipage}{0.4\textwidth}
\topanchor{subfig}%
\includegraphics[width=\textwidth]{example-image-a.pdf}
\mycaption
\end{minipage}
\begin{minipage}{0.4\textwidth}
\topanchor{subfig}%
\includegraphics[width=\textwidth]{example-image-b.pdf}
\mycaption\label{here}
\end{minipage}
\captionof{figure}{Description}\label{there}
\end{center}

Look at \ref{here}.

Hyperlink test \ref{there}.
\end{document}

demo