# Background margin fill for (first line of) section titles?

I'd like some of those "flaps" (how are these really called in typography, actually? Is it "thumb index"?) which is a bit of fill, extending to the margin; so that when you "bend" a book, you can see more easily where a section is - something similar to:

... except for sections - so something like Vertical color background of section title, except for only the first line of the section (or in general, a fill with a fixed width, aligned with the first line of the section).

I thought I could use tikz for this, so I prepared this MWE:

\documentclass[10pt,twoside,openright]{book}

\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{external}

\titleformat*{\section}{%
\normalfont\Large\bfseries\color{black!90}%
\tikzexternaldisable%
%\tikz\node(AA){A};% ! Package titlesec Error: Entered in horizontal mode.
\tikzexternalenable%
}

\begin{document}
\frontmatter
\mainmatter

\part{Introduction}
\chapter*{The first chapter - without thinking anything else}

\section{The first section - and some more text inside; and some more text inside; and some more text inside}

Blah, blah, blah...

\end{document}


The output - along with my desired effect, which I've helpfully :) indicated in red - looks like this (click for full res):

As the source notes, as soon as I start putting in \tikz\node(AA){A}; nodes for testing, I get ! Package titlesec Error: Entered in horizontal mode..

Is this possible to solve, without having to include the tikzmark or tikzpagenodes packages?

As a subquestion, is it possible to solve so that the "flap" extends from the section to the outer paper edge - regardless if the section ends up on left-hand or right-hand page? (I'm still primarily interested in a "complete" line margin-to-margin, except with a break along \textwidth - which implies two rectangle nodes)...

• What is the A? – cfr Mar 29 '15 at 21:34
• Thanks @cfr - the A is just a dummy text I entered for testing, when I first starting getting errors for empty node text {}; - I should have removed it, but nevermind now... – sdaau Mar 29 '15 at 21:38
• Why don't you want to use tikzpagenodes or tikzmark? Any particular reason to avoid making life easier for yourself? – cfr Mar 29 '15 at 21:42
• Thanks again @cfr - I am kind of guessing they'd be applicable here... I was thinking, if the solution is trivial enough, I'd prefer not including them, as I already include a ton of packages in my (real) doc; but if it is much easier, then certainly I'd go along with a solution using them! – sdaau Mar 29 '15 at 21:44

If you are willing to use tikzpagenodes, you could use something like this:

\documentclass[10pt,twoside,openright]{book}

\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external}
\usepackage{tikzpagenodes}

\titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{A}}[\tikzexternalenable]

\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node  (a) {\phantom{\normalfont\Large\bfseries Xy}};
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle (current page text area.west |- a.north) -| (current page text area.east |- a.south) -| cycle;
\end{tikzpicture}}

\begin{document}
\frontmatter
\mainmatter

\part{Introduction}
\chapter*{The first chapter - without thinking anything else}

\section{The first section - and some more text inside; and some more text inside; and some more text inside}

Blah, blah, blah...

\end{document}


# EDIT

Here's a version that adds an alphabetical marker corresponding to the section:

\documentclass[10pt,twoside,openright]{book}

\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}

\titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]

\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node  (a) {\phantom{\normalfont\Large\bfseries Xy}};
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle (current page text area.west |- a.north) -| (current page text area.east |- a.south) -| cycle;
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west)$}) {#1};
\fi
\end{tikzpicture}}

\begin{document}
\frontmatter
\mainmatter

\part{Introduction}
\chapter*{The first chapter - without thinking anything else}

\newpage

\section{The first section - and some more text inside; and some more text inside; and some more text inside}

Blah, blah, blah...

\newpage
\section{This section starts on an even page}
Some text\dots

\end{document}


• @sdaau Thanks! See edit, too. I forgot to do anything with the argument in the first version, as I wasn't sure what the goal was. But this shows how to use it, at least ;). – cfr Mar 29 '15 at 22:08

The solution from @cfr is great, but since I was working on this, I'll post an alternate solution; it is based on:

... with the trick that I'm using \vadjust (How to switch between horizontal and vertical modes?) to get rid of that "Entered in horizontal mode" (Note that it is manually tuned in respect to widths etc, so as such the solutions from @cfr are much better):

\documentclass[10pt,twoside,openright]{book}

\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{external}
\usepackage{lua-visual-debug}

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

\titleformat*{\section}{%
\normalfont\Large\bfseries\color{black!90}%
\tikzexternaldisable%
%\tikz\node(AA){A};% ! Package titlesec Error: Entered in horizontal mode.
\tikzmarkS{s\arabic{section}}\typeout{s\arabic{section}}%
\begin{tikzpicture}[remember picture,overlay]
\typeout{s\arabic{section}}
\node[rotate=90,minimum width=\baselineskip,minimum height=5.6cm,fill=gray!30,text=white,anchor=south west] at ([yshift=-1.35\baselineskip]s\arabic{section}-|current page.north east) {\arabic{section}};
\node[rotate=90,minimum width=\baselineskip,minimum height=3.6cm,fill=gray!30,text=white,anchor=north east] at ([yshift=-0.28\baselineskip]s\arabic{section}-|current page.south west) {};
\end{tikzpicture}%
\tikzexternalenable%
}

\begin{document}
\frontmatter
\mainmatter

\part{Introduction}
\chapter*{The first chapter - without thinking anything else}

\section{The first section - and some more text inside; and some more text inside; and some more text inside}
% can be used outside of section, too:
% \begin{tikzpicture}[remember picture,overlay]
% \typeout{s\arabic{section}}
% \node[rotate=90,minimum width=\baselineskip,minimum height=3cm,fill=gray!30,font=\large\bfseries,text=white,anchor=south west] at ([yshift=-2em]s\arabic{section}-|current page.north east) {\arabic{section}};
% \end{tikzpicture}%

Blah, blah, blah...

\end{document}


... and the output is (click for full res):