[Tex/LaTex] Make headwidth = paperwidth, regardless of page geometry

header-footerscrlayer-scrpage

I would like the headsepline of each page to extend across the page. The problem I am encountering is when the geometry is changed via \newgeometry:

\documentclass{scrreprt}
\usepackage{geometry}
\usepackage[headsepline, headwidth=paper:\the\textwidth/2-\the\paperwidth/2:\the\textwidth/2-\the\paperwidth/2]{scrlayer-scrpage}    

\usepackage{lipsum}

\begin{document}
\lipsum
\newgeometry{textwidth=5cm}
\lipsum

\end{document}

On the pages with new geometry, the header does not extend across the page. How can I make scrlayer-scrpage extend the headsepline with an arbitrary page geometry?

Best Answer

With your code the header offset is calculated once depending on the current \textwidth. It is not recalculated if the \textwidth is changed.

If the headoffset should depend on flexible \textwidth you have to ensure that the headwidth including the offsets is recalculated on every page.

\documentclass{scrreprt}
\usepackage{geometry}
\usepackage[headsepline]{scrlayer-scrpage}
\KOMAoptions{onpsbackground={\KOMAoptions{headwidth=paper}}}

\usepackage{lipsum}

\begin{document}
\lipsum
\newgeometry{textwidth=5cm}\KOMAoptions{headwidth=paper}
\lipsum
\end{document}

enter image description here

Note, that headwidth=paper in oneside mode uses the offsets \the\textwidth/2-\the\paperwidth/2 as default.


But there are possibilities without recalculating the offsets on every page too.

If the header should have the same width as the paper set the horizontal offset of all header layers to 0pt:

\ForEachLayerOfPageStyle*{scrheadings}{%
  \ifstrstart{#1}{scrheadings.head}{%
    \ModifyLayer[hoffset=0pt,width=\paperwidth]{#1}%
  }{}%
}

Maybe you have to do the same for plain.scrheadings. Note that page style plain is only an alias for plain.scrheadings if scrlayer-scrpage is loaded.

enter image description here

Code:

\documentclass
  %[twoside]
  {scrreprt}
\usepackage{geometry}

\usepackage[headsepline,headwidth=page]{scrlayer-scrpage}

\ForEachLayerOfPageStyle*{scrheadings}{%
  \ifstrstart{#1}{scrheadings.head}{%
    \ModifyLayer[hoffset=0pt,width=\paperwidth]{#1}%
  }{}%
}
\ForEachLayerOfPageStyle*{plain.scrheadings}{%
  \ifstrstart{#1}{plain.scrheadings.head}{
    \ModifyLayer[hoffset=0pt,width=\paperwidth]{#1}%
  }{}%
}

\ihead{inner}\chead{center}\ohead{outer}

\usepackage{lipsum}
\begin{document}
\lipsum
\newgeometry{textwidth=5cm}
\lipsum
\end{document}

If only the line below of the header should be extended but not the header itself you have to modify only the scrheadings.head.below.line layer.

\documentclass
  %[twoside]
  {scrreprt}
\usepackage{geometry}

\usepackage[headsepline=:page]{scrlayer-scrpage}
\ModifyLayer[hoffset=0pt,width=\paperwidth]{scrheadings.head.below.line}%

\ihead{inner}\chead{center}\ohead{outer}

\usepackage{lipsum}
\begin{document}
\lipsum
\newgeometry{textwidth=5cm}
\lipsum
\end{document}

Result:

enter image description here