latex: adding pagebreaks at sections

May 11th, 2007

Stephen Wright once said something to the effect:

I have a huge collection of sea shells. It's spread out on all the beaches of the world.

That's an exact description on the state of latex documentation. Sure, here's probably the most powerful typesetting language known to man, well probably just the one man who actually knows it, the rest of us know bits and pieces. But, when you actually need to do something that you haven't done before, or you've done but you can't remember, bon voyage.

Safe trip on that extensive google search, finding ancient web pages describing good old techniques (latex hasn't changed much over the years decades), 404 links to packages that once were in use, and a great deal of tips & tricks that seem useful, but are nothing like what you need to do right now.

Sometimes you'll find the answer. Sometimes you'll give up. Sometimes you'll conclude it's not possible (or at least, not unless you're a latex wizard). In general, it is possible. But because latex is used and abused by so many in so many different ways, over so many years, it's naturally hard to keep track of who accomplished what and how.

But, there is no centralized documentation at all. Latex is so huge that it needs to be extensively documented, but what you find instead is some professor who wrote a tutorial for his students for that particular assignment, or a list of all symbols you can use, or all kinds of bits and pieces, but nowhere can you find the whole. Not how the different programs are related to each other, how to write a fairly general Makefile for them, how to actually construct a workflow out of it. For that you better hope there is someone willing to guide you through it in the beginning.

One of the things I've wanted to do for some time is enforce a pagebreak before every section, because in some cases it just makes sense. Thrilled that I am that today I stumbled upon one of those ancient pages that has a working recipe for it. When you look at the solution, it's ridiculously simple, but when you don't know it... well.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pagedsections}[2007/05/11 Adding pagebreaks before sections]

\let\oldsection = \section
\renewcommand{\section}[1]{
	\pagebreak
	\oldsection{#1}
}

Then, of course, include it into the document as usual:

\documentclass[12pt]{article}

\usepackage{pagedsections}

\begin{document}
\section{first}
blahdeeblah
\section{second}
blah
\end{document}

This lack of documentation is common for applications that predate the age of the internet, or at least the "modern" internet, not including usenet and whatever other deprecated forms of communication. For instance, bash suffers from an acute lack of in-depth documentation.

:: random entries in this category ::

14 Responses to "latex: adding pagebreaks at sections"

  1. Kevin Mark says:

    Isn't there some tips and trick wiki for tex like tug.org or somesuch?

  2. numerodix says:

    A wiki? I don't think I've seen one yet.

  3. John Healy says:

    One exists, for sure, at http://en.wikibooks.org/wiki/Latex

    I have no idea how comprehensive it is though. I'm going to guess 'not'.

    \vfill might be of interest to you given your interest in \pagebreak.

  4. [...] text here. Okay, let me first look up how I do that. How do you actually find this out? I already covered that earlier. Then try it, debug the layout, finally I can see if I got what I wanted. No, that [...]

  5. Gabriel Many says:

    So, this worked, but...it gave each of my unnumber sections (section*, I'm using lyx) a number and stuck them namelessly (just an asterisk) into the table of contents. If you can, help me! I will post if I solve this.

  6. numerodix says:

    Actually, there is a numbering/toc bug with this method and I'm unable to use it myself any longer. I don't know exactly why, and debugging latex is a nightmare.

  7. Gabriel Many says:

    Well, after some research, it looks like a complex way of doing this is to redefine section formating in the preamble. See: http://theoval.sys.uea.ac.uk/~nlct/latex/thesis/node9.html#tab:secnum

    The basic format is:
    \@startsection{}{}{}{}{}{}

    Which led me to create the following (using what I believe are the defaults for the report class):

    \renewcommand{\section}{\@startsection
    {section}% % the name
    {1}% % the level
    {0mm}% % the indent
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {2.3ex \@plus.2ex}%
    {\clearpage\normalfont\Large\bfseries}}

    The thing I added was the \clearpage
    Now, I'd prefer to tell it to just use class defaults for the beforeskip and afterskip commands, but I don't know how to do so, so I used the actual value I found in report.cls.

    Do you have something more elegant?

  8. numerodix says:

    Have you tested this with \tableofcontents? I pasted your code right into the preamble of a report I'm writing right now, changed the document type to report and I get this:

    pdfTeX warning (ext4): destination with the
    same identifier (name{page.1}) has been already used, duplicate ignored

    \relax
    l.84 \newpage
    [1] (./main.tex
    ! You can't use `\spacefactor' in vertical mode.
    \@->\spacefactor
    \@m
    l.1 \section
    {Introduction}

    Still evil lurking somewhere..

  9. Gabriel Many says:

    I have tried it on two different machines running two different tex distributions on two different operating systems with two different version of Lyx and it has worked both times. I did note that the comments messed things up when I cut and pasted into the Lyx preamble in Windows, so it is possible that you are having some sort of error in that sense. Nevertheless, the link I provided in my previous post gives more detailed information about the options. As I said, I went into report.cls and copied the section definition to make sure that I got the font style right, since basically I am just redefining section to have a "clearpage" added to the style.

    In the end, I don't really know that I'll use this, but it seems that it could be useful to have a style that causes a pagebreak without having to start a whole new chapter.

    Here it is without the comments:

    \renewcommand{\section}{\@startsection
    {section}
    {1}
    {0mm}
    {-3.5ex \@plus -1ex \@minus -.2ex}
    {2.3ex \@plus.2ex}
    {\clearpage\normalfont\Large\bfseries}}

  10. numerodix says:

    Well, I don't know what it is, but it refuses to compile, even with a "Hello world" document. And I tried the code from that page you referenced. I keep getting the error "You can't use `\spacefactor' in vertical mode." on the line where I have \section, which I don't understand.

    I tried this both on ubuntu, which ships texlive, and gentoo which still has tetex. I'm not using lyx, just a regular editor.

  11. Gabriel Many says:

    See http://www.math.upenn.edu/tex_docs/help/faq/uktug-faq/FAQ229.html

    I did a quick google search for your error, and indeed, it is recommended that you place it like this (using \makeatletter and \makeatother):

    \makeatletter
    \renewcommand{\section}{\@startsection
    {section}
    {1}
    {0mm}
    {-3.5ex \@plus -1ex \@minus -.2ex}
    {2.3ex \@plus.2ex}
    {\clearpage\normalfont\Large\bfseries}}
    \makeatother

  12. numerodix says:

    Yikes. The quirks never end, do they?

    Thank you, this works beautifully!

  13. TX says:

    Thank you so much for the \pagebreak before sectioning. I was on the exact type of fruitless Google search you mentioned looking for it.

    You're absolutely right that LaTeX documentation is spread all over the place. There's no one convention to write code, there are thousands of random packages to download, and therefore there is a huge learning curve to using this powerful typesetting language.

    Too bad. If organized and given a sleek new standard in coding and more intuitive commands, LaTeX would become much easier to use.

  14. swgreen says:

    Use with hyperref:

    When using the solution here in combination with hyperref, section links point to the page *before* the beginning of the new section. To remedy this, I used the following:

    \makeatletter
    \renewcommand{\section}{\newpage{} \@startsection
    {section}
    {1}
    {0mm}
    {-3.5ex \@plus -1ex \@minus -.2ex}
    {2.3ex \@plus.2ex}
    {\normalfont\Large\bfseries}}
    \makeatother