# Insert newpage before section but prevent pagebreak after part

Maybe some of you can already guess what I'm trying to achieve; I want to redefine some commands with the following result: every \section should start on a new page except for sections which are placed directly after a \part.

MWE:

\documentclass[a4paper,10pt]{article}
\usepackage{lipsum}

\begin{document}

\part{Part one}
\section{First section}
\lipsum[1]

\section{Second section}
\lipsum[2]

\section{Third section}
\lipsum[1]

\part{Part two}
\section{First section}
\lipsum[3]

\section{Second section}
\lipsum[1]

\end{document}


In the above example, every \section should create a new page but not the "First sections", since they're located directly after a \part command.

I started with

\let\oldsection\section
\renewcommand\section{\newpage\oldsection}


but then I got stuck not finding an answer to the question on how to built up an exception (so that the \part sticks together with the following \section on one page).

I hope you know how to cope with my problem - you probably do; I never saw you being unable to solve a problem!

-
Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. –  Jubobs Jan 10 at 22:12
Probably not an elegant solution, but you can use \oldsection (or call it \firstsection) for the first section in a part. –  Kevin C Jan 10 at 22:31

Use a conditional to turn off page breaks after a part:

\documentclass[a4paper,10pt]{article}

\usepackage{etoolbox}
\newtoggle{afterpart}
\pretocmd{\section}
{\iftoggle{afterpart}
{\global\togglefalse{afterpart}}% we're after a part
{\clearpage}% we're not immediately after a part
}{}{}
\pretocmd{\part}
{\clearpage % do a page break
\global\toggletrue{afterpart}% tell \section we're just after a part
}
{}{}

\usepackage{lipsum}

\begin{document}

\part{Part one}
\section{First section}
\lipsum[1]

\section{Second section}
\lipsum[2]

\section{Third section}
\lipsum[1]

\part{Part two}
\section{First section}
\lipsum[3]

\section{Second section}
\lipsum[1]

\end{document}


If you want to exclude some section from the mechanism, add

\newcommand{\sectionnobreak}{%
\global\toggletrue{afterpart}%
\section
}


to your preamble and use \sectionnobreak instead of \section. Alternatively, add

\newcommand{\NPB}{%
\global\toggletrue{afterpart}%
}


to the preamble and type

\NPB\section{Title}


for the section you want to exclude from the automatic page break. The first section after \part will not need any adjustment in any case.

-
worked great for me! One more tiny variation: would there also be a possibility to exclude one more section manually (in my case it would be the "second section" in "part one")? –  user43961 Jan 10 at 23:07
@user43961 Yes, of course. Expect a fix soon. –  egreg Jan 10 at 23:25

Rather than use a conditional I prefer a command that is defined to be \clearpage or not depending on context.

\documentclass[a4paper,10pt]{article}
\usepackage{lipsum}

\let\oldsection\section
\renewcommand\section{\znewpage\oldsection}

\let\oldpart\part
\renewcommand\part{\clearpage\gdef\znewpage{\global\let\znewpage\clearpage}\oldpart}

\global\let\znewpage\clearpage

\begin{document}

\part{Part one}
\section{First section}
\lipsum[1]

\section{Second section}
\lipsum[2]

\section{Third section}
\lipsum[1]

\part{Part two}
\section{First section}
\lipsum[3]

\section{Second section}
\lipsum[1]

\end{document}

-
I think this answer is fantastic. The only reason I don't like it is that I find it depressing. I realise I will never, ever think properly about TeX! –  cfr Jan 10 at 23:09
@cfr sorry..:-) –  David Carlisle Jan 10 at 23:11
Woah, this answer is clever. I had no clue you could define a macro to redefine itself! Perfect for what I needed, thank you –  dwarduk Oct 18 at 17:51

There is an answer on LaTeX Community on pretty much the same question. For completeness of the answers on TeX.SX, I'm "plagiarizing" that answer here:

\documentclass[a4paper,10pt]{article}
\usepackage{lipsum}

\makeatletter
\newif\iffirstsection \firstsectiontrue
\let\@partOLD\@part
\renewcommand{\@part}[2][]{\clearpage\@partOLD[#1]{#2}\firstsectiontrue}
\let\@spartOLD\@spart
\renewcommand{\@spart}[1]{\clearpage\@spartOLD{#1}\firstsectiontrue}
\renewcommand{\section}{%
\iffirstsection\firstsectionfalse\else\clearpage\fi%
\@startsection {section}{1}%
{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}{\reset@font\Large\bfseries}}
\makeatother

\begin{document}
\part{Part one}
\section{First section}
\lipsum[1]

\section{Second section}
\lipsum[2]

\section{Third section}
\lipsum[1]

\part{Part two}
\section{First section}
\lipsum[3]

\section{Second section}
\lipsum[1]

\end{document}

-