# [Tex/LaTex] Error while trying to start chapters on the same page

chapterspage-breakingsectioningtitlesec

This is my first post here. I have tried the solution proposed in this site to start chapters in the same page as the previous one ended but I always get an error. I am a TeX newbie and I feel that I'm not capable of finding the answer myself.

I am using the report class and a few other commands and packages to make the titles behave according to my thesis specifications but when I used the clearpage solutions proposed in the other topic I get an error:

Chapter 2.
! Package titlesec /b/c9/cError:/b/c0/c Entered in horizontal mode.
See the titlesec package documentation for explanation.

How can I solve this and start chapters right after the previous one ended? I'll do my best to provide a MWE:

\documentclass[11pt,a4paper]{report}
\usepackage{titlesec}
\newcommand{\cchapter}[1]{\chapter[#1]{\centering #1}}
\newcommand{\ssection}[1]{\section[#1]{\centering #1}}
\newcommand{\ssubsection}[1]{\subsection[#1]{\centering #1}}
\titleformat{\chapter}[hang]
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter:}{1em}{}
\titlespacing*{\chapter}{0pt}{0pt}{40pt}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother

\begin{document}
...
\cchapter{first}
bla bla bla
\cchapter{second}
bla bla bla2
\end{document}


Thanks!

The problem is that with your patch to \chapter you're not ensuring vertical mode (that is, that a paragraph is terminated) and so \chapter. It's also better to define all sectional titles using titlesec facilities

\documentclass[11pt,a4paper]{report}
\usepackage{titlesec}

\titleformat{\chapter}[hang]
{\normalfont\huge\bfseries}
{\chaptertitlename\ \thechapter:\ }
{0pt}
{\filcenter}
\titlespacing*{\chapter}{0pt}{40pt}{40pt}
\titleformat*{\section}{\centering\Large\bfseries}
\titleformat*{\subsection}{\centering\large\bfseries}

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

\begin{document}
\chapter{first}
bla bla bla
\section{Sec}
bla bla bla
\subsection{Subsec}
bla bla bla
\chapter{second}
bla bla bla2
\end{document}


No need for \cchapter, \ssection and \ssubsection.