如何自定义子标题文字

如何自定义子标题文字

你能帮助我定制subcaption文本参数吗?最初源自caption环境的参数:

这里是caption参数:

%%% 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}

这是我的完整代码:

\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}  

答案1

您可以使用以下方式设置标题的边距和对齐方式\captionsetup

\captionsetup{ margin = \capmargin, justification=centering}其中\capmargin值是所需的边距。

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} 

您可能希望分别自定义标题图和标题子图。例如

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

将使用粗体字体作为图形的标签。

b

更新后续问题:更改标题中的行距。

(1)加载包setspace

(2) 用于font={stretch=1}数字captionsetup

\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

相关内容