在未編號的章節的標題中交替章節標題

在未編號的章節的標題中交替章節標題

我正在格式化論文的最終草稿,並注意到標題中出現的章節標題有問題。

目前,我正在使用 fancyhdr 來獲取標題中備用頁面上的章節標題。由於不同的原因,我不希望對引言章節和結論章節進行編號,也不希望對每章的結論部分進行編號。為了實現這一點,我使用了 \chapter* 和 \section* 命令。然而,這對於出現在某些標題中的章節標題產生了三個不必要的結果:

  1. 「目錄」在簡介的標題中顯示為章節標題:

在此輸入影像描述

但我希望它說“簡介”。

  1. 前面的章節名稱作為章節標題出現在各章結束部分的標題:

在此輸入影像描述

但我希望它說“結論”。

  1. 上一章標題顯示為結論章節標題中的章節標題:

在此輸入影像描述

我再次想說「結論」。

這是一個 MWE:

\documentclass[12pt, twoside]{report}

\usepackage[explicit]{titlesec} %adjust titles
\usepackage{lipsum} %random text

%%%%%%%%Chapter and section titles in headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{} %clears header
\fancyhead[CE]{\nouppercase{\textit{\leftmark}}} %puts chapter title on even page in lower-case italics
\fancyhead[CO]{\nouppercase{\textit{\rightmark}}} %puts section title on odd page in lower-case italics
\renewcommand{\headrulewidth}{0pt} %gets rid of line
\renewcommand{\chaptermark}[1]{\markboth{#1}{}} %gets rid of chapter number
\renewcommand{\sectionmark}[1]{\markright{#1}} %gets rid of section number
%%%%%%%%%

\titleformat{\chapter}[display]{\LARGE\bfseries\centering}{\thechapter}{10pt}{#1}
\titleformat{\section}{\normalfont\fontsize{20}{20}\centering}{\thesection}{1em}{\MakeUppercase{#1}}
\titleformat{\subsection}{\normalfont\centering}{\thesubsection}{1em}{\textit{{#1}}}

\usepackage{subfiles} %allows multi-file projects

\setcounter{tocdepth}{1} %removes subsubsections from toc

%%%%%%%%%removes dots from toc
\makeatletter 
\renewcommand{\@dotsep}{10000} 
\makeatother
%%%%%%%%%

\begin{document}

\tableofcontents

\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}

\renewcommand{\thesection}{\arabic{section}}

\section{An Introductory Section}

\lipsum
\lipsum[1]

\section{Another Introductory Section}

\lipsum

\chapter{A Chapter}
\renewcommand{\thesection}{\thechapter.\arabic{section}}


\lipsum[2-3]

\section{A Section}

\lipsum[4-8]

\section*{Conclusion}

\lipsum[8-9]

\chapter{Another Chapter}
\renewcommand{\thesection}{\thechapter.\arabic{section}}


\lipsum[1-2]

\chapter*{Conclusion}
\addcontentsline{toc}{chapter}{Conclusion}

\lipsum

\end{document}

我知道有人問過類似的問題(例如目錄和標題中的未編號章節)但它們似乎是關於 KOMA-Script 的。鑑於這是最終草案,如果可以使用 fancyhdr 來完成此操作並且無需更改大量內容,那就太理想了。謝謝!

答案1

我建議這個更簡單的程式碼,帶有pagestyles選項titlesec,它定義了\chaptermark命令,對於未編號的部分, (將在和\pretitlemark中使用)\titleformat \section*):

\documentclass[12pt, twoside]{report}

\usepackage[explicit, pagestyles]{titlesec} %adjust titles
\usepackage{lipsum} %random text

\titleformat{\chapter}[display]{\LARGE\bfseries\filcenter}{\thechapter}{10pt}{#1}
\titleformat{name=\chapter, numberless}[display]{\LARGE\bfseries\filcenter}{}{10pt}{\chaptermark{#1}\addcontentsline{toc}{chapter}{#1}#1}

\titleformat{\section}{\normalfont\fontsize{20}{20}\filcenter}{\thesection}{1em}{\MakeUppercase{#1}}
\titleformat{name=\section, numberless}{\normalfont\fontsize{20}{20}\filcenter}{}{0em}{\pretitlemark{section}{#1}\MakeUppercase{#1}}
\renewcommand{\thesection}{\ifthechapter{\thechapter.}{}\arabic{section}}

\titleformat{\subsection}{\normalfont\filcenter}{\thesubsection}{1em}{\textit{{#1}}}

\newpagestyle{myps}{%
\sethead[][\itshape\chaptertitle][]{}{\itshape\sectiontitle}{}
\setfoot{}{\thepage}{}
}
%%%%%%%%%Chapter and section titles in headers
\pagestyle{myps}
\usepackage{subfiles} %allows multi-file projects

\setcounter{tocdepth}{1} %removes subsubsections from toc

%%%%%%%%%removes dots from toc
\makeatletter
\renewcommand{\@dotsep}{10000}
\makeatother
%%%%%%%%%

\begin{document}

\tableofcontents

\chapter*{Introduction}

\section{An Introductory Section}

\lipsum
\lipsum[1]

\section{Another Introductory Section}

\lipsum

\chapter{A Chapter}

\lipsum[2-5]

\section{A Section}

\lipsum[6-8]

\pretitlemark{section}{Conclusion}
\section*{Conclusion}

\lipsum[9-15]

\chapter{Another Chapter}

\lipsum[13-16]

\chapter*{Conclusion}

\lipsum[17-20]

\end{document} 

相關內容