19

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

3 Answers 3

12
  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.
7
  • 1
    @Stefan: Thanks. I try to minimum the extra packages needed, so the minipage and \raggedright tricks are very helpful. However, when I use minigage, it gives me Missing \endcsname inserted. What am I missing? PS: I'm NOT using a backslash in front of minigage! Nov 11, 2010 at 8:55
  • 1
    @Sadeq: use \protect before \end{minipage}.
    – Stefan Kottwitz
    Nov 11, 2010 at 12:15
  • @Stefan: Thanks again. It now works, but the minipage made the code so that the horizontal line and the section heading stick together in a single line. I edited the original question to reflect the new code; could you suggest a fix? Nov 11, 2010 at 14:12
  • If you use samepage instead of minipage there would be a line break after the \hrule and before the title. needspace would cause less problems. I will edit my answer adding an example.
    – Stefan Kottwitz
    Nov 11, 2010 at 15:05
  • Oops, not everything! One problem goes away, another one appears :( The \raggedright not only affects the section titles, it also affects the text that comes after the title (body text). Nov 11, 2010 at 16:45
17

I suggest to use the titlesec package. It should solve both issues. Otherwise it may require more details in form of a complete but minimal example. See code below for some inspiration.

\documentclass[11pt,a4paper,twocolumn,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[raggedright]{titlesec}
\usepackage{blindtext}

\titleformat{\section}{\titlerule\vspace{1ex}\normalfont\Large\bfseries}{\thesection}{1em}{}

\begin{document}
  \blinddocument
\end{document}

Note that the blindtext package is only used for creating dummy text thus not part of the solution.

6

Obligatory ConTeXt solution:

% Define a frame that only draws the top frame and typesets its content flushleft
\defineframed[sectionframe]
             [frame=off,   
              topframe=on,
              rulethickness=1pt,
              align=flushleft,
            ]

\def\sectioncommand#1#2{\sectionframe{#1 #2}}
% #1 is section number 
% #2 is section title

% Ask ConTeXt to use our section command.
\setuphead[section]
          [command=\sectioncommand,
           style=\bfa, % equivalent to \large\bfseries
          ]


% Test case
\starttext
\input knuth
\section{A section with a very long title that spans multiple lines to check if
         line breaking works correctly}
\input knuth
\stoptext
1
  • 1
    i don't know of any actual "obligation" to add context solutions, but i actually rather like the idea. even for those seriously committed to latex, they're a useful reminder of "the other side" ;-) ... so thanks Mar 21, 2011 at 9:37

You must log in to answer this question.

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