如何在 \titleformat 中添加不同粗细的标题规则

如何在 \titleformat 中添加不同粗细的标题规则

我是新手,正在尝试使用 titlesec 包修改章节的格式。

我正在尝试在章节编号和章节标题之间添加一条水平线,类似于: 在此处输入图片描述

到目前为止,我设法添加了章节编号和章节标题,但我不知道如何使该线具有两种不同的粗细。

我想要将章节编号和较粗部分放在右侧,将章节标题放在左侧。标题规则的粗细可以是 2pt,较粗部分可以是 8pt(关系 1:4)。

这是我的 MWE:

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}        
\usepackage[utf8]{inputenc}     
\usepackage[a4paper,left=3cm,right=2.5cm,top=3cm,bottom=3cm, twoside]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{epigraph}
\usepackage{titletoc}
\usepackage{titlesec} 
\usepackage{blindtext}  
\colorlet{maincolor}{MidnightBlue}

% *****  Title format for Chapters *******
    \titleformat{\chapter}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {\filleft\Huge\color{maincolor}
        \begin{tikzpicture}[baseline={([yshift=-1.0ex]current bounding box.center)}]
            \node {\thechapter};
        \end{tikzpicture}
    }
    {1ex}{\titlerule[2pt]\vspace*{1.5ex}\sffamily\Huge\color{maincolor}}

% ***** Title format for the chapter without numbers *******
    \titleformat{name=\chapter,numberless}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {}{-2ex}{\vspace{-10mm}\sffamily\Huge\color{maincolor}}

% *****  Title format for sections *******
\titleformat{\section}[hang]    
{\normalfont\sffamily\large\bfseries\color{maincolor}} 
{\makebox[2em]{\normalfont\sffamily\large\bfseries\color{black}\thesection.}}
{1em}{}[]

% *****  Title format for subsections *******
\titleformat{\subsection}[hang]{\large\sffamily\color{maincolor}}
{\makebox[2em]{\large\sffamily\color{black}\thesubsection.}} 
{5pt}{}[]      
\begin{document}
\chapter{Introduction}
\epigraph{You can’t do better design with a computer, but you can speed up your work enormously.}{--Wim Crouwel}

\section{Aim of the Project}
\Blindtext

\subsection{Objectives}
\blindenumerate[5]

\section{Motivation} \label{Introoceanenergy}
\Blindtext

\section{Challenges of Wave Energy Systems}
\Blindtext

\section{Report Outline}
\blindtext
\blindenumerate[5]

\end{document}

以下是我得到的结果: 在此处输入图片描述

此外,为了充分利用这个问题,我怎样才能扩展短语,使其具有与上一个片段中显示的宽度相似的宽度?(请注意第 3 章中的短语的宽度与我使用代码获得的短语的宽度之间的差异。)

答案1

我认为这不\titlerule支持这一点,但您可以使用原始的\hrule

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}        
\usepackage[utf8]{inputenc}     
\usepackage[a4paper,left=3cm,right=2.5cm,top=3cm,bottom=3cm, twoside]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{epigraph}
\usepackage{titletoc}
\usepackage{titlesec} 
\usepackage{blindtext}  

\colorlet{maincolor}{MidnightBlue}

% *****  Title format for Chapters *******
\titleformat{\chapter}[display]
  {\normalfont\sffamily\Large\bfseries\color{maincolor}}
  {%
   \filleft\Huge\color{maincolor}%
   \begin{tikzpicture}[baseline={([yshift=-1.0ex]current bounding box.center)}]
     \node {\thechapter};
   \end{tikzpicture}%
  }
  {1ex}
  {%
   \hrule height 2pt \hrule height 4pt width 0.25\textwidth
   \vspace*{1.5ex}%
   \Huge\color{maincolor}%
  }

% ***** Title format for the chapter without numbers *******
\titleformat{name=\chapter,numberless}[display]
  {\normalfont\sffamily\Large\bfseries\color{maincolor}}
  {}
  {0pt}
  {\Huge\color{maincolor}}

% *****  Title format for sections *******
\titleformat{\section}[hang]    
  {\normalfont\sffamily\large\bfseries\color{maincolor}} 
  {\makebox[2em][l]{\color{black}\thesection.}}
  {0pt}
  {}

% *****  Title format for subsections *******
\titleformat{\subsection}[hang]
  {\large\sffamily\color{maincolor}}
  {\makebox[3em][l]{\color{black}\thesubsection.}} 
  {0pt}
  {}

\begin{document}

\chapter*{Preface}

\chapter{Introduction}

\section{Aim of the Project}

\subsection{Objectives}

\end{document}

我对章节和小节的代码做了一些更改:该[l]选项是必需的,否则数字将会缩进。

在此处输入图片描述

如果希望右侧的规则更粗,请将代码更改为

\titleformat{\chapter}[display]
  {\normalfont\sffamily\Large\bfseries\color{maincolor}}
  {%
   \filleft\Huge\color{maincolor}%
   \begin{tikzpicture}[baseline={([yshift=-1.0ex]current bounding box.center)}]
     \node {\thechapter};
   \end{tikzpicture}%
  }
  {1ex}
  {%
   \hrule height 2pt
   \moveright0.75\textwidth\vbox{\hrule height 4pt width 0.25\textwidth}
   \vspace*{1.5ex}%
   \Huge\color{maincolor}%
  }

在此处输入图片描述

答案2

这是一个不需要 Tikz 的解决方案:

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc} no more needed
\usepackage[a4paper,left=3cm,right=2.5cm,top=3cm,bottom=3cm, twoside]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{epigraph}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{blindtext}
\colorlet{maincolor}{MidnightBlue}

% ***** Title format for Chapters *******
    \titleformat{\chapter}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {\filleft\Huge\thechapter\enspace }
    {1ex}{\titlerule[2pt]{\hfill\raisebox{1ex}[0pt]{\rule{60mm}{8pt}}}\vskip1ex\Huge}

% ***** Title format for the chapter without numbers *******
    \titleformat{name=\chapter,numberless}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {}{-2ex}{\vspace{-10mm}\sffamily\Huge\color{maincolor}}

% ***** Title format for sections *******
\titleformat{\section}[hang]
{\normalfont\sffamily\large\bfseries\color{maincolor}}
{\makebox[2em]{\normalfont\sffamily\large\bfseries\color{black}\thesection.}}
{1em}{}[]

% ***** Title format for subsections *******
\titleformat{\subsection}[hang]{\large\sffamily\color{maincolor}}
{\makebox[2em]{\large\sffamily\color{black}\thesubsection.}}
{5pt}{}[]
\begin{document}
\chapter{Introduction}
\epigraph{I refuse to join any club that would have me as a member.}{-- Groucho Marx}

\section{Aim of the Project}
\Blindtext

\subsection{Objectives}
\blindenumerate[5]

\section{Motivation} \label{Introoceanenergy}
\Blindtext

\section{Challenges of Wave Energy Systems}
\Blindtext

\section{Report Outline}
\blindtext
\blindenumerate[5]

\end{document} 

在此处输入图片描述

相关内容