セクションの中央揃え

セクションの中央揃え

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

cfr の提案\centeringに従って実行すれば動作します。\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
        [
        ]%

与える

ここに画像の説明を入力してください

簡易形式を使用することもできます

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

Bernard が指摘したように、の代わりに\filcenterprovided byを使用することもできます。titlesec\centering

sectstyしかし、可能であれば、AboAmmar の回答のように簡単に使用できるでしょう。

答え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}

ここに画像の説明を入力してください

関連情報