我正在使用fancyhdr
包在每页顶部添加章节名称。一个简单的 LaTeX 示例的代码:
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}%Add chapter name at the beginning of each page
%\pagestyle{fancy}
\setlength{\headheight}{15pt}% ...at least 51.60004pt
% Title Page
\usepackage[titletoc,title]{appendix}
\title{}
\author{}
\begin{document}
\maketitle
\begin{abstract}
\end{abstract}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancyhead[R]{}
\fancyhead[L]{\chaptername\ \thechapter\ --\ \leftmark}
\chapter{First chapter about happiness}
First page about the argument
\newpage
Second page about the argument. I want that this page has the chapter name at the top of the page.
\chapter{Hello}
\begin{appendices}
\chapter{Hi}
Appendix about something
\newpage
I do not want that this page has the chapter name at the top of the page.
\end{appendices}
\end{document}
正如你所看到的,这样它就继续在页面顶部命名“章节章节末编号 附录名称“而我希望章节名称仅出现在报告章节的页面顶部,而不出现在附录页面或参考书目中。我该怎么做?
答案1
最小更改
首先,您应该\clearpage
在 之前添加一个\end{appendices}
,否则 的更改appendices
将在附录的最后一页结束之前结束。在此之后,您还可以使用\@chapapp
而不是\chaptername
。\@chapapp
在\chaptername
主要部分和\appendixname
附录中。所以
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}% should not been needed with LaTeX >= 2018/04/01
\usepackage{fancyhdr}%Add chapter name at the beginning of each page
%\pagestyle{fancy}
\setlength{\headheight}{15pt}% ...at least 51.60004pt
% Title Page
\usepackage[titletoc,title]{appendix}
\title{}
\author{}
\begin{document}
\maketitle
\begin{abstract}
\end{abstract}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancyhead[R]{}
\makeatletter% Needed because of @ in macro name → https://tex.stackexchange.com/q/8351/277964
\fancyhead[L]{\@chapapp\ \thechapter\ --\ \leftmark}
\makeatother% because of \makeatletter above
\chapter{First chapter about happiness}
First page about the argument
\newpage
Second page about the argument. I want that this page has the chapter name at the top of the page.
\chapter{Hello}
\begin{appendices}
\chapter{Hi}
Appendix about something
\newpage
I do not want that this page has the chapter name at the top of the page.
\clearpage% Dont forget this!
\end{appendices}
\end{document}
结果是:
这与主体部分的标题一致。若要额外省略Appendix
头部的,可以使用:
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}% should not been needed with LaTeX >= 2018/04/01
\usepackage{fancyhdr}%Add chapter name at the beginning of each page
%\pagestyle{fancy}
\setlength{\headheight}{15pt}% ...at least 51.60004pt
% Title Page
\usepackage[titletoc,title]{appendix}
\title{}
\author{}
\begin{document}
\maketitle
\begin{abstract}
\end{abstract}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancyhead[R]{}
\fancyhead[L]{\chaptername \thechapter\ --\ \leftmark}
\chapter{First chapter about happiness}
First page about the argument
\newpage
Second page about the argument. I want that this page has the chapter name at the top of the page.
\chapter{Hello}
\begin{appendices}
\fancyhead[L]{\thechapter\ --\ \leftmark}% Change the header for this appendix
\chapter{Hi}
Appendix about something
\newpage
I do not want that this page has the chapter name at the top of the page.
\clearpage% Don't forget this, or the `\fancyhead` inside `appendices` will not change the last page of the appendix
\end{appendices}
\end{document}
但我不会这么做,因为Chapter
在正文中使用,Appendix
在附录中使用,不一致。
建议的变更
\the…
请注意,在标题定义中使用数字而不是使用\markboth
and添加它\markright
总是很危险的。它可能过早生效,过晚无效,从而导致错误的运行头。所以我建议将计数器移至 的定义\chaptermark
:
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}% should not been needed with LaTeX >= 2018/04/01
\usepackage{fancyhdr}%Add chapter name at the beginning of each page
%\pagestyle{fancy}
\setlength{\headheight}{15pt}% ...at least 51.60004pt
% Title Page
\usepackage[titletoc,title]{appendix}
\title{}
\author{}
\begin{document}
\maketitle
\begin{abstract}
\end{abstract}
\pagestyle{fancy}
\makeatletter% Needed because of @ in macro name → https://tex.stackexchange.com/q/8351/277964
\renewcommand{\chaptermark}[1]{%
\markboth{\@chapapp \thechapter\ --\ #1}{\@chapapp \thechapter\ -- \ #1}% chapter/appendix name and number moved from \fancyhead[L]{…}
}
\makeatother% because of \makeatletter before
\fancyhead[R]{}
\fancyhead[L]{\leftmark}% chapter/appendix name and number moved to \chaptermark
\chapter{First chapter about happiness}
First page about the argument
\newpage
Second page about the argument. I want that this page has the chapter name at the top of the page.
\chapter{Hello}
\begin{appendices}
\chapter{Hi}
Appendix about something
\newpage
I do not want that this page has the chapter name at the top of the page.
\end{appendices}
\end{document}
如您所见,这里不再需要\clearpage
。此外,通常目录、参考书目等未编号的标题也将自动不显示前缀和编号。但是,如果您不想A
在附录中使用前缀(而只想要 ),您仍然需要在 之后进行更改\begin{appendices}
:
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}% should not been needed with LaTeX >= 2018/04/01
\usepackage{fancyhdr}%Add chapter name at the beginning of each page
%\pagestyle{fancy}
\setlength{\headheight}{15pt}% ...at least 51.60004pt
% Title Page
\usepackage[titletoc,title]{appendix}
\title{}
\author{}
\begin{document}
\maketitle
\begin{abstract}
\end{abstract}
\pagestyle{fancy}
\makeatletter
\renewcommand{\chaptermark}[1]{%
\markboth{\@chapapp \thechapter\ --\ #1}{\@chapapp \thechapter\ -- \ #1}% chapter/appendix name and number moved
}
\makeatother
\fancyhead[R]{}
\fancyhead[L]{\leftmark}% chapter/appendix name and number moved
\chapter{First chapter about happiness}
First page about the argument
\newpage
Second page about the argument. I want that this page has the chapter name at the top of the page.
\chapter{Hello}
\begin{appendices}
\renewcommand{\chaptermark}[1]{% locally redefining the mark to remove the chapter/appendix name (instead of locally changing \fancyhead[L}{…})
\markboth{\thechapter\ --\ #1}{\thechapter\ -- \ #1}%
}
\chapter{Hi}
Appendix about something
\newpage
I do not want that this page has the chapter name at the top of the page.
\end{appendices}
\end{document}