[Tex/LaTex] change font size in caption for selected figures

captionsexercisesfontsize

I am trying to have some environments in my LaTeX document where the font in general is smaller than in the main text. This means that whenever I include a figure, the caption from the \begin{figure}... should have the same (smaller) font size. I found answers where the change is globally by providing the font size parameter to the caption package. Yet I was wondering if I could do this locally for some figures. Actually, I would like to know if this is possible in the definition of the exercise environment I have:

\usepackage[lastexercise]{exercise} % for exercise environment

% set font for exercise environment
\newcommand\ExerciseFont{\footnotesize}
\renewcommand\AtBeginAnswer{\ExerciseFont}
\renewcommand\AtBeginExercise{\ExerciseFont}

% change format of exercise environment
\renewcommand{\ExerciseHeader}{\ExerciseFont\textsc{
                \ExerciseName\ \ExerciseHeaderNB. \ExerciseHeaderTitle
                \ExerciseHeaderOrigin}}

% change font for answers header
\renewcommand{\AnswerHeader}{\ExerciseFont\textit{
                Solution:\ }}

Can I somehow change the font size for the figures in such environment?

Best Answer

You can define an \ExerciseCaption to use within the exercise. By using \captionsetup in caption.sty the caption can be changed locally.

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\newcommand\ExerciseCaption[1]{%
  \captionsetup{font=footnotesize}%
  \caption{#1}}
\begin{document}

\begin{figure}
  \centering
  \includegraphics[scale=0.3]{example-image-a}
  \caption{Test 1}
  \label{TA}
\end{figure}

\begin{figure}
  \centering
  \includegraphics[scale=0.3]{example-image-b}
  \ExerciseCaption{Test 2}
  \label{TB}
\end{figure}

Figure \ref{TA} and Figure \ref{TB}
\end{document}

enter image description here