[Tex/LaTex] How to put a line above a section heading and prevent a page break between

horizontal alignmentpage-breakingrulessectioningsections-paragraphs

We have designed a class for some journal. We wanted our section titles to be separated by a horizontal line from the previous line. Here is the code:

\def\section{%
    \vskip 1.2\@bls%
    \hrule height 1pt%
    \@startsection{section}{1}{\z@}{-1pt}{1pt}{\normalfont\large\bfseries}%
}

There are two problems, though:

  1. Sometimes, when a page (or column) ends, the horizontal line remains at the preceding page (or column), and the paragraph title is printed on the new page (or column). I read something about setting some penalty value to fix this, but I wasn't successful.

  2. We like our section titles to be aligned left, but LaTeX justifies and hyphenates them. Adding the command \flushleft changed nothing.


Edit:

Using Stefan Kottwitz's excellent answer, I changed the above code as follows. Unfortunately, it still does not work: LaTeX typesets the section heading after the horizontal line, without any line/paragraph break whatsoever.

\def\section{%
    \begin{minipage}{7cm} %
    \vskip 1.2\@bls%
    \hrule height 1pt%

    \raggedright\@startsection{section}{1}{\z@}{-1pt}{1pt}{\normalfont\large\bfseries}%
    \protect\end{minipage}%
}

Best Answer

  1. To prevent a page break, you could use a minipage environment. The samepage environment could also be used. Further there's the needspace package: the command \needspace requests a user-defined amount of space, otherwise it inserts a page break. That can be useful for headings because they should also not be right at the end of the page.

  2. You can insert a \raggedright command, for instance directly before \@startsection.

There are packages you could use:

  • titlesec is very good for customizing headings

  • sectsty is also useful, for instance for 2. the command \sectionfont{\raggedright} would be sufficient.

Since the needspace package offers a nice solution and might cause less problems, I recommend to use its \needspace command. Since I know you don't like to load additional packages, here's a minimal example with only the copied \needspace command for a demonstration:

\documentclass{article}
\makeatletter
\newcommand{\needspace}[1]{\begingroup\setlength{\dimen@}{#1}%
  \vskip\z@\@plus\dimen@ \penalty -100\vskip\z@\@plus -\dimen@
  \vskip\dimen@ \penalty 9999\vskip -\dimen@\endgroup}% from needspace.sty
\def\section{%
    \needspace{3\baselineskip}%
    \vskip 1.2ex%
    \hrule height 1pt%
    \@startsection{section}{1}{\z@}{-1pt}{1pt}{%
      \raggedright\normalfont\large\bfseries}%
}
\makeatother
\begin{document}
Text
\vspace{.93\textheight}
\section{This is a very, very long heading just to test the justification}
\end{document}

Output:

alt text

To verify that it works like desired:

  • if you remove the line \needspace{3\baselineskip}% there would be a page break between the top line and the section title
  • if you remove \raggedright the word justification would be hyphenated.