3

I want to produce a line below the headsepline that starts at the boundary of the page (not the margin of \textwidth) and ends at the last letter of the chapter/section title.

The closest I get after reading the documentation for KOMA Script is this:

\documentclass[
11pt,
a4paper,
twoside,
openright,
]{scrbook}

\usepackage{lipsum}

\usepackage[
  headsepline=:20cm,
  ilines,
]{scrlayer-scrpage}
\pagestyle{scrheadings}
\begin{document}
\chapter{Introduction}
\lipsum
\section{Motivation}
\lipsum
\end{document}

This yields a line that starts at the border and ends at the inner boundary for \textwidth. However, I want it to end at the innermost character, i.e. either the "n" of "1 Introduction" or the "1" of "1.1 Motivation".

Is this possible?

4
  • 1
    I get an error: ! LaTeX Error: Unknown option iline' for package scrlayer-scrpage'. with your code. Aug 12, 2015 at 21:41
  • 1
    Sorry, I lost an 's' there. Should work now
    – javex
    Aug 12, 2015 at 23:06
  • Thanks for fixing the code. What you ask is certainly possible. I have an answer but since I don't use KOMA classes I wouldn't know how to do it with scrlayer.scrpage. In chat I left my code in case you or some other KOMA expert wants to adapt it to work for you. Aug 12, 2015 at 23:08
  • I adapted my code to KOMA. Please see answer below. Aug 13, 2015 at 19:34

2 Answers 2

5

You could use \ifthispageodd to decide if the rule should go to the left or to the right and \makebox[0pt]... to hide the length of the \rule.

\documentclass{scrbook}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}

\ohead{%
  \Ifthispageodd{\makebox[0pt][l]{\rule[-5pt]{\paperwidth}{.4pt}}}{}%
  \headmark%
  \Ifthispageodd{}{\makebox[0pt][r]{\rule[-5pt]{\paperwidth}{.4pt}}}%
}

\begin{document}
\chapter{Introduction}
\lipsum
\section{Motivation}
\lipsum[1-10]
\section{A longer section name}
\lipsum
\end{document}

enter image description here

enter image description here

Note that 11pt, a4paper, openright, twoside are default for scrbook. Loading scrlayer-scrpage sets the pagestyle scrheadings automatically.

1
  • Nice trick! (some other text so the system allows me to post). Aug 14, 2015 at 2:47
3

Here's one way to achieve what you want (since internal calculations are involved you need to run the code at least twice for the rules to reach their final position):

\documentclass[
11pt,
a4paper,
twoside,
openright,
]{scrbook}
\usepackage{tikz}
\usetikzlibrary{babel}
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay] \coordinate (#1);}

\pagestyle{scrheadings}
\lehead{\leftmark}
\rohead{\rightmark}
\renewcommand*{\sectionmark}[1]{%
  \markright{\protect\tikzmark{starta}\thesection~#1%
  \protect\begin{tikzpicture}[remember picture,overlay]
    \protect\draw 
      ([yshift=-5pt]starta) -- 
      ([yshift=-5pt]current page.east|-{starta});
  \protect\end{tikzpicture}}%
}
\renewcommand*{\chaptermark}[1]{%
  \markboth{\thechapter~#1\protect\tikzmark{startb}%
  \protect\begin{tikzpicture}[remember picture,overlay]
    \protect\draw 
      ([yshift=-5pt]current page.west|-{startb}) --
      ([yshift=-5pt]startb);
  \protect\end{tikzpicture}}{}%
}

\begin{document}

\chapter{Introduction}
\lipsum
\section{Motivation}
\lipsum[1-10]
\section{A longer section name}
\lipsum

\end{document}

The result:

enter image description here

Some zoomed-in images of the headers; for an even numbered page:

enter image description here

and for an odd numbered page:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .