This is a question triggered by Automatically remove page number in one-page documents.
When activating the fancyhdr package, it is necessary to clear the headers, which I do below with \fancyhf{}
, because it will otherwise print MY FIRST SECTION
as the header for the document below. It will still, however, print the ruler below the empty header, so I remove this with \renewcommand{\headrulewidth}{0pt}
.
The problem is this: I put these lines in my custom .sty
file. When I do want a header in my document, I clearly don't want to hard-wire that header into my .sty
file. It should rather go in the preamble of the document I am writing, as done below. But since I put \renewcommand{\headrulewidth}{0pt}
in my .sty
file, it will now not print any ruler, even though I've added a header.
What I am looking for is a way for LaTeX to automatically activate \renewcommand{\headrulewidth}{0pt}
only when the header is actually empty.
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\endinput
%% And ends here
\end{filecontents}
\usepackage{mystyle}
\fancyhead[C]{Woah! There's no ruler here!}
\begin{document}
\section{My first section}
\end{document}