# How do I create a header with tikz boxed chapter references?

Another user asked asked a similar question; yet I just need the top header and not the full design from edp. And I provide a minimal working example.

This is how it should look like:

If you press one of the upper header boxes, they will redirect you to the right chapter. I thought it was really nice, so I wanted it for private purpose. Yet I have trouble with recreating the effect.

Here is what I tried with tikz:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{lipsum}
\pagestyle{empty}

\begin{document}

\begin{tikzpicture}[overlay, remember picture]
\foreach \x in {1,...,7};
\draw let \p1 = (current page.west), \p2 = (current page.east) in node[minimum width=\x2-\x1, minimum height=2cm, draw, rectangle, fill=black!90, anchor=north west, align=left, text width=\x2-\x1] at ($(current page.north west)$) {\Large\bfseries \quad #1};
\end{tikzpicture}

\chapter{Chapter one}
\end{document}


Well, it creates a big black box in the header with no text in it. So it doesn't work at all. I thought of using the \foreach function, but had trouble with it.

Basically I need help with creating the boxes at the top and I have problems with letting them redirect to the chapter.

Hopefully you can help me?

• You have fill=black!90 so you create a line of almost completely black rectangles. – cfr Mar 26 '16 at 20:09
• Your code does not compile for me. Don't you get an error rather than black boxes? – cfr Mar 26 '16 at 20:14
• here an idea tex.stackexchange.com/questions/298006/… – touhami Mar 26 '16 at 20:30
• I am no TikZ expert, but what's the replacement for #1 here? Something from the \foreach loop? – user31729 Mar 26 '16 at 20:45

Here's a tcolorbox version with automatic linking to the chapters. Since tcolorbox is TikZ package much additions can be done to those boxes with over- or underlays.

\documentclass{book}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\usetikzlibrary{calc}
\usepackage{lipsum}
\pagestyle{empty}

\usepackage{totcount}

\usepackage{fancyhdr}

\newtcolorbox{mycolorboxinternal}[1][]{%
enhanced jigsaw,
sharp corners,
boxrule=0.0pt,
boxsep=0pt,
colupper={white},
fontupper={\bfseries},
colback=orange,
halign=center,
valign=center,
nobeforeafter,
height=0.8cm,
#1,
}

\newcommand{\mycolorbox}[2][]{%
\begin{mycolorboxinternal}[#1]%
#2%
\end{mycolorboxinternal}%
}

\regtotcounter{chapter}

\begin{tcbraster}[%raster valign=center,
raster row skip=0pt,
raster before skip=0pt,
raster columns={5},
raster equal height=all,
raster column skip=0pt,
nobeforeafter,
raster left skip=0pt,
raster right skip=0pt]
\foreach \x in {1,...,\totvalue{chapter}} {%
\ifodd\x
\def\backgroundcolor{orange}%
\else
\def\backgroundcolor{red}%
\fi
\mycolorbox[colback=\backgroundcolor]{\protect\nameref{chaplabel:\x}}%
}
\end{tcbraster}
}

\pagestyle{fancy}
\usepackage{blindtext}
\begin{document}

\chapter{Kirche}\label{chaplabel:1}
\chapter{Soziales}\label{chaplabel:2}
\chapter{Gesellschaft}\label{chaplabel:3}
\chapter{Umwelt}\label{chaplabel:4}
\chapter{Kultur}\label{chaplabel:5}
\chapter{Entwicklung}\label{chaplabel:6}
\chapter{Ausland}\label{chaplabel:7}

\newpage
\blindtext[5]
\end{document}


• it's not good idea to hundle labels this way, you can think for some thing that does this automatic. – touhami Mar 27 '16 at 14:03
• @touhami: Yes, of course, but I wanted to provide a quick solution – user31729 Mar 27 '16 at 14:35
• it's my bad , i am so sorry. – touhami Mar 27 '16 at 15:17
• @touhami: Sorry, why? You're completely right, but I had no time yesterday evening – user31729 Mar 27 '16 at 15:19
• cfr below said that the point in the question was about box creating. – touhami Mar 27 '16 at 15:24

This draws the tabs at the top and inserts the chapter numbers using LaTeX's \label/\ref system. In my opinion, that is quite enough for one question and hyperlinking is left as an exercise for the reader.

In other words, I can make TikZ do some stuff, but I can't make hyperref behave when it doesn't want to and it didn't want to.

However, there really is more than one question here, so I think it is worth answering one of the questions even if I cannot answer the other.

\documentclass{book}
\usepackage{tikz,calc}
\pagestyle{empty}
\begin{document}
\newlength\tabwidth
\setlength\tabwidth{\paperwidth/7}%
\begin{tikzpicture}[overlay, remember picture]
\path [fill=red] (current page.north west) rectangle +(\paperwidth,-20mm);
\path (current page.north west) foreach \i [evaluate=\i as \j using {25*(mod(\i,2)}] in {1,...,7} {%
+({(\i-1)*\tabwidth},0) node [minimum width=\tabwidth, minimum height=2cm, rectangle, fill=white!\j!red, font=\Large\bfseries, text=white, anchor=north west] {\ref{ch:\i}}
};
\end{tikzpicture}
\chapter{Chapter one}\label{ch:1}
\chapter{2}\label{ch:2}
\chapter{3}\label{ch:3}
\chapter{4}\label{ch:4}
\chapter{5}\label{ch:5}
\chapter{6}\label{ch:6}
\chapter{7}\label{ch:7}
\end{document}

• Thank your for your answer, but I get a lotof errors: like missing semicolon, number and illegal unit. – Amabile Scientius Mar 26 '16 at 21:13
• @AmabileScientius All I can say is that it works for me and I do not get errors. Do you get errors when you copy just my code into a new, empty .tex file and compile that? If so, precisely which error? The first error is the important one. Most of the others are probably consequences of the first and, if any aren't, you can't tell which until you've fixed the first. – cfr Mar 26 '16 at 21:19
• @AmabileScientius: This works for me out of the box! Maybe you need to update your TeX distribution – user31729 Mar 26 '16 at 21:35
• it's not good idea to hundle labels this way, you can think for some thing that does this automatic – touhami Mar 27 '16 at 14:03
• @touhami I just meant it to demonstrate the TikZ part i.e. drawing the tabs. as I said, it doesn't address the linking part of the question so the content of the tabs is only intended for demonstration purposes. If the question were just getting the content into the tabs, I would have worked on a way to automatise it. But since that would still leave the linking question, I assume it would be better to address the content along with the linking. – cfr Mar 27 '16 at 14:41