1

I am trying to customize the header rule using fancyhdr and tikz packages. I wish the rule could overpass the margins. In order to do so I thought that drawing a line with tikz from (-1,0) to (18,0) would get 1 cm over the margins in each side, but what I got was the line crossing over just the left margin and not the right. Here's a MWE of my code:

\documentclass[12pt,a4paper]{report}

\usepackage{geometry}
    \geometry{
        left = 2cm,
        right = 2cm,
        top = 3cm,
        bottom = 2cm
    }
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{lipsum}

\usepackage{fancyhdr}
    \pagestyle{fancy}
    \lhead{Ex Tunc}
    \chead{\( \circledast \)}
    \rhead{Ex Nunc}
    \renewcommand{\headrule}{%
        \raisebox{.3cm}{%
            \begin{tikzpicture}[x=1cm,y=1cm]
            \draw (-1,0) to (18,0);
            \end{tikzpicture}
        }
    }
\begin{document}
    \lipsum[1-20]
\end{document}

Here's an image of what I got (above) and what I'd like to have (below):

enter image description here

  • You should have a look at the warnings your document produces. The headheight is too small and the warning will tell you which value is the minimal necessary height. – user36296 Jan 18 at 0:00
2

Quick hack: place a negative space in front of the rule.

\documentclass[12pt,a4paper]{report}

\usepackage{geometry}
    \geometry{
        left = 2cm,
        right = 2cm,
        top = 3cm,
        bottom = 2cm
    }
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{lipsum}

\usepackage{fancyhdr}
    \pagestyle{fancy}
    \lhead{Ex Tunc}
    \chead{\( \circledast \)}
    \rhead{Ex Nunc}
    \renewcommand{\headrule}{%
            \hspace*{-1cm}%
        \raisebox{.3cm}{%
            \begin{tikzpicture}[x=1cm,y=1cm]
            \draw (-1,0) to (18,0);
            \end{tikzpicture}
        }
    }
\begin{document}
    \lipsum[1-20]
\end{document}

enter image description here

Another approach could be to use a tikzpicture with the remember picture, overlay options. This has the advantage that it avoids the overful box warnings.

\documentclass[12pt,a4paper]{report}

\usepackage{geometry}
    \geometry{
        left = 2cm,
        right = 2cm,
        top = 3cm,
        bottom = 2cm
    }
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{lipsum}

\usepackage{fancyhdr}
    \pagestyle{fancy}
    \lhead{Ex Tunc}
    \chead{\( \circledast \)}
    \rhead{Ex Nunc}
    \renewcommand{\headrule}{%
        \raisebox{.3cm}{%
            \begin{tikzpicture}[x=1cm,y=1cm,remember picture, overlay]
            \draw (-1,0) to (18,0);
            \end{tikzpicture}
        }
    }
\begin{document}
    \lipsum[1-20]
\end{document}
  • It worked, thank you! By the way, what's the difference between \hspace{} and \hspace*{}? And what these remember picture, overlay options do? – Levy Jan 17 at 23:48
  • 1
    @Levy About \hspace and \hspace* see tex.stackexchange.com/questions/89082/hspace-vs-hspace – user36296 Jan 17 at 23:57
  • 1
    @Levy The overlay option makes the tikzpicture behave as if it would not have any width and height. Therefore it does not get pushed to the right by the content that you add on its left site. – user36296 Jan 17 at 23:59
  • 1
    @marmot Thanks for your suggestion. In this particular case, I would prefer no not use absolute positioning with respect to the page. By using positioning relative the the headline content, this will adapt in case something in the headline changes, for example the font size or the top margin. – user36296 Jan 18 at 9:16
2

You don't need to use a hammersledge like tikz for this: the titleps package, which comes with titlesec, can replace fancyhdr, and it defines a widenhead command:

\documentclass[12pt, a4paper]{report}

\usepackage{geometry}
    \geometry{%
        hmargin = 2cm,
        top = 3cm,
        bottom = 2cm
    }%
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{lipsum}

\usepackage{titleps}

\newpagestyle{wide}{%
\headrule
\sethead{\hspace{1cm}Ex Tunc}{\( \circledast \)}{Ex Nunc\hspace{1cm}}
\setfoot{}{\thepage}{}
}%
\pagestyle{wide}
\widenhead{1cm}{1cm}

\begin{document}

    \lipsum[1-20]

\end{document} 

enter image description here

  • Thank you for your answer. I didn't know about the titlepspackage. – Levy Jan 22 at 14:43
  • It can do more or less what fancyhdr does, and doesn't require playing with marks. – Bernard Jan 22 at 18:40

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.