# Prevent a paragraph break after a section heading

In my .cls file for my thesis, I have defined \subsection as follows:

\newcommand{\subsection}[1]{%
\pagebreak[2]
\refstepcounter{subsection}
{\protect\makebox[0.3in][r]{\thesubsection.} \hspace*{3pt}#1}}
\noindent
\textbf{\thesubsection\space\space{#1}. }
}


However, the thesis examiner has told me that the proper way to format subsection headings is to make the first paragraph continue on the same line as the subsection heading.

I can make this work if my document says

\subsection{A Subsection}
Lorem ipsum dolor sit amet...


typesets to

3.1.1 A Subsection.  Lorem ipsum dolor sit amet...


But if I have a blank line at the beginning of the subsection, I get a paragraph anyway.

\subsection{A Subsection}

Lorem ipsum dolor sit amet...


typesets to

3.1.1 A Subsection.
Lorem ipsum dolor sit amet...


Is there any way to force this to typset like the first version, even if there's a blank line after the \subsection command? If not, is there at least a way to get a warning?

-

Use the titlesec package to redefine your sections: Here's an example of your subsection style:

\usepackage{titlesec} % or RequirePackage[loadonly]{titlesec} in a cls file
\titleformat{\subsection}[runin]{\normalfont\bfseries}{\thesubsection.}{3pt}{}


Note that if you do this in a class file, you will need to load titlesec with the [loadonly] option, and use titlesec to define all of the sectioning command your class will allow.

-
This isn't working for me. I'm getting Undefined control sequence. \section \ttl@extract\section when I \RequirePackage{titlesec}. –  Ken Bloom Jan 10 '11 at 20:39
OK. I need \RequirePackage[loadonly]{titlesec}. –  Ken Bloom Jan 10 '11 at 20:42
@Ken Bloom: Ok. That means you need to provide titlesec definitions for all the sectioning commands your class will use. I've modifed my answer to reflect this. Thanks. –  Alan Munn Jan 10 '11 at 21:06
@Alan, I'm getting there now, but it involves rewriting all of my sectioning and table of contents stuff to do it this way. Is there another way that I can accomplish this without having to rewrite everything? –  Ken Bloom Jan 10 '11 at 21:30
@Ken Bloom: I don't think there's a simple way out of that. You could copy the definition of paragraph from e.g. report.cls and use that to redefine subsection; but redefining sections is tricky business to do by hand generally. –  Alan Munn Jan 10 '11 at 22:01

A quick cheap way to avoid the paragraph break is to say \par (so that you're in vertical mode) and then put the subsection name into \everypar instead of putting it directly onto the page. That way, the subsection name isn't put onto the page until you type something that puts you back into horizontal mode. For example, you can use

\newcommand{\mysubsection}[1]{%
\par
\pagebreak[2]%
\refstepcounter{subsection}%
\everypar={%
{\setbox0=\lastbox}% Remove the indentation
{\protect\makebox[0.3in][r]{\thesubsection.} \hspace*{3pt}#1}}%
\textbf{\thesubsection\space\space{#1}. }%
\everypar={}%
}%
\ignorespaces
}


and then say

\mysubsection{A new subsection}

Whatever whatever whatever


and you're in business.

-
This appears to be what LaTeX's \@startsection and titlesec do internally, but I couldn't follow their code because they wrote it to be much more generic yours. Thanks for the simplification. –  Ken Bloom Jan 14 '11 at 18:17
I tried this, and couldn't get it right -- sometimes it kept sticking the heading before every paragraph (I think it was interfering with my \TODO command), and it would have taken more work to make it break before the section break in \subsection{Foo}\section{Bar} (those will go away eventually, but I have to come up with stuff to write first). It seems there are lots of subtleties in taking the \everypar approach yourself. –  Ken Bloom Jan 16 '11 at 20:20
Oops; sorry about that. Does it help to change the \everypar={} into \global\everypar={}? (I can't reproduce the problem, so I can't test it.) –  Phil Hirschhorn Jan 31 '11 at 23:50