1

I am trying to create a twosided document with KomaScript. I want to add an extra something to every page where a new section begins. I started with creating a new pagestyle according to this post. I tried to modify the example given there. So far, I have done this:

\documentclass[12pt,a4paper,ngerman, twoside]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} 
\usepackage{lmodern} 
\usepackage[ngerman]{babel} 
\usepackage[left=3.8cm,right=2.2cm,top=3cm,bottom=3cm]{geometry}  
\usepackage{xcolor}

\usepackage[headsepline, automark]{scrlayer-scrpage}
\clearpairofpagestyles
\renewcommand*\pagemark{{\usekomafont{pagenumber}Seite\nobreakspace\thepage}}
\automark[section]{section}
\ihead*{\headmark}
\ohead*{\pagemark}
\chead{}
\cfoot[]{}
\ofoot[]{}
\ifoot[]{}

\newpairofpagestyles{highlights}{
    \chead{}
    \KOMAoptions{headsepline=false}
    \ohead*{\colorbox{blue!15}{%
            \makebox[\dimexpr\linewidth-2\fboxsep][l]{\color{white}%
                \hfill
                \pagemark}
        }
    }
}

\begin{document}
\section{Section 1}
Some text.
\cleardoublepage
\thispagestyle{highlights}
\section{Section 2}
Some text.
\end{document}

This results in this style of header:

Result so far

I would like to achieve three things:

  1. Create a custom pagestyle in the given MWE that I can activate on specific pages (where new sections begin) which contains the colorbar (which I specify in the following) and the pagenumber

  2. Create the colorbar with a color gradient behind thpage numberer in the header (it would be nice if the colorbar optionally could also extend out of the header region in the inner or outer direction, depending on taste)

  3. Change the textcolor of the pagemark to be white, so I have white text in front of the colorbar.

To visualize what I am after, I edited the important page of my MWE; this is what I desire to achieve: enter image description here

I already tried to create a new pagestyle in the following way, which also was suggested in the above link. I don't have enough experience with tikz though to make it work:

\usepackage{tikz}
\DeclareNewLayer[background,head,
contents={\hspace*{0cm}%
    \tikz{
        \fill[shade,left color=blue!0,right color=blue!20,right color=blue!70] (0,0)rectangle(20cm ,{1.4cm});
        \node[rotate=0,font=\bfseries] at({\textwidth-0.9cm},{0})
        {\ppagemark};
}}
]{highlights.bg}
\DeclareNewPageStyleByLayers{highlights}{highlights.bg}

Also, with this approach I don't even know how I would get the page number where I want.

Maybe someone of you has done this before or understands the matter deeply enough to be able to help me out.

1

Unfortunately, I don't really understand what the desired result is supposed to be. But I would suggest to define a new layer for the header background:

\DeclareNewLayer[
  background,
  head,
  hoffset=0pt,
  width=\paperwidth,
  mode=picture,
  contents={\putLL{\tikz{
    \ifodd \value{page}
      \fill[shade,left color=blue!0,right color=blue!70]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
    \else
      \fill[shade,left color=blue!70,right color=blue!0]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
    \fi
  }}
}
]{highlights.bg}

Then you can add this layer at the beginning of your new defined pagestyle:

\newpairofpagestyles{highlights}{
  \ohead{\pagemark}
  \KOMAoptions{headsepline=false}
  \addtokomafont{pagenumber}{\bfseries\color{white}}
}
\AddLayersAtBeginOfPageStyle{highlights}{highlights.bg}

Example:

\documentclass[12pt,ngerman, twoside]{scrartcl}

\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}% needed with older TeX distributions
\usepackage{lmodern} 
\usepackage{babel} 
\usepackage[left=3.8cm,right=2.2cm,top=3cm,bottom=3cm]{geometry}  
\usepackage{tikz}% loads xcolor, graphicx etc.

\usepackage[headsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\renewcommand*\pagemark{{\usekomafont{pagenumber}Seite\nobreakspace\thepage}}
\automark[section]{section}
\ihead{\headmark}
\ohead*{\pagemark}

\newpairofpagestyles{highlights}{
  \ohead{\pagemark}
  \KOMAoptions{headsepline=false}
  \addtokomafont{pagenumber}{\bfseries\color{white}}
}
\DeclareNewLayer[
  background,
  head,
  hoffset=0pt,
  width=\paperwidth,
  mode=picture,
  contents={\putLL{\tikz{
    \ifodd \value{page}
      \fill[shade,left color=blue!0,right color=blue!70]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
    \else
      \fill[shade,left color=blue!70,right color=blue!0]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
    \fi
  }}
}
]{highlights.bg}
\AddLayersAtBeginOfPageStyle{highlights}{highlights.bg}

\usepackage{blindtext}% only for dummy text
\begin{document}
\section{Section 1}
Some text.

\cleardoublepage
\thispagestyle{highlights}
\blinddocument
\end{document}

enter image description here

But if every section in your document should start on a new odd page, then you can switch to class scrreprt with option open=right. So here is an additional suggestion assuming that pagestyle highlights uses the same layers as pagestyle plain.scrheadings and the new layer highlight.bg:

\documentclass[12pt,ngerman, twoside,open=right]{scrreprt}
\RedeclareSectionCommand[beforeskip=0pt,afterindent=false]{chapter}

\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}% needed with older TeX distributions
\usepackage{lmodern} 
\usepackage{babel} 
\usepackage[left=3.8cm,right=2.2cm,top=3cm,bottom=3cm]{geometry}  
\usepackage{tikz}% loads xcolor, graphicx etc.

\usepackage[headsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\renewcommand*\pagemark{{\usekomafont{pagenumber}Seite\nobreakspace\thepage}}
\automark[chapter]{chapter}
\ihead{\headmark}
\ohead*{\pagemark}

\DeclareNewLayer[
  background,
  oddpage,% not on even pages in two-sided documents
  head,
  hoffset=0pt,
  width=\paperwidth,
  mode=picture,
  contents={\putLL{\tikz{
    \fill[shade,left color=blue!0,right color=blue!70]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
  }}
}
]{highlights.bg}
\DeclareNewPageStyleByLayers{highlights}{highlights.bg}
\ForEachLayerOfPageStyle{plain.scrheadings}{\AddLayersToPageStyle{highlights}{#1}}% <- add all plain.scrheadings layers to the new page style
\AddToLayerPageStyleOptions{highlights}{oninit=\addtokomafont{pagenumber}{\bfseries\color{white}}\KOMAoptions{headsepline=false}}

\renewcommand\chapterpagestyle{highlights}

\usepackage{blindtext}% only for dummy text
\begin{document}
\chapter{Chapter 1}
Some text.
\blinddocument
\end{document}

enter image description here


Update (regarding a comment below)

You can use options addheight, addhoffset, addvoffset, addwidth, height, hoffset, voffset and width to adjust the position and the size of the new defined layer with the background rule.

If you want to enlarge the header background rule vertically, you can use options addvoffset and addheight in the layer definition.

Example for the second MWE above:

\DeclareNewLayer[
  background,
  oddpage,% not on even pages in two-sided documents
  head,
  addvoffset=2mm,% moves the layer 2mm down
  addheight=4mm,% enlarges the height of the layer by 4mm
  hoffset=0pt,
  width=\paperwidth,
  mode=picture,
  contents={\putLL{\tikz{
    \fill[shade,left color=blue!0,right color=blue!70]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
  }}
}
]{highlights.bg}

enter image description here

If the background rule should reach from the inner text to the outer paper border, do not change hoffset and adjust the width:

\DeclareNewLayer[
  background,
  oddpage,% not on even pages in two-sided documents
  head,
  addvoffset=2mm,% moves the layer 2mm down
  addheight=4mm,% enlarges the height of the layer by 4mm
  %hoffset=0pt,% <- removed
  width=\paperwidth-1in-\hoffset-\oddsidemargin,% <- changed
  mode=picture,
  contents={\putLL{\tikz{
    \fill[shade,left color=blue!0,right color=blue!70]
      (0,0)rectangle(\the\layerwidth,\the\layerheight);
  }}
}
]{highlights.bg}

enter image description here

Note, that you need a second layer if the background should be used in a two-sided document on even pages too.

4
  • This is pretty much spot on what I tried to achieve. Kudos! Thank you for taking the extra step explaining the even better second solution. One small followup question: what would I have to change in the tikz definition of the rectangle to e.g. make it a little bid larger in the vertical direction (so that it is ranging further above and below the page number), so that it becomes even a little more noticeable? I don't really understand yet which part of the command defines from where to where it ranges and how wide and tall the rectangle is.
    – Pidrittel
    Sep 2 '20 at 9:33
  • (or, for example, what would I do if I wanted the bar to reach from the outside of the document to the inner side of the text (so not completely to the other side od the page)?)
    – Pidrittel
    Sep 2 '20 at 10:38
  • @Pidrittel I have updated my answer.
    – esdd
    Sep 2 '20 at 15:13
  • Ah, I see, you just create the layer with corresponding bounds and then fill it completely. Thank you very much for explaining! Now I can alter the rest on my own.
    – Pidrittel
    Sep 2 '20 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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