0

I'm trying to find a way to reference the counter of the main figure, when a figure contains several subfigures. Using \ref I get e.g. '1a', using \subref I get only the 'a'. Is there a way to get only the main counter, i.e. '1'

Of course I could add another \label to the main caption, but I was wondering if there is another way without adding more and more labels to my document.

Thanks for any help!

MWE:

\documentclass{scrartcl}
\usepackage{graphicx, caption, subcaption}
\begin{document}
\begin{figure}
    \centering
    \begin{subfigure}{0.48\linewidth}
        \includegraphics[width=\linewidth]{example-image-a}
        \caption{}\label{fig:image_a}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.48\linewidth}
        \includegraphics[width=\linewidth]{example-image-b}
        \caption{}\label{fig:image_b}
    \end{subfigure}
    \caption{Figure \subref{fig:image_a}) and Figure \subref{fig:image_b}).}
\end{figure}
This is a reference to the subfigure: Figure \ref{fig:image_a}.

This is a reference to the letter of the subfigure: Figure \subref{fig:image_a}

This is a reference to the number of the figure only: ??? (wanted result: Figure 1)
\end{document}

enter image description here

1 Answers1

0

I eventually found a solution. You can modify the answers from that question: ref to subsection number only

You define a new referece command e.g. \mainref that only returns the figure counter. The actual syntax depends on if you use the hyperref package and if you have the subsection in the counter or not. You just have to look on the syntax of \newlabel in the aux file. To the MWE in the question above the solution looks like follows:

\makeatletter
\def\@firstoftwo@second#1#2{%
    \def\temp##1##2\@nil{##1}%
    \temp#1\@nil}
\newcommand\mainref[1]{%
    \expandafter\@setref\csname r@#1\endcsname\@firstoftwo@second{#1}%
}
\makeatother

If you use the hyperref package and include section numbering in the labels, the solution looks like this:

\makeatletter
\def\@firstoftwo@second#1#2#3#4#5{%
    \def\temp##1.##2##3\@nil{##1.##2}%
    \temp#1\@nil}
\newcommand\mainref[1]{%
    \expandafter\@setref\csname r@#1\endcsname\@firstoftwo@second{#1}%
}
\makeatother

Hope that helps someone.