# Issue with subcaption and cross references in Latex

So I've been writing my dissertation in Latex and I've recently stumbled across an issue that I'm having trouble solving and I hope someone on here can help me.

I'm using subcaption to create multi image figures. I've successfully managed to cross reference one set of images but I'm getting issues with the second set. The code is the exact same thing except I'm getting an error on one of them. I've attached my code that shows how some of the references are being shown as '??'.

\documentclass{book}
\usepackage{typedref}
\usepackage[demo]{graphicx}
\usepackage{caption,subcaption}
\usepackage{listings}

\begin{document}

Test \figureref{protonTimeComparison}.

\begin{figure}
\begin{center}
\begin{subfigure}[b]{1\textwidth}
\graphicspath{{./Figures/}}
\includegraphics[width=1\linewidth]{cheers.png}
\subcaption{Test 1a}
\label{proton5min}
\end{subfigure}
\begin{subfigure}[b]{1\textwidth}
\graphicspath{{./Figures/}}
\includegraphics[width=1\linewidth]{Thirty_Minute_Average_Flux_Jan2000.png}
\subcaption{test 1b}
\label{protonthreemin}
\end{subfigure}
\caption{test 1}
\label{protonTimeComparison}
\end{center}
\end{figure}

Test sublabels \figureref{proton5min} and \figureref{protonthreemin}.

Test \figureref{protonP1Issue}

\begin{figure}
\begin{center}
\begin{subfigure}[b]{1\textwidth}
\graphicspath{{./Figures/}}
\includegraphics[width=1\linewidth]{April_2002_Channel_P1.png}
\subcaption{test 2a}
\label{P1Apr}
\end{subfigure}
\begin{subfigure}[b]{1\textwidth}
\graphicspath{{./Figures/}}
\includegraphics[width=1\linewidth]{April_2002_Channel_P2.png}
\subcaption{test 2b}
\label{PApr}
\end{subfigure}
\caption{test 2}
\label{protonP1Issue}
\end{center}
\end{figure}

Test sublabels \figureref{P1Apr} and \figureref{PApr}.

\end{document}


I'm thinking it may have something to do with the typedref package I'm using for the cross-referencing but I'm not certain. Thank you for the help.

I see little reason for using a package such as typedref -- which appears to be distributed with MikTeX but not with TeXlive -- if a much more powerful and versatile cross-referencing package such as cleveref could be used instead.

Oh, and do omit the [b] placement specifiers from the subfigure environments, as they do nothing at all (except provide code clutter).

\documentclass{book}
%%\usepackage{typedref} % not part of TeXLive
\usepackage[demo]{graphicx}
\usepackage{subcaption,listings}
\usepackage[noabbrev,capitalize]{cleveref}

\begin{document}
Test: \cref{protonTimeComparison}.

Test: \cref{proton5min} and \cref{protonthreemin}.
Or: \cref{proton5min,protonthreemin}.

\begin{figure}
\begin{subfigure}{1\textwidth}
\graphicspath{{./Figures/}}
\includegraphics[width=1\linewidth]{cheers.png}
\caption{Test 1a}
\label{proton5min}
\end{subfigure}

\medskip % insert some vertical whitespace to separate the subfigures
\begin{subfigure}{1\textwidth}
\graphicspath{{./Figures/}}
\includegraphics[width=1\linewidth]{Thirty_Minute_Average_Flux_Jan2000.png}
\caption{Test 1b}
\label{protonthreemin}
\end{subfigure}
\caption{Test 1}
\label{protonTimeComparison}
\end{figure}
\end{document}

• I've never heard of 'cleveref' before. I stumbled onto 'typedref' a few years ago to help with cross referencing within a document. Is there any benefit to using 'cleveref' over 'typedref' or cross referencing items with the 'fig:' labels? – zdr0001 Jan 11 '17 at 16:26
• @zdr0001 - \cref does not require you to write the labels in any particular way. Plus, there's only one macro name to remember; no need for \figereref, \tableref, \sectionref, etc – Mico Jan 11 '17 at 17:20

Try the following modification of your MWE:

\documentclass{book}
%\usepackage{typedref}  % source of the problem
\usepackage[demo]{graphicx}
\usepackage{caption,subcaption}
%\usepackage{listings}

\graphicspath{{./Figures/}}

\begin{document}

Test \ref{protonTimeComparison}.

\begin{figure}
\centering
\begin{subfigure}{\textwidth}
\includegraphics[width=\linewidth]{cheers}
\caption{Test 1a}
\label{proton5min}
\end{subfigure}
\begin{subfigure}{\textwidth}
\includegraphics[width=\linewidth]{Thirty_Minute_Average_Flux_Jan2000}
\caption{test 1b}
\label{protonthreemin}
\end{subfigure}
\caption{test 1}
\label{protonTimeComparison}
\end{figure}

Test sub-labels \ref{proton5min} and \ref{protonthreemin}.
Test \ref{protonP1Issue}

\begin{figure}
\centering
\begin{subfigure}{\textwidth}
\includegraphics[width=\linewidth]{April_2002_Channel_P1.png}
\caption{test 2a}
\label{P1Apr}
\end{subfigure}\hfil
\begin{subfigure}{\textwidth}
\includegraphics[width=\linewidth]{April_2002_Channel_P2.png}
\caption{test 2b}
\label{PApr}
\end{subfigure}
\caption{test 2}
\label{protonP1Issue}
\end{figure}
Test sub-labels \ref{P1Apr} and \ref{PApr}.
\end{document}


Main changes in comparison to your MWE:

• path to image is moved to preamble where is their place (if all of yours images are in the same folder)
• in subfigure use \caption instead of subcaption
• you have define \figureref by package typedref. As found latter it is not compatible with subcaption so it is better to define own figureref as \newcommand\figureref[1]{Fig. \ref{#1}} or use some other package for referencing.

Addendum: source of your problems is package typedref, which heavily redefine label definition. Without it above example works fine (as reported). For better solution see Mico answer, where he propose and show how to use cleaveref package.

• The macros \figureref, \tableref, \partref, \chapterref, \sectionref, \itemref, and a few others are defined by the typedref package. Speaking for myself, I find such a proliferation of cross-referencing instructions problematic... – Mico Jan 11 '17 at 1:06
• As @Mico stated, those commands are part of the 'typedref 'package. I started using it a few years ago when I wrote my master's thesis because I thought it was easier to use those commands rather than the 'fig:' labels. I was hoping to get a solution that didn't require me to write the word Figure/Table/Section in my dissertation. – zdr0001 Jan 11 '17 at 16:21
• I test this package and find out that it seems that it is not compatible with subcaption package. It cause error undefined control sequence for labels in subfigure. I will read documentation, maybe there is some information. Otherwise the solution is to make own definition for this referencing. – Zarko Jan 11 '17 at 16:39
• Ok. That is good to know. So it does seem like there are issues with the typedref package I was using for cross referencing items. – zdr0001 Jan 11 '17 at 17:16