我有以下代码
\documentclass[12pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[
left=1in,
right=1in,
top=1.25in,
bottom=1.25in,
headsep=30pt,
heightrounded
]{geometry}
\usepackage[x11names, svgnames, dvipsnames]{xcolor}
\usepackage{amsmath}
\numberwithin{equation}{subsection}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
%==============================================================
\usepackage{fancyhdr}
%\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
%\renewcommand{\sectionmark}[1]{\markright{#1}}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\small\nouppercase{\rightmark}}
\fancyhead[RE]{\small\nouppercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}
%==============================================================
\newtheorem{theorem}{Theorem}[section]
\usepackage{lipsum}
\begin{document}
\pagenumbering{roman}
\thispagestyle{plain}
\tableofcontents
\addcontentsline{toc}{chapter}{Table of Contents}
\thispagestyle{plain}
\newpage
\pagenumbering{arabic}
\chapter{Preliminaries}
\section{Basics}
\lipsum[1-15]
\chapter{$C3$-Modules}
\lipsum[1-30]
\end{document}
如你所见,第 3 章没有章节。我需要第 3 章 $C3$ 模块出现在章节每一页的页眉中,而不仅仅是偶数页。我需要对不包含章节的章节采用这种行为。
答案1
您可以测试空的\rightmark
并在这种情况下使用\leftmark
(我已经删除了几个与问题无关的包):
\documentclass[12pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}% should also not be needed from LaTeX 2018-04-01
\usepackage[
left=1in,
right=1in,
top=1.25in,
bottom=1.25in,
headsep=30pt,
heightrounded
]{geometry}
%==============================================================
\usepackage{scrextend}% provides \Ifstr (and others)
\providecommand\Ifstr{\ifstr}% for historic LaTeX installations only.
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\small\nouppercase{\Ifstr{\rightmark}{}{\leftmark}{\rightmark}}}
\fancyhead[RE]{\small\nouppercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}
%==============================================================
\usepackage{lipsum}
\begin{document}
\cleardoublepage% not needed in this case, but needed, e.g. if there is a
% title before the following lines.
\pagenumbering{roman}
\thispagestyle{plain}% not needed, because default for chapters and therefore table of contents
\addcontentsline{toc}{chapter}{Table of Contents}
\tableofcontents
\newpage% should \cleardoublepage, or use \mainmatter instead of \pagenumbering
\pagenumbering{arabic}
\chapter{Preliminaries}
\section{Basics}
\lipsum[1-15]
\chapter{$C3$-Modules}
\lipsum[1-30]
\end{document}
注意:您应该在之前使用\cleardoublepage
而不是。否则,可能会出现两个奇数页连在一起的情况,这在打印文档时会产生问题。我建议使用和而不是,因为这样在需要时会自动使用。\newpage
\pagenumbering
book
\frontmatter
\mainmatter
\pagenumbering
\cleardoublepage
另请注意,添加\addcontentsline
后 \tableofcontents
将使用目录的最后一页而不是第一页作为目录中的参考。最好使用包tocbibind
来添加此类条目(或使用已经支持此类条目的类)。