答案1
您(错误地)使用了\titleformat
来删除章节和子章节编号,但您将编号和标题之间的空格值设置为1em
。使用选项,hang
即使标签的参数为空,此空格也会在标题前使用。因此,您必须将设置更改为
\titleformat{\subsection}[hang]{\bfseries}{}{0pt}{}
。
但最好将计数器更改secnumdepth
为0
,并使用来\titleformat*
更改小节的字体:
\documentclass{article}
\usepackage{lipsum}% only for dummy text
\usepackage{titlesec}
\titleformat{\section}[block]{\sffamily \Large \filcenter}{}{0pt}{}
\titleformat*{\subsection}{\bfseries}
\setcounter{secnumdepth}{0}
\begin{document}
\section{A section}
\subsection{A subsection}
\lipsum[1-2]
\end{document}
但我从你之前的问题中知道你使用scrreprt
。不要将该包titlesec
与 KOMA-Script 类一起使用!因此这里有一个建议KOMA-Script 命令:
\documentclass[12pt,chapterprefix,sfdefaults=false]{scrreprt}
\usepackage{lipsum}% only for dummy text
\setcounter{secnumdepth}{\chapternumdepth}% only parts and chapters should be numbered
\addtokomafont{disposition}{\mdseries}% only some headings should be bold
\RedeclareSectionCommand[font=\bfseries\large]{chapter}% does the same as \setkomafont{chapter}{\bfseries\large}
\renewcommand\raggedchapter{\centering}% center chapter headings
\RedeclareSectionCommand[font=\sffamily\Large]{section}% does the same as \setkomafont{section}{\sffamily\Large}
\RedeclareSectionCommand[font=\bfseries]{subsection}% does the same as \setkomafont{subsection}{\bfseries}
% format headings with style=section, eg. section, subsection and subsubsection
\let\originalsectionlinesformat\sectionlinesformat
\renewcommand\sectionlinesformat[4]{%
\Ifstr{#1}{section}
{\centering #3#4}% centers section headings
{\originalsectionlinesformat{#1}{#2}{#3}{#4}}% original definition for other section levels
}
\begin{document}
\chapter{Foo}
\section{A section}
\subsection{A subsection}
\lipsum[1-2]
\end{document}