4

I want to add horizontal lines before every section, just like this

enter image description here

manually I can just put \rule{\textwidth}{1pt} before every section, but I want a more flexible method, for example, by putting several code in the preamble to control the style of section to achieve this.

2 Answers 2

3

You probably don't want the rule before the first section, nor when a section starts at the top of a page. The following code accomplishes this.

\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} 

enter image description here

Alternate solution, with the rule at the top of the page in case of a page break.

\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}

enter image description here

7
  • Thank you. This works, but could you please explain the code? I really want to learn something, not just copying code.
    – user15964
    Apr 22, 2015 at 14:04
  • And what if I really want horizontal line before every section, no matter where the section is.
    – user15964
    Apr 22, 2015 at 14:05
  • @user15964 Where should the line go in case of a page break? That's the big problem.
    – egreg
    Apr 22, 2015 at 14:06
  • Sorry, I don't get it. The line is just right above the section.
    – user15964
    Apr 22, 2015 at 14:21
  • @user15964 I mean: in case of a page break, should the rule sit in the same page as the section title or at the bottom of the preceding page?
    – egreg
    Apr 22, 2015 at 14:22
3

It's easy with 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} 

enter image description here

5
  • Thank you. But could you please explain your code a little? What is more, I am currently using XeLatex, and I found your code seems not working in XeLatex mode.
    – user15964
    Apr 22, 2015 at 13:54
  • I'll add some details in a moment. Did you compile the code as is or did you use it within another file? On my system, I have no problem witth xelatex.
    – Bernard
    Apr 22, 2015 at 14:04
  • Yeah, you are right. Your example is OK. I used it in another file. And the error is quite strange, It says "! LaTeX Error: Command `\hbar' already defined." and "l.148 ...MathSymbol{\hbar}{\mathord}{symbols}{157}"
    – user15964
    Apr 22, 2015 at 14:13
  • Oh, I figure out. I only need two lines "\usepackage{titlesec} \titleformat{\section}{\vbox{\rule{\linewidth}{0.8pt}}\bigskip\LARGE\bfseries}{\thesection}{1em}{}"
    – user15964
    Apr 22, 2015 at 14:15
  • I guess it's my loading fourier for maths that redefines \hbar. Remove it, it was there only for personal taste.
    – Bernard
    Apr 22, 2015 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .