
Я форматирую окончательный вариант своей диссертации и заметил проблему с названиями глав и разделов, которые отображаются в заголовках.
В данный момент я использую fancyhdr для получения названий глав и разделов на альтернативных страницах в заголовках. По разным причинам я не хочу, чтобы вводная глава и заключительная глава были пронумерованы, и я не хочу, чтобы заключительный раздел каждой главы был пронумерован. Чтобы добиться этого, я использовал команды \chapter* и \section*. Однако это имело три нежелательных результата относительно названий глав/разделов, которые появляются в некоторых заголовках:
- «Содержание» отображается в качестве названия главы в заголовке введения:
Но я бы хотел, чтобы там было написано «Введение».
- Название предыдущего раздела отображается в заголовке заключительного раздела глав:
Но я бы хотел, чтобы там было написано «Заключение».
- Название предыдущей главы отображается в заголовке заключительной главы:
Опять же, я бы хотел сказать «Вывод».
Вот 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}