
Я хотел бы использовать 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}
Как напомнил Бернард, вы также можете использовать \filcenter
provided 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}