1

I am currently using a header on my document that should do one of a few things.

If the current page is before any chapters or sections are defined, the header should only contain a number.

If there are any sections or chapters defined before the current page, the name of the last defined section / chapter should be in the header.

There are two excpetions to this rule. If there is a new chapter, there should be no header on the page or table of contents.

If there is a title page, there should be no footer / header.

This works great. However, when a table of contents is added, all the headers become prefixed with CONTENTS. How can I prevent the headers being prefixed with CONTENTS without affecting any of the header functionality that was described above.

There is also a case when there is no section or title in the header, but there is a table of contents, the header is consists of CONTENTSCONTENTS.

If it makes a difference, I am building using LuaTex on the latest version of TexLive (downloaded today)

\documentclass[a4paper,oneside]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}

%Header / Footer info
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

%Define the header / footer
\fancyhf{}
\fancyhead[L]{\topmark}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
\begin{titlepage}
hello
\end{titlepage}
\tableofcontents
\clearpage
\lipsum[1-10]
\clearpage
\section{Paragraphs 1 - 10}
\lipsum[1-10]
\clearpage
\chapter{Lorem Ipsum :)}
\lipsum[1-5]
\section{Paragraphs 6 - 10}
\lipsum[6-10]
\section{Paragraphs 11 - 15}
\lipsum[11-15]
\end{document}
  • \topmark doesn't work correctly in LaTeX. Afaik, the only way to get correct heads with top marks is with (my) titleps (see sec. 5 of its manual). – Javier Bezos Sep 2 '18 at 16:12
  • @JavierBezos Hey, I just saw your comment. I actually tried to use your titleps (I really like how I can use it by the way. I prefer the formatting to fancyhdr's) but I still had the issue of contents being included. At the time I was using similar code to the code found in this answer – Dan Sep 2 '18 at 16:36
1

I'm afraid I am too new to LaTeX to know if this is the correct way, it certainly seems a bit to "hacky" to be correct, but it works for me.

The way I succeeded was to use two defined fancyhdr styles, as well as the plain style, and to keep switching the page style at different stages to produce the look I wanted.

At the start of the document, the plain page style was used. Then if there was any text but no chapters / sections defined, a separate fancyhdr was used that simply contained the current page number in the header.

Finally the fancyhdr style from the original code was used. However, the renrewcommand for the \chaptermark and \sectionmark was declared after the page style had been set to this style to force the CONTENTS not to be included in the header.

\documentclass[a4paper,oneside]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}

%Header / Footer info
\fancypagestyle{NormalFancy}{ %Style for when there is at least
                              %one previous chapter or section
    \fancyhf{}
    \fancyhead[L]{\topmark}
    \fancyhead[R]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{FancyNoSectionChapter}{ %Style for when there are no 
                                        %previous chapters or sections
    \fancyhf{}
    \fancyhead[R]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
}

\begin{document}
\begin{titlepage}
hello
\end{titlepage}
\pagestyle{plain} %Set this here to remove the header and footer
                  %at the start of the document

\tableofcontents
\clearpage

%Set the page style before any chapters or sections are defined
\pagestyle{FancyNoSectionChapter}
\lipsum[1-10]
\clearpage

%Set the page style to the normal style before the start of 
%the page with the first defined section / chapter
\thispagestyle{FancyNoSectionChapter}
\pagestyle{NormalFancy} %Make sure the page with the first defined 
                        %section / chapter still has the `FancyNoSectionChapter` style
\renewcommand{\chaptermark}[1]{\markright{#1}} %Redefine how the chapter
                                               %looks so there are no numbers
\renewcommand{\sectionmark}[1]{\markright{#1}} %Redefine how the section
                                               %looks so there are no numbers

\section{Paragraphs 1 - 10}
\lipsum[1-10]
\clearpage
\chapter{Lorem Ipsum :)}
\lipsum[1-5]
\section{Paragraphs 6 - 10}
\lipsum[6-10]
\section{Paragraphs 11 - 15}
\lipsum[11-15]
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.