Generalconditional new page

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
BrainBurner
Posts: 1
Joined: Thu Oct 18, 2007 5:39 pm

conditional new page

Post by BrainBurner »

Hallo,
I'm trying to renew the section command: I'd like to create a new page for each section, but not for the first one! That's because I also use \part, and after every "part title" should begin the first section, but now I have a black page because of the command \newpage.

Do you know ho to solve this problem? Maybe with a if condition, but how?

The command now is the following:

Code: Select all

\renewcommand{\section}{\@startsection {section}{1}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{2.3ex \@plus.2ex}{\reset@font\Large\bfseries}}
Thanks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

conditional new page

Post by Juanjo »

Check the following code, which should go in the preamble:

Code: Select all

\makeatletter
\newif\iffirstsection \firstsectiontrue
\let\@partOLD\@part
\renewcommand{\@part}[2][]{\@partOLD[#1]{#2}\firstsectiontrue}
\let\@spartOLD\@spart
\renewcommand{\@spart}[1]{\@spartOLD{#1}\firstsectiontrue}
\renewcommand{\section}{%
     \iffirstsection\firstsectionfalse\else\newpage\fi%
     \@startsection {section}{1}%
      {\z@}{-3.5ex \@plus -1ex \@minus -.2ex}%
      {2.3ex \@plus.2ex}{\reset@font\Large\bfseries}}
\makeatother
Good luck
avoyance
Posts: 13
Joined: Sun Feb 13, 2011 1:53 am

conditional new page

Post by avoyance »

Juanjo wrote:Check the following code, which should go in the preamble:

Code: Select all

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

This code is only good for the first chapter. But for the second, and the chapters follows, it has no use. Sorry I do not know how to modify it.
avoyance
Posts: 13
Joined: Sun Feb 13, 2011 1:53 am

conditional new page

Post by avoyance »

BrainBurner wrote:Hallo,
I'm trying to renew the section command: I'd like to create a new page for each section, but not for the first one! That's because I also use \part, and after every "part title" should begin the first section, but now I have a black page because of the command \newpage.

Do you know ho to solve this problem? Maybe with a if condition, but how?

The command now is the following:

Code: Select all

\renewcommand{\section}{\@startsection {section}{1}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{2.3ex \@plus.2ex}{\reset@font\Large\bfseries}}
Thanks
The new command is wrong. The error message is "paragraph stretching is not allowed" (or something like that). Sorry I do not know how to correct it.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

conditional new page

Post by Juanjo »

avoyance wrote: This code is only good for the first chapter. But for the second, and the chapters follows, it has no use. Sorry I do not know how to modify it.
It would be convenient to support such a criticism with some code or well-founded arguments :( . In fact, it seems to me that you have misunderstood the opening post, which only mentions the use of parts and sections. It can be logically assumed that the underlying document class is article (or an article-like class). So it has no sense to speak about chapters. It is quite clear that the above code should be adapted for the book and report classes if sections, except the first one, are required to start in a new page inside every chapter. The code works well for articles, except, perhaps some details that may need some tweaking depending on the author's needs. Assume, for example, that, coherently, parts should also start in a new page, that the section counter must be reset in each part, and that floats of a section should be placed before the beginning of the next section. Here you have a complete example:

Code: Select all

\documentclass[a4paper]{article}

\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}
\title{Test of some code}
\author{You and Me}
\date{\today}
\maketitle
\begin{abstract} Testing some code about parts and sections. \end{abstract}

\tableofcontents

\part{Title of the first part}
\section{Title of the first section in the first part}
This section goes in the same page as the part title.

\section{Title of the second section in the first part}
This section goes in a new page.

\section{Title of the third section in the first part}
This section goes in a new page.

\part{Title of the second part}
\section{Title of the first section in the second part}
This section goes in the same page as the part title.

\section{Title of the second section in the second part}
This section goes in a new page.

\section{Title of the third section in the second part}
This section goes in a new page.

\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
avoyance
Posts: 13
Joined: Sun Feb 13, 2011 1:53 am

Re: conditional new page

Post by avoyance »

I tried the new one. But it is the same if I use book class, i.e. it is good only for the first chapter. Sorry I do not know how to modify it.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

conditional new page

Post by Juanjo »

avoyance wrote:I tried the new one. But it is the same if I use book class, i.e. it is good only for the first chapter. Sorry I do not know how to modify it.
You have definitely misunderstood the original post and my replies. The code is designed for the article class, ok? It doesn't work with book or report. This is obvious. Finally, it is not so difficult to adapt the code to the book document class. Start by replacing part by chapter. Good luck.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply