Centralizando seções

Centralizando seções

Eu gostaria de titlesec centralizar minhas seções da seguinte maneira (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}

Mas isso não centraliza o título da seção e também não é muito bonito. Como posso centralizar seções lindamente usando titlesec?

Responder1

Funcionará se você seguir \centeringconforme \LARGE\bfseriessugerido pelo 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
        [
        ]%

insira a descrição da imagem aqui

Você também pode usar o formato simplificado

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

em vez de \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}

Conforme lembrado por Bernard, você também pode usar \filcentero fornecido por titlesecem vez de \centering.

Mas seria fácil de usar sectstycomo na resposta de AboAmmar, se possível.

Responder2

Uma solução rápida usando o sectstypacote

\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}

Ou simplesmente usando titlesecadicionando a [center]opção como esta:

\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}

insira a descrição da imagem aqui

informação relacionada