我想要使用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 提醒的那样,您还可以使用\filcenter
provided bytitlesec
来代替\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}