修改章节的已知 titlesec 宏以实现可变长度规则

修改章节的已知 titlesec 宏以实现可变长度规则

我目前正在排版book样式文档,我使用titlesec包来修改章节标题格式,使用以下代码行:-

\documentclass[a4paper,12pt,twoside,openright]{book}
\usepackage[backend=biber, sorting=count]{biblatex}
\DeclareCiteCommand{\supercite}[\mkbibsuperscript]
{\iffieldundef{prenote}
    {}
    {\BibliographyWarning{Ignoring prenote argument}}%
    \iffieldundef{postnote}
    {}
    {\BibliographyWarning{Ignoring postnote argument}}%
    \bibopenbracket}%
{\usebibmacro{citeindex}%
    \usebibmacro{cite}}
{\supercitedelim}
{\bibclosebracket}
\let\cite=\supercite
\usepackage{
mathtools,amsthm,
siunitx, 
enumerate, 
kantlipsum, 
froufrou, 
bm, 
nicefrac, 
cancel, 
tabularx,booktabs, 
hf-tikz, 
esvect,
pdfpages
}
\usepackage[dvipsnames]{xcolor}
\usepackage[bottom, flushmargin, hang]{footmisc}
\usepackage{caption}
\captionsetup{
format={hang},
font={small},
labelsep={colon},
textfont={it},
labelfont={bf}
}
\usepackage[
bookmarksnumbered=true,
linktoc=page,
colorlinks=true, linkcolor=blue, citecolor=Green,
bookmarksopenlevel=2
]{hyperref}
\usepackage{bookmark}
\usepackage{cleveref}
\usepackage[T1]{fontenc}
\usepackage[scaled]{helvet}
\usepackage{newtxtext,newtxmath}     
\renewcommand{\sfdefault}{phv}
\usepackage{microtype}  
\usepackage[left=1in, right=1in, top=1in, bottom=1in, headsep=30pt]{geometry}                 
\usepackage{setspace, parskip}                   
\setstretch{1.25}          
\usepackage{titlesec}
\titleformat{\chapter}[display]
{\normalfont}{\rule{.05\textwidth}{0.5pt}
    \raisebox{-3pt}{\footnotesize
        \enspace\!\!\!\! CHAPTER \thechapter\!\enspace}\hrulefill\vspace*{-12pt}}
{0pt}{\Huge\bfseries\itshape\filcenter}[\vspace*{6pt}\hrule height 0.5pt]
\assignpagestyle{\chapter}{empty}
\titlespacing{\chapter}{0pt}{0pt}{36pt}         
\titleformat{\section}{\LARGE\bfseries}{\S\,\thesection}{8pt}{}
\titleformat{\subsection}{\Large\bfseries}{\S\,\thesubsection}
{8pt}{\Large\normalfont\itshape}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{calc}
\newlength{\pageoffset}
\setlength{\pageoffset}{10mm}% use whatever you like
\fancyheadoffset[LE,RO]{\pageoffset}
\renewcommand{\chaptermark}[1]{\markboth{Ch\ \thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\S\thesection\ #1}}
\fancyhf{}
\fancypagestyle{fancy2}{
    \fancyhead[LE]{\makebox[\pageoffset][l]{\bfseries\thepage}\bfseries\leftmark}
    \fancyhead[RO]{\bfseries\rightmark\makebox[\pageoffset][r]{\bfseries\thepage}}  
    \renewcommand{\headrulewidth}{0.5pt}
    \fancyfoot{}
}
\fancypagestyle{plain}{%
    \fancyhead{} 
    \renewcommand{\headrulewidth}{0pt}
    \fancyfoot[C]{\bfseries\thepage}
}
\makeatletter
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
    \begingroup
    \mbox{}
    \vspace*{\fill}
    \begin{center}
%       \textcolor{white!75!black}{This page was intentionally left blank.}
    \end{center}
    \vspace{\fill}
    \thispagestyle{empty}
    \newpage
    \if@twocolumn\mbox{}\newpage\fi
    \endgroup\fi\fi}
\makeatother
\begin{document}
\mainmatter
\chapter{Chapter title}
\section{section title}
\kant[1-5]
\end{document}

输出如下: 在此处输入图片描述

我希望顶部和底部规则的长度可以根据章节标题的长度而变化。有人能帮我修改一下吗?

谢谢。

(编辑:- 将“宽度”改为“长度”,因为这可能会导致混淆)

(编辑 2:- 添加了 MWE)

答案1

的最后一个参数\titleformat可以是一个命令,章节标题作为最后一个参数传递给该命令。

因此我们可以区分已编号和未编号的章节。只有当\vatsalchapter具有可选参数时,才会插入“第 N 章”位。

整个内容排版为varwidth,即只使用内容宽度的迷你页面。 规则与标题等距(无编号章节需要视觉校正)。

如果章节标题很“短”,即小于“第 10 章”的宽度加上额外的 6pc(即 72pt,约一英寸),则标题将设置在同样宽的框中。

\documentclass[a4paper,12pt,twoside,openany]{book}
\usepackage[T1]{fontenc}
\usepackage{newtxtext,newtxmath}     
\usepackage[scaled]{helvet}
\usepackage{titlesec}
\usepackage{varwidth}

\usepackage{kantlipsum}

\newlength{\minchapterwidth}
\newlength{\currentchapterwidth}
\AtBeginDocument{%
  \settowidth{\minchapterwidth}{\footnotesize\MakeUppercase{\chaptername} 10}%
  \addtolength{\minchapterwidth}{6pc}%
}


\titleformat{\chapter}[display]
  {\normalfont\Huge\bfseries\itshape\filcenter}
  {}
  {0pt}
  {\vatsalchapter[\MakeUppercase{\chaptername}~\thechapter]}
\titleformat{name=\chapter,numberless}[display]
  {\normalfont\Huge\bfseries\itshape\filcenter}
  {}
  {0pt}
  {\vatsalchapter}

\NewDocumentCommand{\vatsalchapter}{om}{%
  \begin{varwidth}{\textwidth}
  \begingroup
  \footnotesize\normalfont
  \IfValueT{#1}{\vatsalrule{1em}\enspace #1\enspace}% numbered chapter
  \vatsalrule{\fill}\par\nointerlineskip
  \IfNoValueT{#1}{\vspace{-0.5\fontcharht\font`A}}%
  \endgroup
  \vspace{1ex}
  \centering
  \settowidth{\currentchapterwidth}{#2}%
  \ifdim\currentchapterwidth<\minchapterwidth
    \makebox[\minchapterwidth]{#2}%
  \else
    #2%
  \fi
  \par\nointerlineskip
  \vspace{1ex}
  \vatsalrule{\fill}\par
  \end{varwidth}
}
\NewDocumentCommand{\vatsalrule}{m}{%
  \leavevmode
  \leaders\hrule height \dimeval{0.5\fontcharht\font`A+0.2pt}
                 depth \dimeval{0.2pt-0.5\fontcharht\font`A}
  \hskip#1\hspace*{0pt}%
}
  
\assignpagestyle{\chapter}{empty}
\titlespacing{\chapter}{0pt}{0pt}{36pt}         
\titleformat{\section}
  {\LARGE\bfseries}
  {\S\,\thesection}
  {8pt}
  {}
\titleformat{\subsection}
  {\Large\normalfont\bfseries}
  {\S\,\thesubsection}
  {8pt}
  {\itshape}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{Chapter title}

\section{Section title}

\subsection{Subsection title}

\kant[1]

\chapter{This is a very long chapter title that needs to be split across lines}

\kant[2]

\chapter{Short}

\kant[2]

\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案2

尝试这个(简化的)代码。

(1)返回章节标题最长行的长度\titlewidth。必须使用calcwidthpackage 选项。

(2)显式包选项允许使用#1来使标题显式。

(3)block将整个标题写在一个区块(一个段落)中,无需额外的格式。适用于居中标题。

C

\documentclass[a4paper,12pt,twoside,openright]{book}

\usepackage{kantlipsum}

\usepackage[T1]{fontenc}
\usepackage[scaled]{helvet}
\usepackage{newtxtext,newtxmath}     
\renewcommand{\sfdefault}{phv}

\usepackage[left=1in, right=1in, top=1in, bottom=1in, headsep=30pt]{geometry}                 
\usepackage{setspace, parskip}                   
\setstretch{1.25}          

\usepackage[explicit, calcwidth]{titlesec}% changed <<<

\titleformat{\chapter}[block]% % changed <<<
{\filcenter\parbox{\titlewidth}{\rule{.05\linewidth}{0.5pt}%
 \raisebox{-0.6ex}{\footnotesize\, CHAPTER \thechapter\!\enspace}\hrulefill}\\\vspace{1ex}}%
{\Huge\bfseries\itshape\filcenter#1}%
{0pt}{}%
[\vspace{-2ex}\rule{\titlewidth}{0.5pt}]

\titlespacing{\chapter}{0pt}{0pt}{2\baselineskip} % changed <<<

\titleformat{\section}{\Large\bfseries}{\S\,\thesection\enspace #1}{8pt}{}

\titlespacing{\section}{0pt}{\baselineskip}{\baselineskip} %added <<<


\begin{document}
    \mainmatter
    \chapter{Chapter title}
    \kant[1]
    \section{section title}
    \kant[2-5]
\end{document}

更新在后续问题之后。

对于未编号的章节(如目录),需要使用以下格式定义特定格式:

\titleformat{name=\chapter,numberless}[block]{ ...

该命令\addcontentsline{toc}{chapter}{#1}将把该章节添加到目录中(可选)。

X

这是更新后的代码。

\documentclass[a4paper,12pt,twoside,openright]{book}

\usepackage{kantlipsum}

\usepackage[T1]{fontenc}
\usepackage[scaled]{helvet}
\usepackage{newtxtext,newtxmath}     
\renewcommand{\sfdefault}{phv}

\usepackage[left=1in, right=1in, top=1in, bottom=1in, headsep=30pt]{geometry}                 
\usepackage{setspace, parskip}                   
\setstretch{1.25}          

\usepackage[explicit, calcwidth]{titlesec}% changed <<<

\titleformat{\chapter}[block]% % changed <<<
{\filcenter\parbox{\titlewidth}{\rule{.05\linewidth}{0.5pt}%
 \raisebox{-0.6ex}{\footnotesize\, CHAPTER \thechapter\!\enspace}\hrulefill}\\\vspace{1ex}}%
{\Huge\bfseries\itshape\filcenter#1}%
{0pt}{}%
[\vspace{-2ex}\rule{\titlewidth}{0.5pt}]
        
\titleformat{name=\chapter,numberless}[block]% added Unnumbered chapter <<<<<<<<<<<<<<
{\filcenter\rule{\titlewidth}{0.5pt}\\\vspace{1ex}}%
{\addcontentsline{toc}{chapter}{#1}\Huge\bfseries\itshape\filcenter#1}% Add the chapter to the TOC (optional) <<<<
{0pt}{}%
[\vspace{-2ex}\rule{\titlewidth}{0.5pt}]

\titlespacing{\chapter}{0pt}{0pt}{2\baselineskip} % changed <<<

\titleformat{\section}{\Large\bfseries}{\S\,\thesection\enspace #1}{8pt}{}

\titlespacing{\section}{0pt}{\baselineskip}{\baselineskip} %added <<<


\begin{document}
    \mainmatter
    \chapter{Chapter title}
    \kant[1]
    \section{section title}
    \kant[2-5]
    \chapter*{Unnumbered Chapter title}
    \kant[3]
\end{document}

是

相关内容