Your definition will work for sections and subsections, but not for chapters and has a potential drawback in that you loose control over the optional argument for the sectional units; with your definitions, the optional argument will always be equal to the mandatory argument and that might not be desirable.
Here's one option using the sectsty
package:
\documentclass{book}
\usepackage{sectsty}
\usepackage{lipsum}
\allsectionsfont{\centering}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[4]
\subsection{Test Subsection}
\end{document}

And here's now an option using the titlesec
package:
\documentclass{book}
\usepackage[center]{titlesec}
\usepackage{lipsum}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[4]
\subsection{Test Subsection}
\end{document}
These solution horizontally centers the headings for all sectional units; the packages provides commands to modify the formatting on a per-level basis. Here's the code (using sectsty
) to apply the change only to chapters, sections and subsections:
\documentclass{book}
\usepackage{sectsty}
\usepackage{lipsum}
\chapterfont{\centering}
\sectionfont{\centering}
\subsectionfont{\centering}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[4]
\subsection{Test Subsection}
\end{document}
titlesec
package – cmhughes Apr 6 '13 at 19:57