Customize the subcaption caption text

captionssubcaption

Can you assist me in customizing the subcaption text parameters? Parameters originally derived from a caption environment:

Here is the caption parameters:

%%% Caption Formatting
%
% This modifies the default LaTeX format for figure and table captions.
%   Single-line captions are set centered under the figure.
%   Multi-line captions are set centered in a paragraph of width \capwidth.
%
\long\def\@makecaption#1#2{%
    \vskip\abovecaptionskip
    \sbox\@tempboxa{\small{\bfseries #1\/}: #2}%
    \ifdim \wd\@tempboxa > \capwidth
        \@tempdima=\textwidth%
        \advance\@tempdima by -\capwidth%
        \advance\leftskip by 0.5\@tempdima%
        \advance\rightskip by 0.5\@tempdima%
        \singlespacing
        {\bfseries #1\/}: #2\par
    \else
        \hbox to\hsize{\hfil\box\@tempboxa\hfil}%
    \fi
    \vskip\belowcaptionskip}

Here is my full code:

\documentclass[12pt]{article}

%======================================================================================
%  ADDITIONS
%======================================================================================
% For Tables
\usepackage[table]{xcolor}
\usepackage{bigstrut}

\usepackage{graphicx}
\usepackage{eso-pic}
\usepackage{amsmath,amssymb,amsfonts,bm,mathtools}

%####################     FOR FIGURES        ################
\usepackage{caption}
\usepackage[skip=0.5ex]{subcaption}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\usepackage{booktabs}
\linespread{1.5}
%%% Caption Formatting
%
% This modifies the default LaTeX format for figure and table captions.
%   Single-line captions are set centered under the figure.
%   Multi-line captions are set centered in a paragraph of width \capwidth.
%
\long\def\@makecaption#1#2{%
    \vskip\abovecaptionskip
    \sbox\@tempboxa{\small{\bfseries #1\/}: #2}%
    \ifdim \wd\@tempboxa > \capwidth
        \@tempdima=\textwidth%
        \advance\@tempdima by -\capwidth%
        \advance\leftskip by 0.5\@tempdima%
        \advance\rightskip by 0.5\@tempdima%
        \singlespacing
        {\bfseries #1\/}: #2\par
    \else
        \hbox to\hsize{\hfil\box\@tempboxa\hfil}%
    \fi
    \vskip\belowcaptionskip}
%
% The default \capwidth is 80% of the \textwidth.
%
\newlength{\capwidth}
\setlength{\capwidth}{0.8\textwidth}

\begin{document}

\begin{figure}
    \centering
    \setkeys{Gin}{width=\linewidth}
\begin{subfigure}[b]{0.3\linewidth}
     \includegraphics{example-image-a}
     \caption{A \& B sites}
     \label{fig:y equals x}
\end{subfigure}
\hfil
\begin{subfigure}[b]{0.3\linewidth}
     \includegraphics{example-image-b}
     \caption{C \& D sites}
     \label{fig:three sin x}
\end{subfigure}
    \caption{Uranium Concentrations at soil locations}
    \label{fig:uranium}
    \end{figure}


    \lipsum[3]
\begin{figure}[!b]
    \centering
    \begin{subfigure}{0.49\linewidth}
        \includegraphics[scale=0.5]{example-image}
        \subcaption{$Q^{*}$ values for arm 1}
        \label{fig:arm1}
    \end{subfigure}
    \hfil
\begin{subfigure}[b]{0.49\linewidth}
     \includegraphics[scale=0.5]{example-image-b}
     \subcaption{C \& D sites}
     \label{fig:three sin x}
\end{subfigure}

\medskip
    \begin{subfigure}{0.35\textwidth}
        \includegraphics[width=\linewidth]{example-image}
        \subcaption{$Q^{*}$ values for arm 2}
        \label{fig:arm2}
    \end{subfigure}
    \caption{$Q^{*}$ values for different arms which will extend to a very long caption title if I continue to type like this.}
\end{figure}%
\begin{figure}[ht]\ContinuedFloat
    \centering
    \begin{subfigure}{0.6\textwidth}
        \includegraphics[width=\linewidth]{example-image}
        \subcaption{$Q^{*}$ values for arm 3}
        \label{fig:arm3}
    \end{subfigure}

\medskip
    \begin{subfigure}{0.6\textwidth}
        \includegraphics[width=\linewidth]{example-image}
        \subcaption{$Q^{*}$ values for arm 4}
        \label{fig:arm4}
    \end{subfigure}
    \caption[]{$Q^{*}$ values for different arms (cont.)}
    \label{fig:arms}
\end{figure}
    \lipsum[2-3]
    
\end{document}  

Best Answer

You can set the margin and the justification using of the captions using \captionsetup

\captionsetup{ margin = \capmargin, justification=centering} where \capmargin value is the desired margins.

a

\documentclass[12pt]{article}

%======================================================================================
%  ADDITIONS
%======================================================================================
% For Tables
\usepackage[table]{xcolor}
\usepackage{bigstrut}

\usepackage{graphicx}
\usepackage{eso-pic}
\usepackage{amsmath,amssymb,amsfonts,bm,mathtools}

%####################     FOR FIGURES        ################
%\usepackage{caption} % not needed <<<<<<<<<<<<<<<
\usepackage[skip=0.5ex]{subcaption}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\usepackage{booktabs}
\linespread{1.5}
%%% Caption Formatting
%
% This modifies the default LaTeX format for figure and table captions.
%   Single-line captions are set centered under the figure.
%   Multi-line captions are set centered in a paragraph of width \textwidth - 2\capmargin

\newlength{\capmargin} % added <<<<<<<<<<<<<
\setlength{\capmargin}{0.1\textwidth} % added <<<<<<<<<<<<<
\captionsetup{ margin = \capmargin, justification=centering} % added <<<<<<<<<<<<<

\begin{document}
    
    \begin{figure}[htp!]
        \centering
        \setkeys{Gin}{width=\linewidth}
        \begin{subfigure}[b]{0.3\linewidth}
            \includegraphics{example-image-a}
            \caption{A \& B sites}
            \label{fig:y equals x}
        \end{subfigure}
        \hfil
        \begin{subfigure}[b]{0.3\linewidth}
            \includegraphics{example-image-a}
            \caption{C \& D sites}
            \label{fig:three sin x}
        \end{subfigure}
        \caption{Uranium Concentrations at soil locations}
        \label{fig:uranium}
    \end{figure}
        
    \lipsum[3]
    \begin{figure}[!b]
        \centering
        \begin{subfigure}{0.49\linewidth}
            \includegraphics[scale=0.5]{example-image-b}
            \subcaption{$Q^{*}$ values for arm 1}
            \label{fig:arm1}
        \end{subfigure}
        \hfil
        \begin{subfigure}[b]{0.49\linewidth}
            \includegraphics[scale=0.5]{example-image-b}
            \subcaption{C \& D sites}
            \label{fig:three sin x}
        \end{subfigure}
        
        \medskip
        \begin{subfigure}{0.35\textwidth}
            \includegraphics[width=\linewidth]{example-image-b}
            \subcaption{$Q^{*}$ values for arm 2}
            \label{fig:arm2}
        \end{subfigure}
        \caption{$Q^{*}$ values for different arms which will extend to a very long caption title if I continue to type like this.}
    \end{figure}%
    \begin{figure}[ht]\ContinuedFloat
        \centering
        \begin{subfigure}{0.6\textwidth}
            \includegraphics[width=\linewidth]{example-image-c}
            \subcaption{$Q^{*}$ values for arm 3}
            \label{fig:arm3}
        \end{subfigure}
        
        \medskip
        \begin{subfigure}{0.6\textwidth}
            \includegraphics[width=\linewidth]{example-image-c}
            \subcaption{$Q^{*}$ values for arm 4}
            \label{fig:arm4}
        \end{subfigure}
        \caption[]{$Q^{*}$ values for different arms (cont.)}
        \label{fig:arms}
    \end{figure}
    \lipsum[2-3]
    
\end{document} 

You might want to customize the caption figures and the caption subfigures separately. For example

\captionsetup[figure]{margin = \capmargin, justification=centering, labelfont=bf} % added <<<<<<<<<<<<<
\captionsetup[sub]{ justification=centering, labelfont=rm}

will use the bold font for the label of the figure.

b

UPDATE After follow-up question: changing the line spacing in the caption.

(1) Load the package setspace

(2) Use font={stretch=1} in the captionsetup for figures.

\usepackage{setspace} % added <<<<<<<<<<<<<
\newlength{\capmargin} % added <<<<<<<<<<<<<
\setlength{\capmargin}{0.1\textwidth} % added <<<<<<<<<<<<<
\captionsetup[figure]{margin = \capmargin, justification=centering, labelfont=bf,font={stretch=1.2}} % added <<<<<<<<<<<<<
\captionsetup[sub]{ justification=centering, labelfont=rm}% added

c

Related Question