4

How can I add the current chapter and section to the node?

\node[color=white, anchor=west] at ($(current page.north west) - (-10ex,3.5ex)$)
{\ifnum\value{chapter}>0 \thechapter.~\chaptermark\fi};

\chaptermark or \sectionmark doesn't work here.

\documentclass{scrbook}

\usepackage[
  a4paper, top=25mm, bottom=30mm, 
  inner=20mm, outer=70mm,
  marginparsep=7mm, marginparwidth=45mm,
]{geometry}

\usepackage{xcolor}
\definecolor{myviolet}{HTML}{4c0068}

\usepackage{microtype}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[ER]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[fill=myviolet!90, draw=none] ($(current page.north west) + (8.5ex,7ex)$) rectangle ($(current page.north east) - (0ex,7ex)$);
    \draw[fill=myviolet, draw=none] ($(current page.north west) + (8ex,7ex)$) rectangle ($(current page.north west) - (0ex,7ex)$);
    \node[color=white] at ($(current page.north west) - (-4ex,3.5ex)$) {\textbf{\Large\thepage}};
    \node[color=white, anchor=west] at ($(current page.north west) - (-10ex,3.5ex)$) {\ifnum\value{chapter}>0 \thechapter.\fi};
  \end{tikzpicture}%
}
\fancyhead[OL]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[fill=myviolet!90, draw=none] ($(current page.north west) + (0,7ex)$) rectangle ($(current page.north east) - (8.5ex,7ex)$);
    \draw[fill=myviolet, draw=none] ($(current page.north east) - (8ex,7ex)$) rectangle ($(current page.north east) + (0ex,7ex)$);
    \node[color=white] at ($(current page.north east) - (4ex,3.5ex)$) {\textbf{\Large\thepage}};
    \node[color=white, anchor=east] at ($(current page.north east) - (10ex,3.5ex)$) {\ifnum\value{section}>0  \thesection.\fi};
  \end{tikzpicture}%
}

\usepackage{lipsum}

\begin{document}

\chapter{Test Chapter}
\section{Test Section}    
\lipsum[1-3]    
\newpage    
\section{Test Section 2}    
\lipsum[1-3]    
\newpage    
\section{Test Section 3}   
\lipsum[1-3]

\end{document}
11

\chaptermark sets \markboth and then you can use \leftmark for the chapter in the header. Similarly, \sectionmark sets \markright. So \rightmark can be used for the section entry in the header.

\documentclass{scrbook}

\usepackage[
  top=25mm, bottom=30mm, 
  inner=20mm, outer=70mm,
  marginparsep=7mm, marginparwidth=45mm,
]{geometry}

\usepackage{tikz}% loads also xcolor
\definecolor{myviolet}{HTML}{4c0068}

\usepackage{microtype}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[ER]{%
  \begin{tikzpicture}[overlay,remember picture]
    \fill[myviolet!90] ([xshift=8.5ex]current page.north west) rectangle ([yshift=-7ex]current page.north east);
    \node[anchor=west] at ([xshift=10ex,yshift=-3.5ex]current page.north west) {\leftmark};
    \fill[myviolet] ([yshift=-7ex]current page.north west) rectangle ([xshift=8ex]current page.north west);
    \node at ([xshift=4ex,yshift=-3.5ex]current page.north west) {\pagemark};
  \end{tikzpicture}%
}
\fancyhead[OL]{%
  \begin{tikzpicture}[overlay,remember picture]
    \fill[myviolet!90] (current page.north west) rectangle ([xshift=-8.5ex,yshift=-7ex]current page.north east);
    \node[anchor=east] at ([xshift=-8.5ex,yshift=-3.5ex]current page.north east){\rightmark};
    \fill[myviolet] ([xshift=-8ex,yshift=-7ex]current page.north east)rectangle (current page.north east);
    \node at ([xshift=-4ex,yshift=-3.5ex]current page.north east){\pagemark};
  \end{tikzpicture}%
}

% font for the pagenumber and the head entries
\setkomafont{pagenumber}{\color{white}\bfseries\Large}
\setkomafont{pageheadfoot}{\color{white}\bfseries\Large}

\renewcommand\chaptermark[1]{\markboth{\protect\usekomafont{pageheadfoot}\thechapter\autodot\ #1}{}}
\renewcommand\sectionmark[1]{\markright{\protect\usekomafont{pageheadfoot}\thesection\autodot\ #1}}

\usepackage{lipsum}

\begin{document}

\chapter{Test Chapter}
\section{Test Section}    
\lipsum[1-3]    
\newpage    
\section{Test Section 2}    
\lipsum[1-3]    
\newpage    
\section{Test Section 3}   
\lipsum[1-3]

\end{document}

enter image description here

But I think it is better to use scrlayer-scrpage (or the older scrpage2) together with scrbook instead of fancyhdr.

\documentclass{scrbook}

\usepackage[
  top=25mm, bottom=30mm, 
  inner=20mm, outer=70mm,
  marginparsep=7mm, marginparwidth=45mm,
]{geometry}

\setlength\headheight{18pt}% suggested by scrlayer-scrpage
\setlength\footheight{18pt}% suggested by scrlayer-scrpage

\usepackage{tikz}% loads also xcolor
\definecolor{myviolet}{HTML}{4c0068}

\usepackage{microtype}

\usepackage[automark]{scrlayer-scrpage}% or the older scrpage2
\pagestyle{scrheadings}
\clearscrheadfoot
\lehead{%
  \begin{tikzpicture}[overlay,remember picture]
    \fill[myviolet!90] ([xshift=8.5ex]current page.north west) rectangle ([yshift=-7ex]current page.north east);
    \node[anchor=west] at ([xshift=10ex,yshift=-3.5ex]current page.north west) {\leftmark};
    \fill[myviolet] ([yshift=-7ex]current page.north west) rectangle ([xshift=8ex]current page.north west);
    \node at ([xshift=4ex,yshift=-3.5ex]current page.north west) {\pagemark};
  \end{tikzpicture}%
}
\rohead{%
  \begin{tikzpicture}[overlay,remember picture]
    \fill[myviolet!90] (current page.north west) rectangle ([xshift=-8.5ex,yshift=-7ex]current page.north east);
    \node[anchor=east] at ([xshift=-8.5ex,yshift=-3.5ex]current page.north east){\rightmark};
    \fill[myviolet] ([xshift=-8ex,yshift=-7ex]current page.north east)rectangle (current page.north east);
    \node at ([xshift=-4ex,yshift=-3.5ex]current page.north east){\pagemark};
  \end{tikzpicture}%
}

% font for the pagenumber and the head entries
\setkomafont{pagenumber}{\color{white}\bfseries\Large}
\setkomafont{pageheadfoot}{\color{white}\bfseries\Large}

\usepackage{lipsum}

\begin{document}

\chapter{Test Chapter}
\section{Test Section}    
\lipsum[1-3]    
\newpage    
\section{Test Section 2}    
\lipsum[1-3]    
\newpage    
\section{Test Section 3}   
\lipsum[1-3]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.