[Tex/LaTex] Centering chapter/section/subsection

horizontal alignmentsectioning

I am trying to center my chapters/sections/subsections.

I have used the following:

\newcommand{\cchapter}[1]{\chapter[#1]{\centering #1}}
\newcommand{\ssection}[1]{\section[#1]{\centering #1}}
\newcommand{\ssubsection}[1]{\subsection[#1]{\centering #1}}

This seems to work for chapter, but not for section and subsection. What is wrong?

Best Answer

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}

enter image description here

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}