答案1
最后一个参数中的最后一项可以\titleformat
是一个需要进一步参数的宏。
\documentclass{article}
\usepackage{titlesec}
\usepackage{xhfill}
\usepackage{xcolor}
\usepackage{lipsum}
\definecolor{rulecolor}{RGB}{228,228,239}
\titleformat{\section}[display]
{\filcenter}
{}
{0em}
{\makesectiontitlenumbered{\thesection}}
\titleformat{name=\section,numberless}[display]
{\filcenter}
{}
{0em}
{\makesectiontitlenonumber}
\newcommand{\makesectiontitlenumbered}[2]{%
\mbox{}\xrfill[0.4ex]{4pt}[rulecolor]\ #1. #2\ \xrfill[0.4ex]{4pt}[rulecolor]%
}
\newcommand{\makesectiontitlenonumber}[1]{%
\mbox{}\xrfill[0.4ex]{4pt}[rulecolor]\ #1\ \xrfill[0.4ex]{4pt}[rulecolor]%
}
\begin{document}
\section*{Introduction}
\lipsum[2]
\section{Preliminaries}
\lipsum[2]
\end{document}
如果您不想对部分进行编号:
\documentclass{article}
\usepackage{titlesec}
\usepackage{xhfill}
\usepackage{xcolor}
\usepackage{lipsum}
\definecolor{rulecolor}{RGB}{228,228,239}
\titleformat{\section}[display]
{\filcenter}
{}
{0em}
{\makesectiontitle}
\newcommand{\makesectiontitle}[1]{%
\mbox{}\xrfill[0.4ex]{4pt}[rulecolor]\ #1\ \xrfill[0.4ex]{4pt}[rulecolor]%
}
\begin{document}
\section{Introduction}
\lipsum[2]
\section{Preliminaries}
\lipsum[2]
\end{document}
答案2
当使用选项时,您可以在的参数#1
中指定章节标题。<before-code>
\titleformat
titlesec
explicit
\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{xhfill}
\usepackage{lipsum}
\titleformat{\section}
[display]
{\filcenter}
{ }{0em}
{\mbox{}\xrfill[0.4ex]{4pt}[blue]\>#1\>\mbox{}\xrfill[0.4ex]{4pt}[blue]}
[]
\begin{document}
\section{Introduction}
\lipsum[2]
\end{document}