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.
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:
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.
\newcommand\myheading[1]{\par
\bigskip
\hrule height 1pt
\kern 2pt
\hbox to \textwidth{\textbf{#1}\hfil}
\kern 2pt
\hrule height 0.5pt
\kern\medskipamount}
We close a paragraph, leave a vertical space and draw a 1pt thick rule; after it we leave a small space (customize it to suit you) and print the heading, with another small space after it. A 0.5pt thick rule and another space.
Four tricks are used in this low level code: (1) a vertical space inserted via \kern
won't be used by TeX to break a page, so the only permissible place for breaking is at the \bigskip
preceding the heading; (2) \hbox
doesn't start the horizontal mode and no glue (so no place for page breaking) is inserted before or after a box contributed to the main vertical list; (3) a \kern
in vertical mode produces a vertical space that is fixed in length; (4) an \hrule
without a width
specification extends to cover the widest box in the vertical list (here the \hbox to \textwidth{...}
. A consequence of the page break rules is that no page break can intervene between the heading and the paragraph following it (provided it doesn't insert penalties, which may happen if the following item is a list such as itemize
).
Gonzalo's solution based on titlesec is very clean, but sometimes a low level code is faster, provided we understand it. :-)
Best Answer
Use the optional argument of
\titlerule
to specify the rule height. Note that you'll have to wrap\titlerule[<height>]
into braces if you're using it in the optional argument of\titleformat
.