Центрирующие секции

Центрирующие секции

Я хотел бы использовать titlesec для центрирования своих разделов следующее (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}

Но это не центрирует заголовок раздела, и это не очень красиво. Как мне красиво центрировать разделы с помощью titlesec?

решение1

Это сработает, если вы примените \centeringто \LARGE\bfseries, что предлагает 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
        [
        ]%

дает

введите описание изображения здесь

Вы также можете использовать упрощенный формат

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

вместо \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}

Как напомнил Бернард, вы также можете использовать \filcenterprovided by titlesecвместо \centering.

Но его было бы легко использовать, sectstyкак в ответе АбоАммара, если это возможно.

решение2

Быстрое решение с использованием sectstyпакета

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

Или просто titlesecдобавьте [center]следующую опцию:

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

введите описание изображения здесь

Связанный контент