Hiding headline when page contains \section

I use the {scrlayer-scrpage} package for headlines and footlines and want to hide the headline and headsepline when my page contains \section.

I found a similar question here: Suppress headers when page contains a section title using titlesec but I have the problem that I am not able to activate the header afterwards.

Here is a minimal example:

\documentclass[a4paper, twoside=false, DIV=12, BCOR=5mm, bibliography=totocnumbered, numbers=autoendperiod]{scrartcl}

% Set up headers and footers
\usepackage{xpatch}
\footheight=1cm
\clearpairofpagestyles
\ofoot{\pagemark}

% Page Style for page which shall contain the page number only
\newpairofpagestyles{pagenumbersonly}{
\ofoot{\pagemark}
}

% Fixing pagestyle for pages containing \section command


I handle the content of the headline by using \ihead{\ifstr{\rightmark}{\leftmark}{}{\leftmark}}, that works fine for all styles. Otherwise I would have a headline printing the section twice. But how do I get the the headsepline activated for the following pages? I tried to add \KOMAoptions{headsepline=true} after applying the pagestyle in xpretocmd, but the headsepline will not be displayed.

An example for that:

\section{sec}
\subsection{subsec}
\lipsum{}\lipsum{}


produces the result:

EDIT: As requested, the full example:

%% Based on a TeXnicCenter-Template by Tino Weinkauf.

\documentclass[a4paper, twoside=false, DIV=12, BCOR=5mm, bibliography=totocnumbered, numbers=autoendperiod]{scrartcl}

% Options:
% Paper Size: a4paper / a5paper / b5paper / letterpaper / legalpaper / executivepaper
% Duplex: oneside / twoside
% Base Font Size: 10pt / 11pt / 12pt
% DIV 12 normed content- and margin size
% BCOR Bindekorrektur - 5mm more space on the left

\usepackage[ngerman]{babel}                             % German language support
\usepackage[utf8]{inputenc}                             % UTF-8 encoding
\usepackage[T1]{fontenc}                                    % Westeuropean encoding support
\usepackage{lmodern}                                            % Font-fix
\addtokomafont{disposition}{\rmfamily}      % Fix for chapter font
\usepackage{microtype}                                      % Better letters
\usepackage{siunitx}                                            % For units

% Images can be included using \includegraphics{Dateiname}
%
% The mode "LaTeX => PDF" allows the following formats:
%   .jpg  .png  .pdf  .mps
%
% The modes "LaTeX => DVI", "LaTeX => PS" und "LaTeX => PS => PDF"
% allow the following formats:
%   .eps  .ps  .bmp  .pict  .pntg

\usepackage{listings, lstautogobble}            % For formatting Code, for formatting Indents
\usepackage{lscape}                                             % Single page landscape layout support
\usepackage{pdflscape}                                      % Single page landscape layout support for PDF files
\usepackage{pdfpages}                                           % Embedding PDF documents in the final document

% Set up headers and footers
\usepackage{xpatch}
\footheight=1cm
\clearpairofpagestyles
\ofoot{\pagemark}

% Page Style for page which shall contain the page number only
\newpairofpagestyles{pagenumbersonly}{
\ofoot{\pagemark}
}

% Fixing pagestyle for pages containing \section command

% Automark fills the \leftmark and \rightmark placeholders with the section and subsection
% Autooneside is needed by automark
% Footsepline adds a line over the footline

\usepackage{tikz-er2}                                           % TIKZ for figures

\usepackage{lipsum}                                             % For testing purposes. Remove if finished

% Math Packages
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{textcomp}

% Line Spacing
\usepackage{setspace}
%\singlespacing        %% 1-spacing (default)
%\onehalfspacing       %% 1,5-spacing
%\doublespacing        %% 2-spacing

% Other Packages

%\usepackage{soul}
%\usepackage[multiple]{footmisc}

% Babelbib for German citation style
\usepackage[fixlanguage]{babelbib}
\selectbiblanguage{ngerman}

% Abb. instead of Abbildung as Caption

% Bracket on the right

%\newenvironment{rcases}
%  {\left.\begin{aligned}}
%  {\end{aligned}\right\rbrace}

\usepackage{hyperref}
\hypersetup{
pdftitle={hidden},
pdfauthor={hidden},
pdfkeywords={hiddén},
bookmarksopen=true
}

\makeatletter
\renewcommand\section{
~\vspace{1cm}
\@startsection
{section}{1}{0mm}%         % name, ebene, einzug
{\baselineskip}%            % vor-abstand
{2\baselineskip}%            % nach-abstand
{\bfseries\sffamily\LARGE}%      % layout
}
\makeatother

\makeatletter
\renewcommand\subsection{\@startsection
{subsection}{2}{0mm}%      % name, ebene, einzug
{2\baselineskip}%            % vor-abstand
{1.33\baselineskip}%            % nach-abstand
{\bfseries\sffamily\large}%           % layout
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DOCUMENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

%% Title Page %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% TOC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\pagestyle{pagenumbersonly}

\tableofcontents
\listoffigures
\listoftables

\newpage

%Hauptinhalt

\section{sec}
\subsection{subsec}
\lipsum{}\lipsum{}\lipsum{}\lipsum{}

%\input{Content}
%\newpage

\newpage

%% Literature %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\interlinepenalty 10000             % Avoid a pagebreak in a source
%\bibliography{literature}

\newpage

%% Disclaimer %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\appendix

\end{document}


The headlinecontent is correct at both pages. But how do I enable the headsepline for the following pages again (which do not contain \section)?

EDIT 2: I found out it works perfectly as expected when I remove the lines

\makeatletter
\renewcommand\section{
~\vspace{1cm}
\@startsection
{section}{1}{0mm}%         % name, ebene, einzug
{\baselineskip}%            % vor-abstand
{2\baselineskip}%            % nach-abstand
{\bfseries\sffamily\LARGE}%      % layout
}
\makeatother

\makeatletter
\renewcommand\subsection{\@startsection
{subsection}{2}{0mm}%      % name, ebene, einzug
{2\baselineskip}%            % vor-abstand
{1.33\baselineskip}%            % nach-abstand
{\bfseries\sffamily\large}%           % layout
}
\makeatother


Can someone explain me why this works now and what I can do instead of deleting those lines?

• Please provide an example we can copy-paste-compile rather than just fragments. It makes it much easier to help as we don't have to play a guessing game concerning the bits we need to add to reproduce the problem. Setting head and foot height like that manually is not a good idea if you are using typearea to layout your page (which you are), since typearea will not take account of changes you don't let it know about. – cfr Oct 22 '15 at 22:27
• Why do you set the headsepline for the number-only pages to true? Don't you want it to be false? If not, there isn't much point in setting the page style up with false in the first place. – cfr Oct 22 '15 at 22:29
• @cfr thanks for your hint, I added my full example and reduced the content to a minimum. The error still appears there. I tried to apply the headsepline-style to the number-only pages with: \newpairofpagestyles{pagenumbersonly}\KOMAoptions{headsepline=false} [...] } and set the headsepline to true afterwards in xpretocmd for the following pages. But this didn't work. – Drudge Oct 22 '15 at 22:37
• And can you tell me where I use typearea exactly? I cannot find it in my document. Or is it a part of a package? – Drudge Oct 22 '15 at 22:39
• @cfr I might found a solution and edited a post. Can you give me a hint what I can do instad of deleting those lines? – Drudge Oct 22 '15 at 23:16

Do not redefine \section and \subsection. You can use \RedeclareSectionCommand to change beforeskip and afterskip

\RedeclareSectionCommand[
beforeskip=-1\baselineskip,
afterskip=2\baselineskip,
font=\sffamily\LARGE
]{section}

\RedeclareSectionCommand[
beforeskip=-2\baselineskip,
afterskip=1.33\baselineskip,
font=\sffamily\large
]{subsection}


This code have to be inserted before prepending code to \section.

To remove the headsepline only on pages with pagenumbersonly style you can use

\newpairofpagestyles{pagenumbersonly}{\ofoot{\pagemark}}


MWE:

\documentclass[DIV=12, BCOR=5mm, bibliography=totocnumbered,
footheight=1cm
]{scrartcl}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}

\RedeclareSectionCommand[
beforeskip=-1\baselineskip,
afterskip=2\baselineskip,
font=\sffamily\LARGE
]{section}

\RedeclareSectionCommand[
beforeskip=-2\baselineskip,
afterskip=1.33\baselineskip,
font=\sffamily\large
]{subsection}

% Set up headers and footers
\usepackage{xpatch}
\clearpairofpagestyles
\ofoot{\pagemark}

% Page Style for page which shall contain the page number only
\newpairofpagestyles{pagenumbersonly}{\ofoot{\pagemark}}

% Fixing pagestyle for pages containing \section command
\xpretocmd{\section}{\vspace*{1cm}\thispagestyle{pagenumbersonly}}{}{}

\usepackage{lipsum}                                             % For testing

\renewcaptionname{ngerman}{\refname}{Literaturverzeichnis}
\renewcaptionname{ngerman}{\figurename}{Abb.}

\begin{document}

\pagestyle{pagenumbersonly}
\tableofcontents
\listoffigures
\listoftables

\clearpage
\section{sec}
\subsection{subsec}
\lipsum[1-6]
\lipsum[1-7]
%\lipsum[1-5]% uncomment to see the problem
\section{sec}
\subsection{subsec}
\lipsum[1-10]
\end{document}


I have removed the stuff that is not needed for the question and I have used

\ihead{\leftmark}


to ensure that only \leftmark is printed if \leftmark and \rightmark are equal.

Another suggestion is using page style plain instead defining the new one.

\documentclass[DIV=12, BCOR=5mm, bibliography=totocnumbered,
footheight=1cm
]{scrartcl}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}

\RedeclareSectionCommand[
beforeskip=-1\baselineskip,
afterskip=2\baselineskip,
font=\sffamily\LARGE
]{section}

\RedeclareSectionCommand[
beforeskip=-2\baselineskip,
afterskip=1.33\baselineskip,
font=\sffamily\large
]{subsection}

% Set up headers and footers
plainfootsepline
]{scrlayer-scrpage}
\usepackage{xpatch}
\clearpairofpagestyles
\ofoot*{\pagemark}

% Fixing pagestyle for pages containing \section command
\xpretocmd{\section}{\vspace*{1cm}\thispagestyle{plain}}{}{}

\usepackage{lipsum}

\renewcaptionname{ngerman}{\refname}{Literaturverzeichnis}
\renewcaptionname{ngerman}{\figurename}{Abb.}

\begin{document}

\pagestyle{plain}
\tableofcontents
\listoffigures
\listoftables

\clearpage
\section{sec}
\subsection{subsec}
\lipsum[1-6]
\lipsum[1-7]
\lipsum[1-5]
\section{sec}
\subsection{subsec}
\lipsum[1-10]
\end{document}


Note that there will be a problem if an automatical page break is done when the section starts. Then the pagenumbersonly style is set on the page before. Uncomment the \lipsum[1-5] command to see this problem.

Update

Maybe you want to use the scrreprt class and \chapters:

\documentclass[DIV=12, BCOR=5mm, bibliography=totocnumbered,
footheight=1cm
]{scrreprt}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}

\RedeclareSectionCommand[
beforeskip=\dimexpr-1cm-1\baselineskip\relax,
afterskip=2\baselineskip,
font=\sffamily\LARGE
]{chapter}

\RedeclareSectionCommand[
beforeskip=-2\baselineskip,
afterskip=1.33\baselineskip,
font=\sffamily\large
]{section}

% Set up headers and footers
plainfootsepline
]{scrlayer-scrpage}
\clearpairofpagestyles
\ofoot*{\pagemark}

\usepackage{lipsum}                                             % For testing

\renewcaptionname{ngerman}{\refname}{Literaturverzeichnis}
\renewcaptionname{ngerman}{\figurename}{Abb.}

\begin{document}

\begingroup
\renewcommand\clearpage\relax
\pagestyle{plain}
\tableofcontents
\listoffigures
\listoftables
\endgroup

\clearpage
\chapter{sec}
\section{subsec}
\lipsum[1-6]
\lipsum[1-6]
%\lipsum[1-5]% uncomment to see the problem
\chapter{sec}
\section{subsec}
\lipsum[1-10]
\end{document}

• Thanks, your code is a lot of cleaner than mine. It was intended not even having a \leftmark when there is a \section on the page, so I removed this. Unfortunately your beforeskip in the RedeclareSectionCommand does not work every time. I think the beforeskip is relative to preceding text. That causes a space when there was text before but when there is a \newpage\section, the beforeskip does not work. There is no additionally skip of 1cm relative to the headline as I archived it with my vspace{1cm} before. It should look like the image in my question. Is there a way to fix that? – Drudge Oct 23 '15 at 1:19
• See my edited answer. You prepend \vspace*{1cm} to the \section command and use beforeskip=-1\baselineskip. But maybe you want to use class like scrreprt that uses \chapters? – esdd Oct 23 '15 at 7:39
• So I write \vspace*{1cm}\section everytime in my document when I create a new \section? I think I can life with that. The only question left is why does the afterskip option has a positive value while the beforeskip option has a negative value? Could you explain that, please? – Drudge Oct 23 '15 at 11:42
• Ignore my last comment, I haven't seen the vspace in the xpretocmd in your edit. Now I'm satisfied :) – Drudge Oct 23 '15 at 16:50

I found a solution for my own problem. The problem is that the line:

\xpretocmd{\section}{\thispagestyle{pagenumbersonly} \KOMAoptions{headsepline=true}}{}{}


interferes with the lines

\makeatletter
\renewcommand\section{
~\vspace{1cm}
\@startsection
{section}{1}{0mm}%         % name, ebene, einzug
{\baselineskip}%            % vor-abstand
{2\baselineskip}%            % nach-abstand
{\bfseries\sffamily\LARGE}%      % layout
}
\makeatother

\makeatletter
\renewcommand\subsection{\@startsection
{subsection}{2}{0mm}%      % name, ebene, einzug
{2\baselineskip}%            % vor-abstand
{1.33\baselineskip}%            % nach-abstand
{\bfseries\sffamily\large}%           % layout
}
\makeatother


I have to use \xpretocmd after the renewcommands. Then everything works as expected. I would be glad if someone could explain me this behaviour.

• You see why a complete example is needed? Your original question did not mention the fact that you overwrote the commands after appending to them! – cfr Oct 22 '15 at 23:36
• Yes, sorry for that. I didn't thought that is relevant since the pagestyle was applied correctly to \section as it does not. But now the \KOMAoptions{headsepline=true} is interpreted, too, which I cannot explain to me. – Drudge Oct 22 '15 at 23:38
• That's the point of making a minimal working example. Then you figure out what is required. \renewcommand obliterates the existing definition of a command. So any changes you make to a command before that are obliterated. – cfr Oct 22 '15 at 23:51
• The page style pagenumbersonly was not set by the \section command. With your code there was no entry in the header because of \ihead{\ifstr{\rightmark}{\leftmark}{}{\leftmark}}\ohead{\ifstr{\rightmark}{\leftmark}{}{\rightmark}}. So if \rightmark is equal to \leftmark there will be an empty \ihead and an empty \ohead. Insert some text in the empty arguments then this text would be printed in the header of the page with your first section: \ihead{\ifstr{\rightmark}{\leftmark}{text}{\leftmark}} \ohead{\ifstr{\rightmark}{\leftmark}{text}{\rightmark}}. – esdd Oct 22 '15 at 23:56