1

Here is a MWE:

\documentclass[fontsize=11pt,pagesize=auto,hidelinks,cleardoublepage=empty,parskip]{scrbook}
\usepackage[paperheight=10in,paperwidth=8in,margin=2cm,heightrounded,bindingoffset=5mm,showframe=false]{geometry}

\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}
    {\gdef\chapterlabel{} \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\%}}{0pt}
    {
        \begin{tikzpicture}[remember picture,overlay]%
            \node(main)[yshift=-7cm] at (current page.north west)
            {%
                \begin{tikzpicture}[remember picture, overlay]
                    \draw[fill=black] (0,0) rectangle (\paperwidth,7cm);
                    \node[anchor=east,yshift=-3cm] at (current page text area.north east) {\color{white}\large Chapter \thechapter};%
                    \node[anchor=east,yshift=-4cm] at (current page text area.north east) {\color{white} #1};%
                \end{tikzpicture}
            };%
        \end{tikzpicture}
    }
\titlespacing*{\chapter}{0pt}{0pt}{3cm}[0pt]

\begin{document}
    \chapter{Introduction}
    \blindtext[4]
\end{document}

It renders as:

enter image description here

Notice how the right edge of the chapter title does not align with the right edge of the body text. Why is this? I can't figure out what I'm doing wrong here.

Kent Boogaart
  • 664
  • 4
  • 15
  • 1
    You should not use titlesec and a KOMA class together –  Mar 01 '18 at 10:36
  • @ChristianHupfer I'm not sure what that means, or how to fix it. Or why everything else seems to work despite the fact that I'm apparently doing something wrong. – Kent Boogaart Mar 01 '18 at 10:38
  • 1
    There is a a clear warning that KOMA classes and titlesec are not compatible to each other. –  Mar 01 '18 at 10:39

3 Answers3

2

A node has an inner sep, a given distance from the node content to the border of the node. By default this is 0.333em, you should set inner sep=0 for your two nodes.

In addition to Christian's comments about titlesec and KOMA, you should also take Zarko's advice in his answer, and not nest tikzpictures. It can sometimes cause problems, and it is not at all needed here.

enter image description here

\documentclass[fontsize=11pt, pagesize=auto, hidelinks, cleardoublepage=empty, parskip]{scrbook}
\usepackage[paperheight=10in, paperwidth=8in, margin=2cm, heightrounded, bindingoffset=5mm, showframe=false]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}
    {\gdef\chapterlabel{} \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\%}}{0pt}
    {
        \begin{tikzpicture}[remember picture,overlay]%
            \node(main)[yshift=-7cm] at (current page.north west)
            {%
                \begin{tikzpicture}[remember picture, overlay]
                    \draw[fill=black] (0,0) rectangle (\paperwidth,7cm);
                    \node[anchor=east,yshift=-3cm,inner sep=0] at (current page text area.north east) {\color{white}\large Chapter \thechapter};%
                    \node[anchor=east,yshift=-4cm,inner sep=0] at (current page text area.north east) {\color{white} #1};%
                \end{tikzpicture}
            };%
        \end{tikzpicture}
    }
\titlespacing*{\chapter}{0pt}{0pt}{3cm}[0pt]

\begin{document}
    \chapter{Introduction}
    \blindtext[4]
\end{document}
Torbjørn T.
  • 206,688
  • Thanks Torbjørn, that was indeed the problem. What frustrates me is that I knew about inner sep as I've used it elsewhere. I just didn't notice the connection in this particular context. I'm really struggling when it comes to debugging LaTeX. How would I have known this? Is there some tooling I'm missing that allows you to see where values are being derived from? Why spacing is being added/not added etc? – Kent Boogaart Mar 01 '18 at 10:43
  • @KentBoogaart In a case like this I suppose the first thing you could have done is to draw the border of the nodes (add draw=white), that would make it clearer how the node is positioned. – Torbjørn T. Mar 01 '18 at 10:44
2

Note that the usage of package titlesec with a KOMA-Script class is not recommended. So here is a suggestion without titlesec and tikz that also works for unnumbered chapters like table of contents and for chapter titles longer than text width:

\documentclass[fontsize=11pt,cleardoublepage=empty,parskip,chapterprefix]{scrbook}
\usepackage[paperheight=10in,paperwidth=8in,
            margin=2cm,heightrounded,
            bindingoffset=5mm]{geometry}
\usepackage{xcolor}
\usepackage{blindtext}% only for dummy text

\renewcommand\raggedchapter{\raggedleft}
\RedeclareSectionCommand[
  font=\Huge\scshape\color{white},
  prefixfont=\large,
  beforeskip=-1sp,
  afterskip=1sp
]{chapter}

\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#1}{chapter}{%
    \parbox[b][5cm]{\textwidth}{%
      \makebox[0pt][c]{\textcolor{black}{\rule{2\paperwidth}{\paperheight}}}%
      \raggedchapter
      \raisebox{.5\baselineskip}{\parbox[b]{\textwidth}{\raggedchapter#2#3}}}%
      \par
  }{%
    #2#3% original definition for other levels
  }%
}

\begin{document}
\tableofcontents
\chapter{Introduction}
\Blindtext
\chapter{Chapter with long, long, long title that needs more than one line}
\Blindtext
\end{document}

enter image description here

enter image description here

enter image description here

Note that you have to use a different font like libertine to get chapter titles using \sffamily\bfseries\scshape.

esdd
  • 85,675
1

beside what Torbjørn T. was indicated, you should not nested tikzpicture:

\documentclass[fontsize=11pt,pagesize=auto,
               hidelinks,cleardoublepage=empty,
                parskip]{scrbook}
\usepackage[paperheight=10in,
            paperwidth=8in,margin=2cm,heightrounded,
            bindingoffset=5mm]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}
    {\gdef\chapterlabel{} \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\%}}{0pt}
    {
    \begin{tikzpicture}[remember picture,overlay,
    every node/.append style={text=white, inner xsep=0pt}]
        \draw[fill=black] (current page.north west) rectangle (\paperwidth,-3cm);
        \node[left, yshift=-3cm, font=\large] at (current page text area.north east) {Chapter \thechapter};%
        \node[left,yshift=-4cm] at (current page text area.north east) {#1};%
    \end{tikzpicture}
    }
\titlespacing*{\chapter}{0pt}{0pt}{3cm}[0pt]

\begin{document}
    \chapter{Introduction}
    \blindtext[4]
\end{document}

the above code is also little bit corrected regarding use of font size and text color.

enter image description here

Zarko
  • 296,517