Centering sections

I would like to use titlesec to center my sections as follows (MWE):

\documentclass[10pt,a4paper,twoside]{book}
\usepackage[latin1]{inputenc}
\usepackage{lipsum}
\usepackage{titlesec}
\titleformat{\section}% command to format the section titles
[block]% shape/type of title
{\LARGE\bfseries}% formatting commands applied to both label and title
{\begin{center} \thesection \end{center}}% section number; here set inside an invisible box with a constant width
{0em}% separation between number and chapter title; we've already covered this with the box
{}% additional formatting command for title itself not applied to number
[
]%
\begin{document}
\chapter{The first chapter}
\lipsum[1-5]
\section{First section}
\lipsum[3]
\section*{Section}
\lipsum[4]

\end{document}


But this does not center the title of the section, and it is not very beautiful either. How can I beautifully center sections using titlesec?

• Try \centering rather than \begin{center}...\end{center} and put it with the \LARGE\bfseries which applies to the title, too. – cfr Aug 3 '15 at 12:25
• @cfr, I have tried, but it does not work. – usr203050 Aug 3 '15 at 12:28
• My fault! I had an error in my code. Sorry for that. You should have answered it rather than comment it. – usr203050 Aug 3 '15 at 12:43

It will work if you put \centering along with \LARGE\bfseries as suggested by cfr.

\titleformat{\section}% command to format the section titles
[block]% shape/type of title
{\LARGE\bfseries\centering}% formatting commands applied to both label and title
{\thesection}% section number; here set inside an invisible box with a constant width
{1ex}% separation between number and chapter title; we've already covered this with the box
{}% additional formatting command for title itself not applied to number
[
]%


gives

You can also use the simplified format

\titleformat*{\section}{\LARGE\bfseries\centering}


instead of \titleformat.

\documentclass[10pt,a4paper,twoside]{book}
\usepackage[latin1]{inputenc}
\usepackage{lipsum}
\usepackage{titlesec}
%\titleformat{\section}% command to format the section titles
%        [block]% shape/type of title
%        {\LARGE\bfseries\centering}% formatting commands applied to both label and title
%        {\thesection}% section number; here set inside an invisible box with a constant width
%        {1ex}% separation between number and chapter title; we've already covered this with the box
%        {}% additional formatting command for title itself not applied to number
%        [
%        ]%

\titleformat*{\section}{\LARGE\bfseries\centering}
\begin{document}
\chapter{The first chapter}
\lipsum[1-5]
\section{First section}
\lipsum[3]
\section*{Section}
\lipsum[4]

\end{document}


As reminded by Bernard, you can also use \filcenter provided by titlesec instead of \centering.

But it would be easy to use sectsty as in AboAmmar's answer if possible.

• Good, I in fact had an error in my code. I wrote \titleformat{\thesection} instead of \titleformat{\section}. Thank you. – usr203050 Aug 3 '15 at 12:42
• @Harish Kumar: I don't know if there's any side effect with \centering, but titlesec provides the \filcenter command. – Bernard Aug 3 '15 at 13:44
• @Bernard Thanks for reminding \filcenter. I have added a note. BTW, there are not any side effects of \centering that I am aware of. – user11232 Aug 3 '15 at 13:50

A quick solution using the sectsty package

\documentclass[10pt,a4paper,twoside]{book}
\usepackage[latin1]{inputenc}
\usepackage{lipsum}
%\usepackage{titlesec}
\usepackage{sectsty}

\allsectionsfont{\centering}

\begin{document}
\chapter{The first chapter}
\lipsum[1-5]
\section{First section}
\lipsum[3]
\section*{Section}
\lipsum[4]

\end{document}


Or, simply using titlesec by adding the [center] option as this:

\documentclass[10pt,a4paper,twoside]{book}
\usepackage[latin1]{inputenc}
\usepackage{lipsum}
\usepackage[center]{titlesec}

\begin{document}
\chapter{The first chapter}
\lipsum[1-5]
\section{First section}
\lipsum[3]
\section*{Section}
\lipsum[4]

\end{document}


• That is good, but I have used titlesec for various other purposes in the same document. Is it not possible to adapt it to titlesec instead of sectsty? – usr203050 Aug 3 '15 at 12:34
• @usr203050 Did it! – AboAmmar Aug 3 '15 at 12:48