[Tex/LaTex] Change header text and keep the same design built in \fancyhead

fancyhdrheader-footer

I'm trying to make a good looking header for even and odd pages and it works perfectly fine. But I would like for one section to change the text in the header (a name different than section or chapter), and keep the same design built with \fancyhead[]{}.

I have the following code:

\documentclass{book}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[LE]
{\begin{tikzpicture}[overlay,remember picture]
\fill[red] ([xshift=1.5cm]current page.north west) rectangle (\paperwidth,1cm);%
\fill[black] (current page.north west) rectangle (0cm,1cm);%
\node[anchor=west] at (0,1.5cm) {\bfseries \color{white}{\leftmark}};
\node[anchor=west] at ([yshift=-0.608cm]current page.north west) {\bfseries \color{white}{Page \thepage}};
 \end{tikzpicture}
}

\fancyhead[RO]
{
\begin{tikzpicture}[overlay,remember picture]%
\fill[black] (current page.north west) rectangle (\paperwidth,1cm);
\fill[red] (current page.north west) rectangle (0cm,1cm);
\node[anchor=east] at (0,1.5cm) {\bfseries \color{white}{\rightmark}};
\node[anchor=east] at ([yshift=-0.608cm]current page.north east) {\bfseries \color{white}{Page \thepage}};
\end{tikzpicture}
}

\begin{document}
\chapter{Test Chapter}
\section{Test Section}    
\lipsum[1-15]
\newpage    
\section{Test Section 2}    
\lipsum[1-15]
\lhead{Here is where I want another header, but still keep the layout}
\newpage   
\end{document}

If I use \lhead{The text}, the whole tikz design disappears and I end up having plain text in the header. Can anyone help me with this?

Best Answer

Use \markright{The text}, which will set the \rightmark just like \section would do.

Be more careful with protecting end-of-lines: inside a tikzpicture they're ignored, outside it they're kept.

\documentclass{book}
\usepackage{tikz}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[LE]{%
  \begin{tikzpicture}[overlay,remember picture]
    \fill[red] ([xshift=1.5cm]current page.north west) rectangle (\paperwidth,1cm);
    \fill[black] (current page.north west) rectangle (0cm,1cm);
    \node[anchor=west] at (0,1.5cm) {\bfseries \color{white}{\leftmark}};
    \node[anchor=west] at ([yshift=-0.608cm]current page.north west) 
      {\bfseries \color{white}{Page \thepage}};
   \end{tikzpicture}%
}

\fancyhead[RO]{%
  \begin{tikzpicture}[overlay,remember picture]
    \fill[black] (current page.north west) rectangle (\paperwidth,1cm);
    \fill[red] (current page.north west) rectangle (0cm,1cm);
    \node[anchor=east] at (0,1.5cm) {\bfseries \color{white}{\rightmark}};
    \node[anchor=east] at ([yshift=-0.608cm]current page.north east)
      {\bfseries \color{white}{Page \thepage}};
  \end{tikzpicture}%
}

\begin{document}
\chapter{Test Chapter}
\section{Test Section}    
\lipsum[1-15]
\newpage    
\section{Test Section 2}    
\lipsum[1-15]
\markright{The text}
\newpage   
\end{document}