[Tex/LaTex] subcaption color with the subfigure package

colorfloatssubcaptionsubfloats

To define the caption color in my document, I am using

\usepackage{caption}
\usepackage[font={small,color=ocre}]{caption}

Which works perfectly, but currently facing the following two problems:

  1. Subcaptions are not colored (I am using the subfigure package, so I can't use the same code as above)

enter image description here

  1. When a citation is included in the figure caption , it is not colored.
    enter image description here

Does any one have some solutions or hints?

After the request, here is a minimum working example:

\documentclass[a4paper]{article} 
\usepackage{amsmath} 
\usepackage[numbers,square]{natbib}  
%The following is uesd to make the caption colored
\usepackage{xcolor}
\definecolor{ocre}{RGB}{1,160,233}
\usepackage{caption}
\usepackage[font={small,color=ocre}]{caption}
\usepackage{graphicx,subfigure}
\usepackage[hidelinks]{hyperref}  % hyperlinks
      \usepackage[hidelinks]{hyperref}  % hyperlinks
      \hypersetup{
           colorlinks=true, 
          allcolors=black,          
          citecolor=black,
          linkcolor=black,
      }

\usepackage{filecontents}

\begin{filecontents*}{\jobname.bib}
  @book{Knu86,
    author = {Knuth, Donald E.},
    year = {1986},
    title = {The \TeX book},
  }
\end{filecontents*}

\bibliography{\jobname} % if you’re using BibTeX


\begin{document} 

\begin{figure}[hb]
\centering     %%% not \center
\boxed{\subfigure[Subfigure One]{\label{fig:SubfigureOne}\includegraphics[width=0.4\textwidth]{../../Desktop/StackExchange/Test.pdf}}}

\boxed{\subfigure[Subfigure Two]{\label{fig:SubfigureTwo}\includegraphics[width=0.4\textwidth]{../../Desktop/StackExchange/Test.pdf}}}

\caption{Comparison between Subfigure One and Subfigure Two. \autoref{fig:SubfigureOne} shows the difference for the complete data range, and \autoref{fig:SubfigureTwo} as displayed in \cite{Knu86}}
\label{fig:OverAllFigure}
\end{figure}

\end{document}

Best Answer

Code

\documentclass[a4paper]{article} 
\usepackage{amsmath} 
\usepackage[numbers,square]{natbib}  
%The following is uesd to make the caption colored
\usepackage{xcolor}
\definecolor{ocre}{RGB}{1,160,233}
\usepackage{caption}
\usepackage[font={small,color=ocre}]{caption}
\usepackage{graphicx,subfigure}
\usepackage[hidelinks]{hyperref}  % hyperlinks
      \usepackage[hidelinks]{hyperref}  % hyperlinks
      \hypersetup{
           colorlinks=true, 
          allcolors=black,          
          citecolor=black,
          linkcolor=black,
      }

\usepackage{etoolbox}

\AtBeginEnvironment{figure} {
  \hypersetup{
           colorlinks=true, 
          allcolors=ocre,          
          citecolor=ocre,
          linkcolor=ocre,
      }
}

\newcommand{\boxedcolor}[2]{%
  \boxed{\color{#1}#2}%
}

\usepackage{filecontents}

\begin{filecontents*}{\jobname.bib}
  @book{Knu86,
    author = {Knuth, Donald E.},
    year = {1986},
    title = {The \TeX book},
  }
\end{filecontents*}

\bibliography{\jobname} % if you’re using BibTeX


\begin{document} 

\begin{figure}[hb] 

\centering     %%% not \center
\boxedcolor{ocre}{\subfigure[Subfigure One]{\label{fig:SubfigureOne}\includegraphics[width=0.4\textwidth]{exercises.png}}}

\boxedcolor{ocre}{\subfigure[Subfigure Two]{\label{fig:SubfigureTwo}\includegraphics[width=0.4\textwidth]{exercises.png}}}

\caption{Comparison between Subfigure One and Subfigure Two. \autoref{fig:SubfigureOne} shows the difference for the complete data range, and \autoref{fig:SubfigureTwo} as displayed in \cite{Knu86}}
\label{fig:OverAllFigure}
\end{figure}

Another reference to Figure~\ref{fig:OverAllFigure} with a black hyperling.

\end{document}

Document

Colored captions

Explanation

  1. The problem is, that you defined a black color for hyperlinks using \hypersetup. You can change the link colors locally using \hypersetup again at the begin of every figure environment. I think the most comfortable way of doing this is using \AtBeginEnvironment of the package etoolbox.

  2. To change the color of the subfigure captions, I defined a new command \boxedcolor. It allows you to change the color inside the box and also affects subfigure captions. Depending on whether you always want to use ocre as you color, you could also define \boxedcolor it with one argument

    \newcommand{\boxedcolor}[1]{%
      \boxed{\color{ocre}#1}%
    }
    

    which leads to shorter usage: \boxedcolor{...}.

    Alternatively, you could append \color{ocre} to figure using \AtBeginDocument like I did it with hypersetup before. This would be the most comfortable solution, but it also leads to a colored box (which is not desired, I expect).