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.

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!

share|improve this question
1  
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
    
I'm so sorry. In fact, I read about this some time ago already; but since I wasn't too sure about this anymore, I thought I'd better be polite rather than not. Back to topic: I started thinking about a solution with the "samepage" environment, but finally, I think this wouldn't work neither since it would get far too complex to create an abstract environment that way, including variables which define the "next section title" and so on... –  user43961 Jan 10 at 22:32

3 Answers 3

up vote 10 down vote accepted

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.

share|improve this answer
    
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
1  
@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}
share|improve this answer
    
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

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}}
\@addtoreset{section}{part}
\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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