Zentrierabschnitte

Zentrierabschnitte

Ich möchte titlesec meine Abschnitte wie folgt zentrieren (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}

Dadurch wird der Abschnittstitel jedoch nicht zentriert und es sieht auch nicht sehr schön aus. Wie kann ich Abschnitte mithilfe von schön zentrieren titlesec?

Antwort1

Es wird funktionieren, wenn Sie die Vorschläge von cfr \centeringbefolgen .\LARGE\bfseries

\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
        [
        ]%

gibt

Bildbeschreibung hier eingeben

Sie können auch das vereinfachte Format verwenden

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

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

Wie Bernard erinnert, können Sie anstelle von auch \filcenter„bereitgestellt von “ verwenden .titlesec\centering

Aber wenn möglich wäre es einfach, es sectstywie in AboAmmars Antwort zu verwenden.

Antwort2

Eine schnelle Lösung mit dem sectstyPaket

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

Oder verwenden Sie es einfach, titlesecindem Sie die [center]Option wie folgt hinzufügen:

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

Bildbeschreibung hier eingeben

verwandte Informationen