如何使章节标题具有不同长度的标题规则?

如何使章节标题具有不同长度的标题规则?

首先是MWE,其输出如下:

\documentclass{book}

\usepackage{titlesec}%

\let\oldtitleline\titleline
\renewcommand{\titleline}{\oldtitleline*}
\setlength{\titlewidth}{0.3\textwidth}%
\newcommand{\periodafter}[1]{#1.}%

\titleformat{\chapter}{\Large\bfseries\centering}{\chaptertitlename\ \thechapter.}{\wordsep}{\periodafter}[\addvspace{-0.1\baselineskip}{\hfill\titlerule[0.4pt]\hfill\null}]
\makeatother

\setlength{\titlewidth}{0.15\textwidth}%
\titleformat{\section}{\centering}{\thesection.}{\wordsep}{\periodafter}[\addvspace{-0.1\baselineskip}{\hfill\titlerule[0.4pt]\hfill\null}]
 

\begin{document}
\chapter{A chapter title}
\section{A section title}


\end{document}

在此处输入图片描述

如您所见,我重置了 的值来\titlewidth更改章节标题的标题规则长度,但不起作用?那么有没有办法修复它,即让章节标题具有不同长度的标题规则?

答案1

使用您的代码,\titleline绝不叫。

\documentclass{book}

\usepackage{titlesec}

\newcommand{\periodafter}[1]{#1.}

\titleformat{\chapter}
  {\Large\bfseries\filcenter}
  {\chaptertitlename\ \thechapter.}
  {\wordsep}
  {\periodafter}
  [%
   \addvspace{-0.1\baselineskip}%
   \hspace*{\fill}\rule{0.3\textwidth}{0.4pt}\hspace*{\fill}%
  ]

\titleformat{\section}
  {\filcenter}
  {\thesection.}
  {\wordsep}
  {\periodafter}
  [%
   \addvspace{-0.1\baselineskip}%
   \hspace*{\fill}\rule{0.15\textwidth}{0.4pt}\hspace*{\fill}%
  ]

\begin{document}

\chapter{A chapter title}
\section{A section title}

\end{document}

在此处输入图片描述

我建议对 做出不同的定义\periodafter,请看示例以了解原因。

\documentclass{book}
\usepackage{amsthm} % for \@addpunct

\usepackage{titlesec}

\makeatletter
\newcommand{\periodafter}[1]{#1\@addpunct{.}}
\makeatother

\titleformat{\chapter}
  {\Large\bfseries\filcenter}
  {\chaptertitlename\ \thechapter.}
  {\wordsep}
  {\periodafter}
  [%
   \addvspace{-0.1\baselineskip}%
   \hspace*{\fill}\rule{0.3\textwidth}{0.4pt}\hspace*{\fill}%
  ]

\titleformat{\section}
  {\filcenter}
  {\thesection.}
  {\wordsep}
  {\periodafter}
  [%
   \addvspace{-0.1\baselineskip}%
   \hspace*{\fill}\rule{0.15\textwidth}{0.4pt}\hspace*{\fill}%
  ]
 
\begin{document}

\chapter{A chapter title}
\section{A section title!}

\end{document}

在此处输入图片描述

相关内容