我在用着titlesec
定义自定义子部分标题,通过调整答案来这个问题。
我希望\mysubsubheading
与现有的 共存\subsubsection
,作为一种替代样式。但现在当我使用 时\subsubheading
,子小节编号不再作为格式的一部分出现:
如何定义一种不干扰现有样式的新子部分样式?我不需要在同一子部分中同时使用两种子部分样式;相反,在给定子部分中,我希望能够在使用 和 之间进行\subsubsection
选择\mysubsubsection
。
\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}
\newcounter{mysubsubsection}
\titleclass{\mysubsubsection}{straight}[\subsection]
\titleformat{\mysubsubsection}
{\normalfont\bfseries}{WP \themysubsubsection:~}{0em}{}
\titlespacing*{\mysubsubsection}{0pt}{*3.25}{*1.5}
\counterwithin{subsection}{mysubsubsection}
\renewcommand{\themysubsubsection}{\arabic{mysubsubsection}}
\begin{document}
\subsection{subsection}
\subsubsection{subsubsection}
\mysubsubsection{My subsubsection}
\end{document}
答案1
我只需将样式放入定义它们的宏中,然后使用这些宏来切换样式,\subsubsection
而不会使用自定义\mysubsubsection
宏:
\documentclass{article}
\usepackage{titlesec}
\newcommand*\subsubsectionstyleA
{%
\titleformat\subsubsection
{\normalfont\bfseries}
{WP \thesubsubsection:~}
{0pt}
{}%
\titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
\renewcommand*\thesubsubsection{\arabic{subsubsection}}%
}
\newcommand*\subsubsectionstyleB
{%
\titleformat\subsubsection
{\normalfont\normalsize\bfseries}
{\thesubsubsection}
{1em}
{}%
\titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
\renewcommand*\thesubsubsection{\thesubsection.\arabic{subsubsection}}%
}
\begin{document}
\section{section}
\subsection{subsection}
\subsubsectionstyleA
\subsubsection{subsubsection}
\subsubsection{subsubsection}
\subsection{subsection}
\subsubsectionstyleB
\subsubsection{subsubsection}
\subsubsection{subsubsection}
\end{document}
这样,使用 styleA(您的自定义样式)的子小节计数将在目录中显示为1
、等。如果您希望也包含在目录中,则可以使用类似的方法:2
WP
\documentclass{article}
\usepackage{titlesec}
\newcommand*\subsubsectionstyleA
{%
\titleformat\subsubsection
{\normalfont\bfseries}
{\thesubsubsection:~}
{0pt}
{}%
\titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
\renewcommand*\thesubsubsection{WP~\arabic{subsubsection}}%
}
\newcommand*\subsubsectionstyleB
{%
\titleformat\subsubsection
{\normalfont\normalsize\bfseries}
{\thesubsubsection}
{1em}
{}%
\titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
\renewcommand*\thesubsubsection{\thesubsection.\arabic{subsubsection}}%
}
\begin{document}
\tableofcontents
\section{section}
\subsection{subsection}
\subsubsectionstyleA
\subsubsection{subsubsection}
\subsubsection{subsubsection}
\subsection{subsection}
\subsubsectionstyleB
\subsubsection{subsubsection}
\subsubsection{subsubsection}
\end{document}
答案2
以下是在 中使用布尔值 ( prefix
)的解决方案\titleformat
。但请注意,前缀未包含在目录中(我认为可以包含在 中titletoc
。但未经测试)。
\documentclass{article}
\usepackage{fontspec}
\usepackage[toctitles]{titlesec}
\usepackage{etoolbox}
\newbool{prefix}
\titleformat{\subsubsection}
{\normalfont\bfseries}{\ifbool{prefix}{WP~\thesubsubsection\,:}{\thesubsubsection}}{0.5em}{}
\titlespacing*{\mysubsubsection}{0pt}{*3.25}{*1.5}
\newcommand{\mysubsubsection}[2][]{\booltrue{prefix}\subsubsection[#1]{#2}\boolfalse{prefix}}
\begin{document}
\tableofcontents
\subsection{Subsection}
\subsubsection{A normal subsubsection}
Some text.
\mysubsubsection{My subsubsection}
Some more text.
\subsubsection{Subsubsection}
Some text again.
\mysubsubsection{Another subsubsection}
Still some more text.
\end{document}