1

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?

1

As the text in your headers comes from \leftmark and \rightmark you might get a solution by defining your own mark on that page. Like

\markboth{LEFT TEXT}{RIGHT TEXT}

But that depends on the presence of \section commands whether it will work. If the page boundaries are clearly defined, like with the \newpage in your example, it may help to put this code both at the beginning and the end of the page.

Another way is to replace \leftmark and \rightmark in your definitions with \myleftmark and \myrightmark, and define these as:

\newcommand{\myleftmark}{\leftmark}
\newcommand{\myrightmark}{\rightmark}

and the on that special page redefine these with

\renewcommand{\myleftmark}{SPECIAL TEXT}
\renewcommand{\myrightmark}{SPECIAL TEXT}

and define them back to the first ones after that page (or put that special page inside a TeX group).

1

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}

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.