[Tex/LaTex] \subfloat \caption odd behavior

captionscross-referencingfloatssubfloats

I'm very new to Latex and trying my best to adopt it as my method of choice for paper and my Thesis. I have always managed to solve any problems encountered by browsing through this forum but can't find anything on this error.

I have a figure made up of 4 sub-figures in a 2×2 matrix. Somehow my template doesn't allow me to use the subcaption package so I am forced to use the subfig package which seems to work fine if not for the odd error I encountered.

My code is as follows:

\documentclass[11pt, english, doublespacing,]{MastersDoctoralThesis}
\usepackage{graphicx,amsmath,amssymb,rotating,float,cite,multirow,subfig,
array,bm,appendix,epsfig,epstopdf,pdfpages,array,caption}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{figure}[!htp]
    \centering
    \label{Saliency_Maps}
    \subfloat[]{\includegraphics[width=0.49\linewidth]{figures/colour_conp.png} \label{Sal:Colour}}
    \subfloat[]{\includegraphics[width=0.49\linewidth]{figures/intensity_conp.png} \label{Sal:Intensity}}
    \\
    \subfloat[] {\includegraphics[width=0.49\linewidth]{figures/orientation_conp.png} \label{Sal:Orit}}
    \subfloat[]{\includegraphics[width=0.49\linewidth]{figures/final_sal_map.png} \label{Sal:Final}}
    \caption{ Conspicuity maps for a sample image showing colour \subref{Sal:Colour}, intensity \subref{Sal:Intensity} and orientation \subref{Sal:Orit} maps which are then combined into the final saliency map \subref{Sal:Final} which will need to be thresholded.}
\end{figure}

\end{document} 

The caption is causing all sort of errors such as:

Argument of \caption@ydblarg has an extra }. ...inal} which will need to be thresholded.}

…and…

Paragraph ended before \caption@ydblarg was complete. ...inal} which will need to be thresholded.}

…among other under full and overfull \hbox errors, which make no sense.

Removing the \caption{...} doesn't show any errors, but obviously shows no cation. Also even though there are errors when the \caption{…} is inserted the figures and caption display correctly….. with the exception that the letters under the figures are uppercase while the letters substituted be \subref{...} are lower case. (See image)

enter image description here

The errors are only suppressed if I set \setcounter{tocdepth}{3}. Setting it to \setcounter{tocdepth}{2} triggers a whole lot of errors which similarly don't make any sense and there are some odd entries in the "List of Figures" page where the offending image entry should be.

Any ideas what could be happening and how I can solve this nagging issue. Alternatives are welcome.

Many thanks

Best Answer

The cause of your problem is use of \subref{...}. Their use in caption had to be protected: \protect\subref{ ...}. See and test code below:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}

\begin{document}
\begin{figure}[htp]
    \centering
\subfloat[\label{Sal:Colour}]{\includegraphics[width=0.49\linewidth]{example-image}}
\hfill
\subfloat[\label{Sal:Intensity}]{\includegraphics[width=0.49\linewidth]{example-image}}
    \\
\subfloat[\label{Sal:Orit}] {\includegraphics[width=0.49\linewidth]{example-image}}
\hfill
\subfloat[\label{Sal:Final}]{\includegraphics[width=0.49\linewidth]{example-image}}
\caption{ Conspicuity maps for a sample image showing colour \protect\subref{Sal:Colour}, intensity \protect\subref{Sal:Intensity} and orientation \protect\subref{Sal:Orit} maps which are then combined into the final saliency map \protect\subref{Sal:Final} which will need to be thresholded.}
\label{Saliency_Maps}
    \end{figure}
\end{document}

enter image description here

By the way, label in figure environment had to be always after caption, othervise the referencing will work wrong.