[Tex/LaTex] Applying a different format at every first page of a chapter using Scrlayer-Scrpage

fancyhdrkoma-scriptscrlayer-scrpage

I decided to learn Scrlayer-Scrpage since I'm using a KOMA-script, but I couldn't replicate the result I had achieved using Fancyhdr.

Here's the one with Fancyhdr:

\documentclass[oneside, openany]{scrbook}
\usepackage{lipsum}

\usepackage{fancyhdr}
\fancypagestyle{my_style}{    
    \fancyhf{}
    \fancyhead[RO]{\leftmark}
    \fancyhead[LE]{\rightmark}
    \fancyhead[LO,RE]{\thepage}
}    
\pagestyle{my_style}
\fancypagestyle{plain}{
    \fancyhf{}
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0.4pt}
}

\usepackage{etoc}           
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

\begin{document}
\title{title}
\maketitle

\tableofcontents
\thispagestyle{empty}
\addtocontents{toc}{\protect\thispagestyle{empty}} 

\addchap{Copyright}
\pagenumbering{roman}

\chapter{Chapter}
\pagenumbering{arabic}

\section{hello}
\lipsum[1-20]
\end{document}

It has achieved the followings:

  1. Using etoc, ToC is not numbered or lined even if it exceeds 1 page.
  2. Except the first pages of a chapter, a line is drawn above the contents.

This is my attempt using Scrlayer-Scrpage:

\documentclass[oneside, openany, headsepline]{scrbook}
\usepackage{lipsum}

\usepackage[]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{\pagemark}
\ohead{\headmark}

\usepackage{etoc}           
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

\begin{document}
\title{title}
\maketitle

\tableofcontents
\thispagestyle{empty}
\addtocontents{toc}{\protect\thispagestyle{empty}} 

\addchap{Copyright}
\pagenumbering{roman}

\chapter{Chapter}
\pagenumbering{arabic}

\section{hello}
\lipsum[1-20]
\end{document}

It does have succeeded in not numbering the ToC, but I couldn't find a way to distinguish the first pages of a chapter. The manual of Scrlayer-Scrpage sometimes lists several options without even telling where to put them, and I couldn't find any solution.

Beside the question above, I also want to know how to change the headmarks in both packages. Fancyhdr prints it as Chapter 1. and Scrlayer-Scrpage just as 1.

Best Answer

The page style of first page of a chapter of most book classes, distinguishes from the page style of the other pages. Standard class book uses hard coded \thispagestyle{plain} for the first page of a \chapter. KOMA-Script class scrbook uses \thispagestyle{\chapterpagestyle} and the default of \chapterpagestyle is plain too.

And if you have a look at the result of your scrlayer-scrpage example:

two pages with the example from the question

there are several differences between the first page of the chapter (left side) and other pages (right side):

  • no head separation line/rule on first page vs. head separation line/rule in other pages
  • no running head on first page vs. running head on other pages
  • no page number on first page vs. page number on other pages

This first is, because option headsepline does not set a head separation rule on page style plain. You could switch it on using plainheadsepline. But I think, you do not want it. So this is good

The second is, because you have removed the defaults for page style scrheadings and plain.scrheadings (which is the same like plain after the \pagestyle{scrheadings}, that is done while loading scrlayer-scrpage) using \clearpairofpagestyls and you've added \headmark only to scrheadings but not to plain.scrheadings. That is absolutely correct.

The third has almost the same reason as the second, but you've added \pagemark only to scrheadings. So if you like to have a page number on the first page of a chapter, you have to add \pagemark to plain.scrheadings. This could be done replacing \ihead by \ihead*. But this would add the page number to the head. I think, you want the page number at the middle of the foot. So you have to use

\cfoot[\pagemark]{}

Note, the optional argument of \cfoot etc. does change the corresponding element of the plain page style.

So with:

\documentclass[oneside, openany, headsepline]{scrbook}
\usepackage{lipsum}

\usepackage[]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{\pagemark}
\ohead{\headmark}

\usepackage{etoc}           
\renewcommand{\etocaftertitlehook}{\pagestyle{plain}}
\renewcommand{\etocaftertochook}{\thispagestyle{plain}}

\begin{document}
\title{title}
\maketitle

\tableofcontents
\thispagestyle{empty}
\addtocontents{toc}{\protect\thispagestyle{empty}} 

\addchap{Copyright}
\pagenumbering{roman}

\chapter{Chapter}
\pagenumbering{arabic}

\section{hello}
\lipsum[1-20]
\end{document}

You will get

two pages with page number on first chapter page

You could even remove using etoc and use \BeforeStartingTOC and \AfterStartingTOC to set the first page of the table of contents using page style empty but the rest of the table of contents with page style plain:

\documentclass[oneside, openany, headsepline]{scrbook}
\usepackage{lipsum}

\usepackage{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{\pagemark}
\ohead{\headmark}
\cfoot[\pagemark]{}

\BeforeStartingTOC{\pagestyle{plain}\thispagestyle{empty}}
\AfterStartingTOC{\clearpage}

\begin{document}
\title{title}
\maketitle

\tableofcontents

\addchap{Copyright}
\pagenumbering{roman}

\chapter{Chapter}
\pagenumbering{arabic}

\section{hello}
\lipsum[1-20]

\end{document}

BTW: Your fancyhdr definition distinguish from your description. It does also set a separation line on the plain pages. I would not recommend this, but you can do it:

\documentclass[oneside, openany, headsepline,footlines=1]{scrbook}
\usepackage{lipsum}

\usepackage[plainfootsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{\pagemark}
\ohead{\headmark}
\cfoot[\pagemark]{}

\BeforeStartingTOC{\pagestyle{plain}\thispagestyle{empty}}
\AfterStartingTOC{\clearpage}
\AddToLayerPageStyleOptions{plain.scrheadings}{% whenever page style plain.scrheadings is selected
  onselect={\KOMAoptions{footsepline}}% activate footsepline
}
\AddToLayerPageStyleOptions{scrheadings}{% whenever page style scrheadings is selected
  onselect={\KOMAoptions{footsepline=false}}% deactivate footsepline
}

\begin{document}
\title{title}
\maketitle

\tableofcontents

\addchap{Copyright}
\pagenumbering{roman}

\chapter{Chapter}
\pagenumbering{arabic}

\section{hello}
\lipsum[1-20]

\end{document}

enter image description here

Here I've used \AddToLayerPageStyleOptions to set KOMA-Script option footsepline depending on the selected page style

If you also want to have upper case running head see option markcase in the KOMA-Script manual. If you want the prefix CHAPTER see \chaptermarkformat. If you want to change the font, see elements pageheadfoot, pagehead, pagenumber and commands \setkomafont and \addtokomafont.