Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using the following command in my class (thanks to this answer):

\renewcommand\section{\@startsection{section}{1}{\z@}%
                                {3.25ex \@plus1ex \@minus.2ex}%
                                {-1em}%
                                {\normalfont\large\bfseries}*}

Now I had the problem that the section title happened to be the last thing on a page, and its corresponding text ended up in the next page. How to change this definition to prevent this problem to happen? I tried adding a \nopagebreak as last thing before }, with no success...

share|improve this question

closed as unclear what you're asking by Werner, Speravir, Jesse, ChrisS, OSjerick Jan 10 at 5:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What you ask for is the default behavior of the \@startsection command. Also the definition you exhibit for \section puts the section title on the same line as the text of the first paragraph, which contradicts your description. So please provide a minimal working example (MWE) document that exhibits this problem. –  Dan Dec 5 '13 at 5:39

2 Answers 2

You could use the titlesec package to redefine the \section command. For example, it says:

If you dislike typing the full skip values, including the plus and minus parameters, an abreviation *n is provided (where n is an integer). In the beforesep argument this is equivalent to n ex with some stretchability and a minute shrinkability. In the aftersep some strechability (smaller) and no shrinkability. Thus, you can write

\titlespacing*{command}{left}{beforesep}{aftersep}[right]
\titlespacing{\section}{0pt}{*4}{*1}

Edit: The following example I believe successfully keeps the \section with the text below it.

\documentclass[letterpaper]{article}

\usepackage{lipsum} % generate filler text
\usepackage{titlesec}

\titleformat{\section}[runin]
{\large\bfseries}
{}{0pt}{}
\titlespacing{\section}
{0pt}{*2}{*1}

\begin{document}

\section{Section 1}

\lipsum[1-5]

\section{Section 2}

\lipsum[1-5]

\end{document}
share|improve this answer
    
And how those spacing settings will effect the prevention of page break just after the title? –  lvella Dec 5 '13 at 1:46
    
See edited post with new example –  I Like to Code Dec 5 '13 at 2:04
    
I think you misunderstood me (or maybe I was not clear enought) you managed to get the title on the same line of the text; that is not what I wanted. I wanted the title on one line, followed by the text paragraph below it, but should have a way to tell LaTeX the title and the text are semantically related so the title must not end a page so its text will start next. It is like using ~ to prevent the line break algorithm to place two words on separated lines... –  lvella Dec 5 '13 at 2:22
    
Ah. When I compiled the example using the code which you originally provided in the question, in the output, the text followed the title like the \paragraph command. –  I Like to Code Dec 5 '13 at 2:35

I didn't understand why exactly it worked, but I managed to do it. I just had to add \leavevmode at the beginning of the definition:

\renewcommand\section{\leavevmode\@startsection{section}{1}{\z@}%
                                {3.25ex \@plus1ex \@minus.2ex}%
                                {-1em}%
                                {\normalfont\large\bfseries}*}

If you know why it works or whether it as the "right" solution, please tell me!

EDIT: it seems it doesn't affect all cases where a page break could happen, so it is not a complete solution.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.