如何讓 \titleformat 顯示章節數?

如何讓 \titleformat 顯示章節數?

我有以下 MWE:

\documentclass[twoside]{book}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,headheight=28pt,bindingoffset=6mm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}
\usepackage{fancyhdr}

\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge}
\newpagestyle{mystyle}{
  \sethead[][][\chaptertitle]{\thesection~\sectiontitle}{}{\thepage}}

\begin{document}

\pagestyle{mystyle}
\chapter{A chapter}
\lipsum[1-40]

\end{document}

這成功地從章節標題中刪除了“章節”一詞。不過我也想包括章節數。像'1。介紹'。如何使用 titlesec 執行此操作?

答案1

display如果您希望章節標籤與章節標題位於同一行,則不應使用該樣式。使用預設hang樣式,或者block如果您想將其居中。 Notetwoside不是必要的:它是book類別中的預設值。fancyhdr如果您想titlesecpagestyles加載 的選項一起使用,也不要加載titleps,並且它可能與 發生衝突fancyhdr

這是一個代碼:

\documentclass{book}

\usepackage[a4paper, width=150mm, vmargin=25mm, headheight=28pt, bindingoffset=6mm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}

\titleformat{\chapter}%
  {\normalfont\bfseries\Huge}{\thechapter.}{10pt}{}
\newpagestyle{mystyle}{
  \sethead[][\thechapter.\enspace\chaptertitle][]{}{\thesection~\sectiontitle}{}
\setfoot{}{\thepage}{}}

\begin{document}

\pagestyle{mystyle}
\chapter{A Chapter}
\lipsum[1-5]
\section{A First Section}
\lipsum[6-20]

\end{document} 

在此輸入影像描述

答案2

\thechapter.\quad作為標題設定的一部分包括:

在此輸入影像描述

\documentclass{book}

\usepackage{titlesec}

\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge\thechapter.\quad}

\begin{document}

\chapter{A chapter}

\end{document}

相關內容