# How to start chapter on the same page and keep my header and footer (already have codes available)

## Set up

At the moment here is my code for my first page and general layout of my project:

\documentclass[12pt, oneside]{book}

\usepackage{sectsty,fancyhdr}
\sectionfont{\LARGE\bfseries}
\usepackage{geometry}

\usepackage{lipsum}%remove

% *******************************Margin Settings*******************************

\geometry{% margin settings, has to go first before \pagestyle{}
paper=a4paper,
inner=2.5cm, % Inner margin
outer=2.5cm, % Outer margin
bindingoffset=0.0cm, % Binding offset
top=2.5cm, % Top margin
bottom=2.5cm, % Bottom margin
footskip=6.4mm,% controles footer
%showframe,% show how the type block is set on the page
}

\sectionfont{\LARGE\bfseries}%this will keep the formattings of sections
\pagestyle{fancy}
\fancyhf{} % sets all head and foot elements empty.
\rhead{} % right side of upper footer
\lhead{Project title} %left side
\renewcommand{\headrulewidth}{2.0pt} % Width of the top line
\renewcommand{\footrulewidth}{1.0pt} % Bottom line
\setlength{\headheight}{14.5pt}%used to remove the \fancyhf{} \headheight error
\rfoot{Page \thepage} % vice versa with the page number
\lfoot{\leftmark} % Should show the name of section

\makeatletter
\renewcommand{\sectionmark}[1]{%this avoids the \makecaptical of footers
\markboth{\ifnum \c@secnumdepth>\z@ %this allowes symbols in foorters
\thesection\hskip 1em\relax
\fi #1}{}}
\makeatother

%****************************ADD NEW CODE HERE**********************

%***************************Title************************************
\newcommand{\mytitle}{
\begin{center}
{\Huge{\textbf{Project of...}}}
\end{center}

\begin{center}
\textsc{\Large My name}\\[0.5cm]
\end{center}

\begin{center}
{\large My ID number}\\[0.5cm]
\end{center}
}

%------------------------------------------------------------------

% Begin

%------------------------------------------------------------------

\begin{document}

\mytitle
\chapter{chapter 1}
\section{section 1.1}
\subsection{subection 1.1.1}
\lipsum%remove

\chapter{chapter 2}
\section{section 2.1}
\subsection{subection 2.1.1}
\lipsum%remove

%------------------------------------------------------------------

% end

%------------------------------------------------------------------

\end{document}


## The Problem

This is "code 1" (to be added to my preambles in the "new code" section above):

\fancypagestyle{chapterstart}{% 1st page of the chapters, i.e. no new page for chapter
}
\makeatletter

\def\chapter{
\pagestyle{chapterstart}
\secdef\@chapter\@schapter}

\renewcommand\thesection{\@arabic\c@section}

\makeatother


This code this allows me to start a new chapter on the same page, and it also keeps my header and footer on the same page. However this resets the section numbers for each chapter. e.g. "chapter 2, section 2.1" would instead become "chapter 2, section 1.1".

This is "code 2" (To be added above, as before):

\usepackage{etoolbox}
\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother


This code allows me to start a chapter on the same page and keeps the original numbering for sectioning, e.g. "chapter 2, section 2.1" would stay as "chapter 2, section 2.1". However my header and footer will not be included on the same page

## The Goal

How do I combine "code 1" and "code 2" to allow me to: start a new chapter on the same page, keep my header and footer on the new chapter pages, and keep the formatting for the section numbering.

• Why do you use book and \chapter if you don't want chapters. Couldn't you just use article with topmost level \section instead? – Schweinebacke Jun 30 '17 at 7:09
• @Schweinebacke I never said I didn't want chapters? And plus I need the book class for a custom table of contents that i'm using. But thank you for the input. – UniStuffz Jun 30 '17 at 9:53
• But you want to remove the characteristics of chapters. Why couldn't you customize the table of contents of article? For me this looks like a xy-problem. – Schweinebacke Jun 30 '17 at 10:56

Code 1 would have worked without \renewcommand\thesection{\@arabic\c@section}. This omits the chapter number (the original is \thechapter.\@arabic\c@section).

But there is a downside, it doesn't set \@afterindentfalse, which would lead to an indented first paragraph after the chapter title for normal text.

The line \pagestyle{chapterstart} is not necessary. The original \chapter sets \thispagestyle{plain}. Without this, the page style isn't changed.

In code 2 \chapter is changed with \patchcmd, which has the advantage that a warning or an error can be given out, if the command changed and can't be patched anymore (last argument, example in code).

\documentclass[12pt, oneside]{book}

\usepackage{sectsty,fancyhdr}
\sectionfont{\LARGE\bfseries}
\usepackage{geometry}

\usepackage{lipsum}%remove

% *******************************Margin Settings*******************************

\geometry{% margin settings, has to go first before \pagestyle{}
paper=a4paper,
inner=2.5cm, % Inner margin
outer=2.5cm, % Outer margin
bindingoffset=0.0cm, % Binding offset
top=2.5cm, % Top margin
bottom=2.5cm, % Bottom margin
footskip=6.4mm,% controles footer
%showframe,% show how the type block is set on the page
}

\sectionfont{\LARGE\bfseries}%this will keep the formattings of sections
\pagestyle{fancy}
\fancyhf{} % sets all head and foot elements empty.
\rhead{} % right side of upper footer
\lhead{Project title} %left side
\renewcommand{\headrulewidth}{2.0pt} % Width of the top line
\renewcommand{\footrulewidth}{1.0pt} % Bottom line
\setlength{\headheight}{14.5pt}%used to remove the \fancyhf{} \headheight error
\rfoot{Page \thepage} % vice versa with the page number
\lfoot{\leftmark} % Should show the name of section

\makeatletter
\renewcommand{\sectionmark}[1]{%this avoids the \makecaptical of footers
\markboth{\ifnum \c@secnumdepth>\z@ %this allowes symbols in foorters
\thesection\hskip 1em\relax
\fi #1}{}}
\makeatother

%****************************ADD NEW CODE HERE**********************

%***************************Title************************************
\newcommand{\mytitle}{
\begin{center}
{\Huge{\textbf{Project of...}}}
\end{center}

\begin{center}
\textsc{\Large My name}\\[0.5cm]
\end{center}

\begin{center}
{\large My ID number}\\[0.5cm]
\end{center}
}

%------------------------------------------------------------------
% Code 2
\usepackage{etoolbox}
\makeatletter
% no new page for \chapter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
% don't change the pagestyle
\patchcmd{\chapter}{\thispagestyle{plain}}{}{}{%
% example for a warning, 'Package' in text necessary to make TexStudio show it.
\GenericWarning{(preamble)\@spaces\@spaces\@spaces\@spaces}{Package preamble Warning: patching \string\chapter\space did not work.}}
}
% allow floats on top of the page with a new chapter
\patchcmd{\chapter}{\global\@topnum\z@}{}{}{}
% if not commented out, first paragraph will be indented
%\patchcmd{\chapter}{\@afterindentfalse}{}{}{}
%\makeatother

%------------------------------------------------------------------

% Begin

%------------------------------------------------------------------

\begin{document}

\mytitle
\chapter{chapter 1}
\section{section 1.1}
\subsection{subection 1.1.1}
\lipsum%remove

\chapter{chapter 2}
\section{section 2.1}
\subsection{subection 2.1.1}
\lipsum%remove

%------------------------------------------------------------------

% end

%------------------------------------------------------------------

\end{document}

• Thank you for the full explanation in comments. Really helpful. I'm still trying to figure out to re-define commands, or learn how to make my own commands. Do you know what resources I can go to? – UniStuffz Jun 30 '17 at 10:24