[Tex/LaTex] Centering chapter titles

chapters

In the MWE below. What is the most convenient way to center the titles Preface and Abstract, rather than setting them flush left?

\documentclass{book}
\begin{document}

\chapter*{Preface}
\chapter*{Abstract}

\chapter{Introduction}
\chapter{Body}
\chapter{Conclusion}

\end{document}

Best Answer

Since this about the \chapter* command for two titles (Abstract and Preface) only, it's sufficient to say \chapter*{\centering Abstract} etc. as command. This is grouped inside the internal command \@makeschapterhead -- it does not have a side effect on other implicitly used \chapter* commands, such as in \tableofcontents.

(The \blindtext as well as Some text aren't centered, so the \centering inside the \chapter* argument is safe, as long as no entry to the ToC is necessary (which is not the case, as we are talking about \chapter*)

If all \chapter* or \chapter titles should be centered horizontally, an \xpatchcmd approach is more useful. (See the edit at the bottom of this answer)

\documentclass{book}



\usepackage{blindtext}

\begin{document}

\chapter*{\centering Preface}
\blindtext

Some text
\chapter*{\centering Abstract}


\chapter{Introduction}
\chapter{Body}
\chapter{Conclusion}

\end{document}

enter image description here

Edit

This code centers all chapter titles (regardless whether \chapter* or \chapter) horizontally

\documentclass{book}

\usepackage{xpatch}
\usepackage{blindtext}


\makeatletter

\xpatchcmd{\@makeschapterhead}{%
  \Huge \bfseries  #1\par\nobreak%
}{%
  \Huge \bfseries\centering #1\par\nobreak%
}{\typeout{Patched makeschapterhead}}{\typeout{patching of @makeschapterhead failed}}


\xpatchcmd{\@makechapterhead}{%
  \huge\bfseries \@chapapp\space \thechapter
}{%
  \huge\bfseries\centering \@chapapp\space \thechapter
}{\typeout{Patched @makechapterhead}}{\typeout{Patching of @makechapterhead failed}}

\makeatother

\begin{document}
\tableofcontents

\chapter*{Preface}
\blindtext

Some text
\chapter*{Abstract}
\blindtext


\chapter{Introduction}
\blindtext

Another text


\chapter{Body}
\blindtext

\chapter{Conclusion}
\blindtext


\end{document}