我尝试使用 \begin{center} \end{center} 将文章中的章节和小节居中,虽然这种方法对第一个章节有效,但当我尝试将第二个章节或小节居中时却不起作用。我做错了什么?
答案1
使章节标题居中的最简单的方法是sectsty
:
\documentclass{article}
\usepackage{sectsty}
\allsectionsfont{\centering}
\begin{document}
\section{A section}
Some tex here.
\subsection{A sub section}
Some tex here.
\section*{An un numbered section}
Some tex here.
\subsection*{An un numbered sub section}
Some tex here.
\end{document}
答案2
我假设您尝试将章节和小节的标题居中。为此,我将使用titlesec
包。
\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}{\filcenter\bfseries} {\thesection.}{0.5em}{}
\titleformat{\subsection}{\filcenter\bfseries} {\thesection.\thesubsection.}{0.5em}{}
\titleformat{name=\section,numberless}{\filcenter\bfseries} {}{0.5em}{}
\titleformat{name=\subsectionmny=numberless}{\filcenter\bfseries} {}{0.5em}{}
%% spacings
\titlespacing*{\section} {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\titlespacing*{\subsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\begin{document}
\section{A section}
Some tex here.
\subsection{A sub section}
Some tex here.
\section*{An un numbered section}
Some tex here.
\subsection*{An un numbered sub section}
Some tex here.
\end{document}