Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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
} 
share|improve this answer

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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