31

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?

cmhughes
  • 100,947
Mykje
  • 805

1 Answers1

38

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}

Gonzalo Medina
  • 505,128
  • 1
    Amazing. I've gone through the titlesec.pdf twice, and couldn't find that simple \usepackage[center]{titlesec} command, until I've found your answer... Can you please tell me where is this documented? – Zvika Mar 22 '16 at 12:39
  • 2
    What if we want to such thing in an article document? – Pedram Apr 12 '17 at 02:50