将零件编号添加到图号中

将零件编号添加到图号中

我正在尝试修改页码的处理方式,以便将部分编号作为图号的一部分。例如,根据 MWE,生成的图应称为图 B.1.1,而不是 1.1。这可以通过以下属性控制:“\counterwithin{figure}{section}”。我尝试了“\counterwithin{figure}{part}{section}”,但没有成功。“\counterwithin{figure}{part}”命令有效,但随后会丢失“部分编号”(即输出 B.1)。

总结一下,我只要输出B.1.1就可以了。

在 XeLaTeX 下编译 MWE。

MWE 的输出,图标签 1.1

\documentclass[11pt]{extarticle}

% For forcing images to be placed where declared
\usepackage{float}
% To use custom font
\usepackage{fontspec}
% To modify sections/part/chapter etc. properties
\usepackage{titlesec}
% To modify caption properties
\usepackage[font=footnotesize]{caption}
% To manipulate figure numbers
\usepackage{chngcntr}
% To include images
\usepackage{graphicx}
% To reset section numbers when a new part declared
\counterwithin*{section}{part}
% Remove hyphens from captions
\usepackage[labelsep=endash]{caption}
% Do not hyphenate text
\usepackage[none]{hyphenat}
% Use colour names and be able to declare colours
\usepackage[table,dvipsnames]{xcolor}
% Change font to Arial
\setmainfont{Arial}
% Set my own colours
\definecolor{myBlue}{HTML}{005B82}
\definecolor{myLightBlue}{HTML}{4F81BD}
\definecolor{myCaptionBlue}{HTML}{1F497D}
% Change the way the Part headings look like
\titleformat{\part}[hang]{\color{myBlue}\normalfont\LARGE\bfseries}{\thepart}{1em}{}
% Part headings to use Alphabets rather than Roman Numerals
\renewcommand{\thepart}{\Alph{part}}
% Change the way the section heading looks like
\titleformat*{\section}{\color{myBlue}\bfseries\fontsize{14}{0}\selectfont}
% Change the way the subsection heading looks like
\titleformat*{\subsection}{\color{myBlue}\bfseries\fontsize{13}{0}\selectfont}
% Change the way the subsubsection heading looks like
\titleformat*{\subsubsection}{\color{myLightBlue}\bfseries\fontsize{11}{0}\selectfont}
% Add a custom subsubsubsection, and change the way the heading looks like for it
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}
    {-3.25ex \@plus-1ex \@minus-.2ex}
    {0.5ex \@plus .1ex}
    {\color{myLightBlue}\itshape\bfseries\fontsize{11}{0}\selectfont}
    }
\makeatother
\let\subsubsubsection\paragraph
% Change the way the caption looks like
\captionsetup{labelfont={color=myCaptionBlue,it},textfont={color=myCaptionBlue,it}}
% Ensure figure numbers include section numbers into account
\counterwithin{figure}{section}


\begin{document}

\part{First Part}

\section{Introduction}

Dummy Text

\part{Second Part}

\section{Image}

\begin{figure}[H]
    \centering
    \includegraphics{example-image-a}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document}

答案1

章节编号不包含部件编号的情况很不寻常。那么你如何知道某个章节(例如编号为 3 的章节)属于 A 部分还是 B 部分?

如果您愿意更改章节编号,请在之后\titleformat*{\section}{\color{myBlue}\bfseries\fontsize{14}{0}\selectfont}添加\counterwithin{section}{part}

在此处输入图片描述

如果您坚持使用章节编号系统,那么\counterwithin{figure}{section}添加后就足够了\renewcommand{\thefigure}{\thepart.\thesection.\arabic{figure}}

编辑::此案例的完整 MWE 为:

\documentclass[11pt]{extarticle}

% For forcing images to be placed where declared
\usepackage{float}
% To use custom font
\usepackage{fontspec}
% To modify sections/part/chapter etc. properties
\usepackage{titlesec}
% To modify caption properties
\usepackage[font=footnotesize]{caption}
% To manipulate figure numbers
\usepackage{chngcntr}
% To include images
\usepackage{graphicx}
% To reset section numbers when a new part declared
% Remove hyphens from captions
\usepackage[labelsep=endash]{caption}
% Do not hyphenate text
\usepackage[none]{hyphenat}
% Use colour names and be able to declare colours
\usepackage[table,dvipsnames]{xcolor}
% Change font to Arial
\setmainfont{Arial}
% Set my own colours
\definecolor{myBlue}{HTML}{005B82}
\definecolor{myLightBlue}{HTML}{4F81BD}
\definecolor{myCaptionBlue}{HTML}{1F497D}
% Change the way the Part headings look like
\titleformat{\part}[hang]{\color{myBlue}\normalfont\LARGE\bfseries}{\thepart}{1em}{}
% Part headings to use Alphabets rather than Roman Numerals
\renewcommand{\thepart}{\Alph{part}}
% Change the way the section heading looks like
\titleformat*{\section}{\color{myBlue}\bfseries\fontsize{14}{0}\selectfont}
% Change the way the subsection heading looks like
\counterwithin{section}{part} % <-----
\titleformat*{\subsection}{\color{myBlue}\bfseries\fontsize{13}{0}\selectfont}
% Change the way the subsubsection heading looks like
\titleformat*{\subsubsection}{\color{myLightBlue}\bfseries\fontsize{11}{0}\selectfont}
% Add a custom subsubsubsection, and change the way the heading looks like for it
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}
    {-3.25ex \@plus-1ex \@minus-.2ex}
    {0.5ex \@plus .1ex}
    {\color{myLightBlue}\itshape\bfseries\fontsize{11}{0}\selectfont}
    }
\makeatother
\let\subsubsubsection\paragraph
% Change the way the caption looks like
\captionsetup{labelfont={color=myCaptionBlue,it},textfont={color=myCaptionBlue,it}}
% Ensure figure numbers include section numbers into account
\counterwithin{figure}{section}


\begin{document}
\part{First Part}

\section{Introduction}
Dummy Text

\part{Second Part}
\section{Image}

\begin{figure}[ht]
    \centering
    \includegraphics[height=4cm]{example-image-a}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}
some trext
\begin{figure}[ht]
    \centering
    \includegraphics[height=4cm]{example-image-b}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document}

在此处输入图片描述

答案2

二十

在您的序言中添加以下两行标记:

% To modify caption properties
\usepackage[font=footnotesize]{caption}
\DeclareCaptionLabelFormat{bf-part}{\textbf{\thepart.#2}} %<<<<<<<<<<added
\captionsetup{labelformat=bf-part,labelsep=endash} %<<<<<<<<<<added

并删除重复项

% Remove hyphens from captions
\usepackage[labelsep=endash]{caption}

最好也删除

%% Change the way the caption looks like
\captionsetup{labelfont={color=myCaptionBlue,it},textfont={color=myCaptionBlue,it}}

并将所有内容统一到一个地方

% To modify caption properties
\usepackage{caption}
\DeclareCaptionLabelFormat{bf-part}{\textbf{\thepart.#2}} %<<<<<<<<<<added
\captionsetup{%
    labelformat=bf-part, %<<<<<<<<<<added
    labelsep=endash,
    labelfont={color=myCaptionBlue,it},
    textfont={color=myCaptionBlue,it,footnotesize}}

这是完整的代码。

\documentclass[11pt]{extarticle}

% For forcing images to be placed where declared
\usepackage{float}
% To use custom font
\usepackage{fontspec}
% To modify sections/part/chapter etc. properties
\usepackage{titlesec}
% To modify caption properties
\usepackage{caption}
\DeclareCaptionLabelFormat{bf-part}{\textbf{\thepart.#2}} %<<<<<<<<<<added
\captionsetup{%
    labelformat=bf-part,
    labelsep=endash, %<<<<<<<<<<added
    labelfont={color=myCaptionBlue,it},
    textfont={color=myCaptionBlue,it,footnotesize}} 

% To manipulate figure numbers
\usepackage{chngcntr}
% To include images
\usepackage{graphicx}
% To reset section numbers when a new part declared
\counterwithin*{section}{part}
% Remove hyphens from captions
%\usepackage[labelsep=endash]{caption}
% Do not hyphenate text
\usepackage[none]{hyphenat}
% Use colour names and be able to declare colours
\usepackage[table,dvipsnames]{xcolor}
% Change font to Arial
\setmainfont{Arial}
% Set my own colours
\definecolor{myBlue}{HTML}{005B82}
\definecolor{myLightBlue}{HTML}{4F81BD}
\definecolor{myCaptionBlue}{HTML}{1F497D}
% Change the way the Part headings look like
\titleformat{\part}[hang]{\color{myBlue}\normalfont\LARGE\bfseries}{\thepart}{1em}{}
% Part headings to use Alphabets rather than Roman Numerals
\renewcommand{\thepart}{\Alph{part}}
% Change the way the section heading looks like
\titleformat*{\section}{\color{myBlue}\bfseries\fontsize{14}{0}\selectfont}
% Change the way the subsection heading looks like
\titleformat*{\subsection}{\color{myBlue}\bfseries\fontsize{13}{0}\selectfont}
% Change the way the subsubsection heading looks like
\titleformat*{\subsubsection}{\color{myLightBlue}\bfseries\fontsize{11}{0}\selectfont}
% Add a custom subsubsubsection, and change the way the heading looks like for it
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}
    {-3.25ex \@plus-1ex \@minus-.2ex}
    {0.5ex \@plus .1ex}
    {\color{myLightBlue}\itshape\bfseries\fontsize{11}{0}\selectfont}
}
\makeatother
\let\subsubsubsection\paragraph
%%%% Change the way the caption looks like
%%\captionsetup{labelfont={color=myCaptionBlue,it},textfont={color=myCaptionBlue,it}}
% Ensure figure numbers include section numbers into account
\counterwithin{figure}{section} 
\begin{document}
    
    \part{First Part}
    
    \section{Introduction}      
        \begin{figure}[H]
        \centering
        \includegraphics{example-image-a}
        \caption{Caption}
        \label{fig:my_label}
    \end{figure}
    
    Dummy Text
    
    \part{Second Part}      
    \section{Image}     
    \begin{figure}[H]
        \centering
        \includegraphics{example-image-b}
        \caption{Caption}
        \label{fig:my_label}
    \end{figure}
        
\end{document}

相关内容