# Error while trying to start chapters on the same page

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!

-
i think the cited question is start-new-chapter-on-same-page . – barbara beeton Jun 27 '12 at 12:44

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.

-
Thank you very much. This answer solved my problem! – RJCPS Jun 29 '12 at 9:04
@RJCPS Not because I need reputatation, of course; but marking the most helpful answer as the accepted one can help future readers more than a simple comment like yours. – egreg Jun 29 '12 at 9:26

The method given in this answer fails in your case, due (I think) to the titlesec package. A variant, in which we replace the \clearpage and \cleardoublepage commands in \chapter with \par seems to work, though.

\documentclass[11pt,a4paper]{report}
\usepackage{titlesec}
\newcommand{\cchapter}[1]{\chapter[#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}{\par}{}{}
\makeatother

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

-
Thanks for your time, I tried both and your solution also worked. – RJCPS Jun 29 '12 at 9:06