1

I would like to have a solid rectangle (with some text inside it) as my heading. I tried the following:

\documentclass{article}
\usepackage{tikz}
\usepackage{fancyhdr}

\fancypagestyle{heading1}{ \fancyhead{} \renewcommand{\headrulewidth}{0pt} \begin{tikzpicture} \draw[fill=darkgray] (0,0)rectangle(\paperwidth,2); \node[white] at (2, 1) {Some heading text on page 1}; \end{tikzpicture} }

\begin{document} \thispagestyle{heading1}

Some text on page 1

\end{document}

But the gray rectangle does not extend to the far left side of the page (although it does go to the far right side), nor does it extend to the top.

Sam
  • 141
  • You need something like \hspace*{\dimexpr -1in-\oddsidemargin}. You can also go with tikzpagenodes, everypage or \AddToHook{shipout/foreground}... – John Kormylo Jul 16 '21 at 21:31
  • You can refer to a previous topic on this. see https://tex.stackexchange.com/questions/296509/fancy-header-and-footers – fromthebeeland Jul 16 '21 at 21:33

2 Answers2

0

You can start your rectangle from the top left corner. Like this:

\documentclass{article}
\usepackage{tikz}
\usepackage{fancyhdr}
\fancypagestyle{heading1}{
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=darkgray] (current page.north west) rectangle(\paperwidth,-1);
\node[white] at (2, 1) {Some heading text on page 1};
\end{tikzpicture}
}
\begin{document}
\thispagestyle{heading1}
Some text on page 1
\end{document}

Gray rectangle on top of page

There is probably a more correct way to do a thing like this.

0

For a simple rectangle, you do not need tikz:

enter image description here

\documentclass{article}
\usepackage{xcolor}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{geometry}
\geometry{verbose,tmargin=4cm,headheight=3cm,headsep=1cm}
\fancypagestyle{heading1}{
\renewcommand{\headrulewidth}{0pt}
\fancyhead{\centering\makebox[0pt][c]{\colorbox{blue!60}{
\centering\begin{minipage}[c][3cm][c]{.99\paperwidth}
\hspace{\dimexpr\oddsidemargin+1in}\huge\bfseries\sffamily\color{white} Some heading text on page 1
\end{minipage}}}}}
\begin{document}
\thispagestyle{heading1}
\lipsum
\end{document}
Fran
  • 80,769