2

So, I've seen this, but I need sections to start on a new page only after the first one

And I've been trying many different methods, and I was thinking that I could make an if statement somewhere that if the section number exceeds 2, it'll begin adding a \clearpage. But I need this if statement to run with each new section that gets created, in theory

My major attempt at this was to do it inside my already-exsiting \titleformat command, since I thought that it'll run the if with each new section, and it looked something like this:

\newcommand{\sectionbreak}{} % create it empty at first to modify later
\titleformat{\section}
{\large\bfseries}
{\thesection}
{0pt}
{
\ifnum\value{section} > 1
{\renewcommand{\sectionbreak}{\clearpage}}
\else{\renewcommand{\sectionbreak}{}}
\fi
}
{}

I thought it'll run the if after each section, because, in another document, I have another \ifnum statement similar to this one that makes a \hspace smaller in the separator, if the section number is larger than 10

But, this doesn't work, and I don't know what else to try. So, any solutions? This is going to be something used in both a book, and also in an article documentclass. So, class isn't very relevant

Andy3153
  • 361
  • 2
    Could you please provide a MWE beginning with \documentclass, ending with \end{document}, and including only such packages as might be relevant here (stuff like titlesec if you are using it...)? I'm assuming the \documentclass is one that has chapters in addition to sections (so not article...)? – marquinho Apr 04 '22 at 06:27

2 Answers2

4

Just define \sectionbreak properly:

\documentclass{book}

\usepackage{titlesec}

\titleformat{\section} {\large\bfseries} {\thesection} {1em} {} \newcommand{\sectionbreak}{\ifnum\value{section}>0 \clearpage\fi}

\begin{document}

\tableofcontents

\chapter{One:}

\section{on same page}

Some text

\section{On new page}

Some text

\section{On new page}

Some text

\chapter{Two:}

\section{on same page}

Some text

\section{On new page}

Some text

\section{On new page}

Some text

\end{document}

enter image description here

egreg
  • 1,121,712
2

This solution adapts your already promising approach with \ifnum.

However, I found (in a MWE I made) that placing the conditional code in the 5th argument of \titleformat, as you did, messes with the section headers. I.e., the desired page break is placed between section number and title. The 5th argument, officially before-code, is executed "before the title body" but apparently after the section number.

My favoured approach in these cases would be with the \preto macro from etoolbox, because the conditional then becomes active right before the entire workings of \section.

\usepackage{etoolbox}
\preto\section{\ifnum\value{section}=0
\else\clearpage\fi}

Assuming you are working with a document class that has chapters in addition to sections (say book), and that the "first" section refers to the first section in each chapter, this would be a MWE:

\documentclass{book}

\usepackage{etoolbox}

\usepackage{titlesec}

\titleformat{\section} {\large\bfseries} {\thesection} {0pt} {} {}

\preto\section{\ifnum\value{section}=0 \else\clearpage\fi}

\begin{document} \tableofcontents

\chapter{One:}

\section{on same page}

\section{On new page}

\section{On new page}

\chapter{Two:}

\section{on same page}

\section{On new page}

\section{On new page}

\end{document}

table of contents with page numbers

marquinho
  • 1,721
  • I am going to use this method, I found another way to do what I was asking for, by replacing the \sectionbreak command renewals with the code snippet that got accepted as an answer inside the question I mentioned (this: \AddToHook{cmd/section/before}{\clearpage}). I'm going to use your method though, because I noticed mine messes with section headers as you mentioned, adding some extra space. – Andy3153 Apr 04 '22 at 06:53
  • 1
    @Andy3153 Thanks for accepting :) Right, now I've seen the other answer in your linked question. To be fair, the method with the hook is even simpler and more organic than mine (if your LaTeX distribution is reasonably new) – marquinho Apr 04 '22 at 06:57
  • 1
    @Andy3153 Still, for future reference, could you please edit your question to say which document class you are using? – marquinho Apr 04 '22 at 06:58
  • Well, the thing is, this was going to be something used in both a book, and also in an article documentclass. So, class isn't very relevant. Should I add anything in this case? – Andy3153 Apr 04 '22 at 07:20
  • 1
    I see! Maybe you could add what you just wrote, it's certainly helpful to know! – marquinho Apr 04 '22 at 07:32
  • 1
    @Andy3153 Actually @egreg's answer should be the accepted one. Do feel free to un-accept mine :). And follow @egreg's advice as it's the intended way to use titlesec! – marquinho Apr 04 '22 at 08:57