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

Levy
  • 1,167

2 Answers2

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 '19 at 23:48
  • 1
    @Levy About \hspace and \hspace* see https://tex.stackexchange.com/questions/89082/hspace-vs-hspace – samcarter_is_at_topanswers.xyz Jan 17 '19 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. – samcarter_is_at_topanswers.xyz Jan 17 '19 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. – samcarter_is_at_topanswers.xyz Jan 18 '19 at 09: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

Bernard
  • 271,350