如何透過控制節的樣式在每個節之前添加一條水平線

如何透過控制節的樣式在每個節之前添加一條水平線

我想在每個部分之前添加水平線,就像這樣

在此輸入影像描述

手動我可以放在\rule{\textwidth}{1pt}每個部分之前,但我想要一種更靈活的方法,例如透過在序言中放置幾段程式碼來控制部分的樣式來實現這一點。

答案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} 

在此輸入影像描述

相關內容