# Skip double marks on chapter pages

Here is a little example to illustrate my problem:

\documentclass[fontsize=12pt, paper=a4]{scrreprt}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage{scrpage2}

\clearscrheadfoot
\pagestyle{scrheadings}
\ihead{\leftmark}
\ohead{\rightmark}
\cfoot{\pagemark}

\automark[subsection]{chapter}

\begin{document}

\chapter{Test}
\Blindtext

\section{Sec}
\Blindtext

\subsection{SubSec}
\Blindtext

\chapter{Test2}
\Blindtext

\section{Sec2}
\Blindtext

\subsection{SubSec2}
\Blindtext
\end{document}


As you can see I would like to have chapter and subsection in the heading of each page. But on pages 2 and 6, where no subsection is available, the chapter appears twice in the head. On pages 3, 4, 7 and 8 everything is OK.

I would like to suppress the \rightmark for the subsection, when there is no subsection available. How can I do this?

-

## 2 Answers

You can make the right hand heading text conditional on it not being the same as the left:

\ohead{%
\expandafter\def\expandafter\tmpa\expandafter{\romannumeral\Q\leftmark}%
\expandafter\def\expandafter\tmpb\expandafter{\romannumeral\Q\rightmark}%
\ifx\tmpa\tmpb\else\rightmark\fi
}

-

I think it makes more sense to have the section in the header instead of nothing without having a subsection. The following example uses the first section or subsection element on the page for the header. It uses an additional marks register from e-TeX.

\documentclass[fontsize=12pt, paper=a4]{scrreprt}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage{scrpage2}
\usepackage{etex}

\clearscrheadfoot
\pagestyle{scrheadings}
\ihead{\leftmark}
% \ohead{\rightmark}
\iftrue % oneside
\ohead{\firstmarks\subsectionmarks}
\else % twoside
\lohead{\firstmarks\subsectionmarks}
\rohead{\botmarks\subsectionmarks}
\fi
\cfoot{\pagemark}

\automark[subsection]{chapter}

\makeatletter
\newmarks\subsectionmarks
\newcommand*{\marksubsec}[1]{%
\begingroup
\protected@edef\@temp{%
\marks\subsectionmarks{#1}%
}%
\@temp
\endgroup
}
\@ifdefinable\org@chaptermark{%
\let\org@chaptermark\chaptermark
\renewcommand*{\chaptermark}{%
\marks\subsectionmarks{}
\org@chaptermark
}%
}
% Redefining \sectionmark
\@ifdefinable{\org@sectionmark}{%
\let\org@sectionmark\sectionmark
\iftrue
\renewcommand*{\sectionmark}[1]{%
\marksubsec{%
\ifnum\value{secnumdepth}>0 %
\if@mainmatter
\sectionmarkformat
\fi
\fi
#1%
}%
\org@sectionmark{#1}%
}
\else
\renewcommand*{\sectionmark}[1]{%
\marksubsec{}%
\org@sectionmark{#1}%
}%
\fi
}
\@ifdefinable\org@subsectionmark{%
\let\org@subsectionmark\subsectionmark
\renewcommand*{\subsectionmark}[1]{%
\marksubsec{%
\ifnum\value{secnumdepth}>1 %
\if@mainmatter
\subsectionmarkformat
\fi
\fi
#1%
}%
\org@subsectionmark{#1}%
}%
}
\makeatother

\begin{document}

\chapter{Test}
\Blindtext

\section{Sec}
\Blindtext

\subsection{SubSec}
\Blindtext

\chapter{Test2}
\Blindtext

\section{Sec2}
\Blindtext

\subsection{SubSec2}
\Blindtext
\end{document}

• Section headers can be omitted by changing \iftrue to \iffalse after the comment % Redefining \subsectionmark.

• The example assumes oneside setting. For twoside the subsection(/section) header can refer to the first and last subsection(/section) on the page, then change \iftrue to \iffalse for defining the outer header.

-
Thanks Heiko. Generally you are right. But in my case I have short chapters and sections and many subsections with text, because it is an instruction book. So for me the solution from David is enough. –  Dirk Oct 12 '12 at 10:54
Yes +1 this occurred to me while driving home that not doing the wrong mark would probably be better than fixing it up afterwards:-) –  David Carlisle Oct 12 '12 at 11:00