3

I want to add one horizontal line above and one below the chapter-entry as well.


Before using the scrbook-class, I used this simple solution made by titlesec-package:

\newpagestyle{headrules}{
    \headrule

    % for twosided layout, use:
    \sethead[\thepage][\chaptertitle][]                 % even pages (left side of book)
    {\chaptertitle}{\sectiontitle}{\thepage}            % odd pages (right side of book)

    % for onesided layout, use:
    % \sethead{\thechapter. \chaptertitle}{}{\thepage}  % in this case, the document only has odd pages
}

Working minimum example:

\documentclass{book}

\usepackage[english]{babel}
\usepackage[automark]{scrpage2}
\usepackage[pagestyles]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}[display]{\fontfamily{pag}\Large\bfseries}{\titlerule[4pt]\chaptertitlename\ \thechapter}{5pt}{\large}[{\titlerule[2pt]}]
\titleformat{\section}{\fontfamily{pag}\normalsize\bfseries}{\thesection}{5pt}{\normalsize}
\titleformat{\subsection}{\fontfamily{pag}\normalsize\bfseries}{\thesubsection}{5pt}{\normalsize}
\titlespacing*{\chapter}{0pt}{30pt}{20pt}
\titlespacing*{\section}{0pt}{20pt}{10pt}
\titlespacing*{\subsection}{0pt}{20pt}{5pt}

\newpagestyle{headrules}{
    \headrule

    % for twosided layout, use:
    \sethead[\thepage][\chaptertitle][]                 % even pages (left side of book)
    {\chaptertitle}{\sectiontitle}{\thepage}            % odd pages (right side of book)

    % for onesided layout, use:
    % \sethead{\thechapter. \chaptertitle}{}{\thepage}  % in this case, the document only has odd pages
}

\setlength{\headheight}{1.1\baselineskip}               % adjusting the height of head

\pagestyle{headrules}                                   % use this style instead of \pagestyle{plain}

\begin{document}

\blinddocument

\end{document}

Screenshot:

enter image description here


So, the question now is: How can I solve this without using the titlesec-package anymore? Using titlesec together with a KOMA-class is not recommended, and I prefer to use the scrbook-class instead of book because of those many great advantages (for example addchap and others)...

Do I have to change the pagestyle plain-format for this?

Thank you very much for your help!


The following solution from clemens for separating \chapters and \addchaps seems to make sense, but it doesn't work in the following minimal-example (don't know why):

\documentclass[chapterprefix=true]{scrbook}

\renewcommand*\chapterheadstartvskip{%
    \ifnumbered{chapter}
    {\noindent\rule{\linewidth}{4pt}\par\vspace*{-4pt}}
    {}%
}
\renewcommand*\chapterheadendvskip{%
    \ifnumbered{chapter}
    {\vspace*{-4pt}\noindent\rule{\linewidth}{2pt}\par\vspace{\baselineskip}}
    {}%
}

\begin{document}

\chapter{Experimental chapter-entry}

\addchap{Experimental addchap-entry}

\end{document}

It doesn't matter if there's a numbered \chapter or an unnumbered \addchap - either the selected line is existent or absent in both selectors...

Is there any mistake in my line of thought?


Solution: Upgrading to KOMA-Script v3.18 brought the solution for me, too! Thank you very much clemens and all the others who were helping me!

Thanks a lot for your patient help!

9
  • It is a wellknown fact that KOMA classes and titlesec should not used together (And no, fancyhdr is no good idea with KOMA as well)
    – user31729
    Jul 11, 2015 at 11:27
  • Of course, this is a well known fact! But thanks a lot for your hint, I've changed the subject and explanatory text a bit...
    – Dave
    Jul 11, 2015 at 11:36
  • The scrpage2 package is outdated. Use scrlayer-scrpage instead.
    – user31729
    Jul 11, 2015 at 11:41
  • 3
    I would use \chapterheadstartvskip and \chapterheadendvskip to add the lines. (And it is imho a not so well known fact, that it is difficult to create fancy sectioning commands only with KOMA-tools. ) Jul 11, 2015 at 11:46
  • See komascript.de/node/486 Jul 11, 2015 at 13:07

2 Answers 2

5

Update

With KOMA-Script Version 3.19 or newer there is another possibility: you can redefine the new command \chapterlineswithprefixformat to insert the lines:

\newcommand\titlerule[1][1pt]{\rule{\textwidth}{#1}}
\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#2}{}{}{\titlerule[4pt]\par}%
  #2#3\titlerule[2pt]%
}

MWE:

\documentclass[
  chapterprefix=true
]{scrbook}[2015/09/29]% needs version 3.19 or newer

\RedeclareSectionCommand[
  beforeskip=0pt,
  afterskip=1\baselineskip
]{chapter}

\newcommand\titlerule[1][1pt]{\rule{\textwidth}{#1}}
\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#2}{}{}{\titlerule[4pt]\par\nobreak}%
  #2#3\par\nobreak\titlerule[2pt]%
}

\usepackage{blindtext}
\begin{document}
\chapter{Experimental chapter-entry}
\blindtext
\addchap{Experimental addchap-entry}
\blindtext
\chapter*{Experimental chapter*-entry}
\blindtext
\addchap*{Experimental addchap*-entry}
\blindtext
\end{document}

If this should also work with option chapterprefixline=false add the following lines to the preamble:

\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \@hangfrom{#2}{#3}\par\nobreak\titlerule%
}
\makeatother

Original answer

If the line above the chapter heading should only be inserted if the chapterprefix is written you could insert this line in \chapterformat. This works also for \chapter* and \addchap*.

\documentclass[chapterprefix=true]{scrbook}

\RedeclareSectionCommand[
  beforeskip=0pt,
  afterskip=1\baselineskip
]{chapter}
\usepackage{etoolbox}
\newcommand\titlerule[1][1pt]{\noindent\rule{\linewidth}{#1}\par}
\preto\chapterformat{\IfUsePrefixLine{\vspace{-\baselineskip}\titlerule[4pt]}{}}
\preto\chapterheadendvskip{\titlerule[2pt]}

\usepackage{blindtext}
\begin{document}
\chapter{Experimental chapter-entry}
\blindtext
\addchap{Experimental addchap-entry}
\blindtext
\chapter*{Experimental chapter*-entry}
\blindtext
\addchap*{Experimental addchap*-entry}
\blindtext
\end{document}

enter image description here

enter image description here

3

Despite their name the usual way with KOMA-Script is to redefine \chapterheadstartvskip and \chapterheadendvskip:

\documentclass[chapterprefix=true]{scrbook}

\renewcommand*\chapterheadstartvskip{\noindent\rule{\linewidth}{4pt}\par}
\renewcommand*\chapterheadendvskip{\noindent\rule{\linewidth}{2pt}\par\vspace{\baselineskip}}

\usepackage{blindtext}

\begin{document}

\blinddocument

\end{document}

enter image description here

With slightly altered definitions only number chapters will get the rules. Adding \vspace*s at the right place will move the lines closer to the text:

\renewcommand*\chapterheadstartvskip{%
  \ifnumbered{chapter}
    {\noindent\rule{\linewidth}{4pt}\par\vspace*{-4pt}}
    {}%
}
\renewcommand*\chapterheadendvskip{%
  \ifnumbered{chapter}
    {\vspace*{-4pt}\noindent\rule{\linewidth}{2pt}\par\vspace{\baselineskip}}
    {}%
}
8
  • Thank you very much for your advice! This works actually perfect! There are only two more things: 1.) Like in the advice from Andrew Swann, I can't adjust the space between the chapters name and the \rule. I've tried to set \rule[2mm]{\linewidth}, but this does only move the existing text below the line, but not the line itself... 2.) How can I set making the top \rule only to "real/original" chapter-headlines, but not to \addchap-headlines? The foreword, table of content, acronymslist e.g. only use \addchap-headlines, and they should not get the top line (for those only line below).
    – Dave
    Jul 11, 2015 at 14:08
  • @Dave Do you mean between the first rule and the text or between the text and the second rule? Anyway, some \vspace* in the right place should do. With \ifnumbered{chapter}{<true>}{<false>} within the redefinition you can add the rules only for numbered chapters if placing them in the <true> branch
    – cgnieder
    Jul 11, 2015 at 14:15
  • ... or should I poste a new question because of that: "How to select only "real" \chapter-headlines except \addchap-headlines simultaneously?"
    – Dave
    Jul 11, 2015 at 14:17
  • Thank you very much for your great support! The spaces are customized now! The \vspace*-command has been at the wrong place during my experiment indeed, it has to be placed before \noindent and everything works fine now! Furthermore I've tried to set \ifnumbered{chapter}{\renewcommand*\chapterheadstartvskip{\noindent\rule{\linewidth}{4pt}\par}}{}, but this doesn't work. Is there any mistake?
    – Dave
    Jul 11, 2015 at 14:35
  • @Dave \ifnumbered needs to be inside the definition, see my edited answer
    – cgnieder
    Jul 11, 2015 at 14:37

You must log in to answer this question.

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