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:
-
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.
-
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
To prevent a page break, you could use a
minipage
environment. Thesamepage
environment could also be used. Further there's theneedspace
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.You can insert a
\raggedright
command, for instance directly before\@startsection
.There are packages you could use:
titlesec
is very good for customizing headingssectsty
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:Output:
To verify that it works like desired:
\needspace{3\baselineskip}%
there would be a page break between the top line and the section title\raggedright
the word justification would be hyphenated.