1

I am using fancyhdr package to add chapter name at the top of each page. The code of a simple LaTeX example:

\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}

As you can see, in this way it goes on naming on the top of the page "Chapter last number of the chapters name of the appendix" while instead I would like the chapter names at the top of the pages only for chapters of the report and so not also for appendix pages or bibliography. How can I do it ?

enter image description here

  • The chapapp in the heading of your last example is, because you've removed \makeatletter and \makeatother from my examples. – cabohah Feb 15 '23 at 17:54
  • Using a wrong number (+ prefix) in the heading of your last example is, because you've neither added \clearpage before \end{appendices} as I stated as minimum requirement, nor moved \@chapapp\ \thechapter from \fancyhead to \chaptermark as I've recommended in my answer. – cabohah Feb 15 '23 at 17:56
  • BTW: Please don't copy code from an answer to the question. Substantially changing an already answered question is mostly not a good idea. If you have problems integrating the code of an answer to your code, ask a new question. You can always ask a follow-up question as a new question. In this case adding a link to the original question or answer to a new question could be useful. – cabohah Feb 15 '23 at 18:34
  • @cabohah ok I removed that EDIT part. – HelpNeederStudent Feb 16 '23 at 08:26

1 Answers1

1

Minimum changes

First of all you should add a \clearpage before \end{appendices}, otherwise changes of appendices will end before the last page of the appendix finishes. After this you could additionally use \@chapapp instead of \chaptername. \@chapapp is \chaptername in the main part and \appendixname in the appendix. So

\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}

results in:

enter image description here

which is consistent with the headings in the main part. To additionally omit the Appendix in the head only, you can use:

\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}

enter image description here

But I won't do this, because it is inconsistent using Chapter in the main part but not Appendix in the appendix.

Recommended changes

Note, using \the… number in the header definition instead of adding it using \markboth and \markright is always dangerous. It can become valid to early and invalid to late and therefore result in wrong running heads. So I would recommend, to move the counter to the definition of \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}

As you can see, here you do not needed the \clearpage any longer. Moreover, usually not numbered headings of the table of contents, the bibliography etc. will automatically also not show the prefix and the number. However, if you do not want the prefix (but only the A) in the appendix, you would still need a change, after \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}

cabohah
  • 11,455
  • Thanks very much for your help, however making the modifications that you suggest (i.e. substituting \chaptername with @chapapp) it does not work as I wanted. I modified the question post to describe what I obtain. – HelpNeederStudent Feb 15 '23 at 16:47
  • @HelpNeederStudent I've added some additional comments to make more clear, that it is not a good idea to copy only one \@chapapp instead of coping all the absolutely required code of the minimum suggestion or to switch over to the recommended changes. – cabohah Feb 15 '23 at 18:12
  • Ok now copying your second snippet of code works. Thanks very much and I apologize for not having read all your detailed answer ! – HelpNeederStudent Feb 16 '23 at 08:25