This question is directly related to this one. I used the code and wanted to have an empty pagestyle on pages having a section start and at the same time the header should be customized like this one. When I use the codes together it won't work anymore, because the title in the header is now the title from the table of contents.
When a section starts there is no header:
When a section starts and has another [sub]section in it there should be a header but is not.
When there is no section at the page the header should have the rightmark if it's not blank, else the leftmark. That worked before merging the codes, now it doesn't (in this example it should be the current section):
Here are some cases for what should be inside the header and when there should be a header:
- If the page starts with a section and has no previous text that breaked to the page there shouldn't be a header.
- If there is text that breaks to the page from a [*]section there should be a header containing the number and title of [*].
- If the page doesn't has breaking text and starts with a sub[*]section it should have a header and in the header the number and title of the upper section e.g. if the page starts a subsection it should hold the section title.
How can I get both working, no header at section start and a customized header on pages, which don't have a section start or have [sub]sections which break to this page?
Here is a MWE:
\documentclass[12pt,a4paper,ngerman]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\hypersetup{breaklinks=true,
pdfborder={0 0 0},
ngerman}
\usepackage{times}
\usepackage{microtype}
\usepackage[parfill]{parskip}
\usepackage[
a4paper,
left=4cm,
right=3cm,
top=3cm,
bottom=3cm,
%showframe,
headheight=15pt,
includeheadfoot
]{geometry}
\linespread{1.25}
\usepackage{blindtext}
\renewcommand{\sfdefault}{\rmdefault}
\usepackage{fancyhdr}
\usepackage{chngcntr}
\usepackage{xparse}
\fancyhf{}
\lhead{\nouppercase{\rightorleftmark}}
\cfoot{\thepage}
\fancypagestyle{section}{%
\fancyhf{}%
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
}
\counterwithin{figure}{section}
\counterwithin{table}{section}
% https://tex.stackexchange.com/a/187853/49121
\let\originalsection\section
\RenewDocumentCommand{\section}{som}{%
\IfBooleanTF{#1}
{\originalsection*{#3}}
{\IfNoValueTF{#2}
{\originalsection{#3}}
{\originalsection[#2]{#3}}%
}%
\thispagestyle{section}%
}
%https://tex.stackexchange.com/a/101926/49121
\makeatletter
\newcommand{\rightorleftmark}{%
\begingroup\protected@edef\x{\rightmark}%
\ifx\x\@empty
\endgroup\leftmark
\else
\endgroup\rightmark
\fi}
\makeatother
\begin{document}
\pagestyle{plain}
\pagenumbering{Roman}
\setcounter{page}{1} % when using custom titlepage
\cleardoublepage
\phantomsection
\pdfbookmark{Inhaltsverzeichnis}{tableofcontents}
\markboth{Inhaltsverzeichnis}{}
\tableofcontents
\cleardoublepage
\phantomsection
\pdfbookmark{Abbildungsverzeichnis}{listoffigures}
\listoffigures
\cleardoublepage
\phantomsection
\pdfbookmark{Tabellenverzeichnis}{listoftables}
\listoftables
\cleardoublepage
\phantomsection
\pagenumbering{arabic}
\pagestyle{fancy}
\cleardoublepage
\blinddocument
\end{document}
\rightmark
, obviously the last set one (that is, the TOC title) is used.\renewcommand{\subsectionmark}[1]{\markright{#1}}
(or variant thereof if you need the number) should do.