我想在每个部分之前添加水平线,就像这样
手动的话我可以直接放在\rule{\textwidth}{1pt}
每个section前面,但是我想要更灵活的方法,比如通过在序言中放置几段代码来控制section的样式来实现。
答案1
您可能不希望该规则位于第一节之前,也不希望该规则位于节从页面顶部开始时。以下代码可实现此目的。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{lipsum}
\titleformat{\section}
{\sectionrule\Large\bfseries}
{\thesection}
{1em}
{}
% this command is executed at each \section command
\newcommand{\sectionrule}{%
% no rule before the first section
\ifnum\value{section}=0
\else
% otherwise, ensure being between paragraphs
\par
% add some vertical space
\addvspace{\bigskipamount}%
% the rule realized as leaders, so it disappears at a page break
% see also http://tex.stackexchange.com/a/61643/4427
\leaders\vrule width \textwidth\vskip0.4pt
% some other vertical space
\bigskip
\fi
}
\begin{document}
\section{First}
\lipsum[1-3]
\section{Second}
\lipsum[2-6]\lipsum[2]\lipsum[2]
\section{Third}
\lipsum
\end{document}
替代解决方案,在出现分页符的情况下将规则放在页面顶部。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{lipsum}
\titleformat{\section}
{\sectionrule\Large\bfseries}
{\thesection}
{1em}
{}
\newcommand{\sectionrule}{%
\par
\addvspace{\bigskipamount}%
\hrule
\nopagebreak
\bigskip
}
\begin{document}
\section{First}
\lipsum[1-3]
\section{Second}
\lipsum[2-6]\lipsum[2]\lipsum[2]
\section{Third}
\lipsum
\end{document}
答案2
使用 titlesec 很简单:
\documentclass{article}% http://ctan.org/pkg/amsproc
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{titlesec}
\titleformat{\section}{\vbox{\rule{\linewidth}{0.8pt}}\bigskip\LARGE\bfseries}{\thesection}{1em}{}
\begin{document}
\begin{equation}
v = \sqrt{2gl(\cos\phi_0 - \cos \phi)}
\end{equation}
\section{Section two}
\end{document}