使用 \titleformat 进行分段会导致 \subsection* 出现问题

使用 \titleformat 进行分段会导致 \subsection* 出现问题

我正在尝试编写一个带有小节但不带节号的序言(因为从技术上讲,序言实际上不是报告的一部分)。节和小节是使用 \titleformat 构建的,如下所示:

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}
{\thesection.}{0.5em}{#1}
[\vspace{3pt}
{\color{\colorstyle}{\titlerule[1.5pt]}}
\vspace{1pt}
\titlerule]
\titlespacing*{\section}
{0em}{2em}{1em}

\titleformat{\subsection}
{\normalfont\bfseries\filcenter}
{}{0pt}
{\textcolor{\colorstyle}{\noindent\Vhrulefill}\hspace{1em}
\thesubsection.
\hspace{0.5em}#1\hspace{1em}\textcolor{\colorstyle}{\Vhrulefill}}
\titlespacing*{\subsection}
{0em}{2em}{0.5em}

但是,\section* 工作正常,没有编号,而 \subsection* 的编号为“0.0 subsectiontitle”,但我不需要任何编号。有人知道为什么会发生这种情况以及如何避免它吗?

工作示例:

\documentclass{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[usenames,dvipsnames]{xcolor}

\newcommand{\colorstyle}[1]{\def\colorstyle{#1}}
\definecolor{mycolor}{RGB}{0,80,158}
\colorstyle{mycolor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Sections
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[explicit,pagestyles]{titlesec}

\def\Vhrulefill{\leavevmode\leaders\hrule height 0.7ex depth \dimexpr0.4pt
0.7ex\hfill\kern0pt}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}
{\thesection.}{0.5em}{#1}
[\vspace{3pt}
{\color{\colorstyle}{\titlerule[1.5pt]}}
\vspace{1pt}
\titlerule]
\titlespacing*{\section}
{0em}{2em}{1em}

\titleformat{\subsection}
{\normalfont\bfseries\filcenter}
{}{0pt}
{\textcolor{\colorstyle}{\noindent\Vhrulefill}\hspace{1em}
\thesubsection.
\hspace{0.5em}#1\hspace{1em}\textcolor{\colorstyle}{\Vhrulefill}}
\titlespacing*{\subsection}
{0em}{2em}{0.5em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section*{Section without a number}
Bacon ipsum dolor sit amet pork loin leberkas turducken.

\subsection*{Subsection behaving oddly}
Beef pork belly porchetta turducken, bresaola venison boudin doner.

\end{document}

答案1

使用以下numberless功能:

\documentclass{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[usenames,dvipsnames]{xcolor}

\newcommand{\colorstyle}[1]{\def\colorstyle{#1}}
\definecolor{mycolor}{RGB}{0,80,158}
\colorstyle{mycolor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Sections
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[explicit,pagestyles]{titlesec}

\def\Vhrulefill{\leavevmode\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}

\titleformat{\section}
  {\normalfont\Large\bfseries\filcenter}
  {\thesection.}
  {0.5em}
  {#1}
  [\vspace{3pt}%
   {\color{\colorstyle}{\titlerule[1.5pt]}}
   \vspace{1pt}
   \titlerule]

\titlespacing*{\section}
  {0em}
  {2em}
  {1em}

\titleformat{\subsection}
  {\normalfont\bfseries\filcenter}
  {}
  {0pt}
  {\textcolor{\colorstyle}{\noindent\Vhrulefill}%
   \hspace{1em}\thesubsection.\hspace{0.5em}#1\hspace{1em}%
   \textcolor{\colorstyle}{\Vhrulefill}}

\titleformat{name=\subsection,numberless}
  {\normalfont\bfseries\filcenter}
  {}
  {0pt}
  {\textcolor{\colorstyle}{\noindent\Vhrulefill}%
   \hspace{1em}#1\hspace{1em}%
   \textcolor{\colorstyle}{\Vhrulefill}}

\titlespacing*{\subsection}
  {0em}
  {2em}
  {0.5em}

\begin{document}

\section{Abc def ghi}

\subsection{Abc def ghi}

\section*{Abc def ghi}

\subsection*{Abc def ghi}

\end{document}

在此处输入图片描述

相关内容