1

I was inspired by this example to try and make some advanced headers using tikz. I attempted to construct a simple example in order to understand how it all works together and managed to create this: enter image description here The problem is that the header doesn't extend across the entire page like the example, and I've tried to figure out how but to no avail, unfortunately. Here's the MWE:

\documentclass{article}
\usepackage[a4paper, hmargin=2cm, vmargin=1cm, includeheadfoot]{geometry}
\usepackage[nocheck]{fancyhdr}
\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\headrulewidth}{0pt}

\setlength{\headheight}{20pt}

\pagestyle{fancy}

\fancyhead[R]{
    \begin{tikzpicture}
        \draw[left color = red!80!black, right color=red!50!black] (current page.north west) rectangle ($ (current page.north east) +(-4cm,-1cm) $);
        \node[circle, very thick, draw=yellow, text = white] at ($ (current page.north east) + (-4.5cm, -0.5cm) $) {\thepage};
    \end{tikzpicture}
}

\begin{document}

Here's a random introduction

\newpage

Here's some more random text

\end{document}

In the example, the original poster could just use the current page function of tikz to ensure the header stretched across horizontally without further modifications, yet I need to manually do that with the calc library if I want to achieve the same effect. How can I auto calibrate the header to extend across the page horizontally?

1 Answer 1

5

I'm not sure, what is your problem. I guess that something like this:

enter image description here

I didn't bother with rectangle color, its height and position. This is easy to add or change in the MWE below:

\documentclass{article}
\usepackage[a4paper, hmargin=2cm, vmargin=1cm, includeheadfoot]{geometry}

\usepackage{tikz}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[C]{%
    \begin{tikzpicture}[remember picture, overlay]
\fill[teal]    (current page.north west)rectangle  ([yshift=-1cm] current page.north east);

\node[circle, draw, thick, minimum size=7.5mm, inner sep=0pt, white]
    at ([xshift=-1cm, yshift=-0.5cm] current page.north east)   {\thepage};
    \end{tikzpicture}
            }
\begin{document}
\pagestyle{fancy}

The first page

\newpage

The second page

\end{document}
4
  • what is the need for [remember picture, overlay] option in your answer ?
    – Jhor
    Commented yesterday
  • !Thanks. Are you sure that "It does not harm"? What would happen if, on the same page, something really need it, like a tikzmark for example ?
    – Jhor
    Commented 22 hours ago
  • 1
    @Zarko how is it not necessary? My "simplified" code: \draw[left color = red!80!black, right color=red!50!black] (current page.north west) rectangle ([yshift=-1cm]current page.north east); \node[circle, very thick, draw=yellow, text = white, xshift=-1cm, yshift=-0.5cm] at (current page.north east) {\thepage}; is only calibrated when specifying remember picture, overlay for the tikzpicture (which I had missed the first time). If I don't use that, it's not calibrated. If you can explain the effects of this in your answer along with your alternative solution it'd be great!
    – Atex
    Commented 21 hours ago
  • 1
    Ups, I messed up my solution. correct one was the first (and now the last) version of answer.
    – Zarko
    Commented 20 hours ago

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .