12

By default, fancyhdr includes a headrule separator line that spans the text width at the top of the page. Is there a (simpler) way to get the headrule (or footrule, for that matter) to span only, say, 50% of the text width?

For instance, consider this MWE that does (roughly) what I've asked using a tabular to add a line manually with a little space around the title itself:

\documentclass[12pt]{article}

\newcommand\mytitle{Title of Document}

\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[c]{\begin{tabular}{c}\hspace{1in}\textsc{\mytitle}\hspace*{1in}\\\hline\end{tabular}}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

Is there a more straightforward way to accomplish this? The ideal solution would not be dependent on the length of the title, and would have an adjustable width in terms of percentage of the \textwidth dimension.

Epilogue

I ended up using

\makeatletter
\def\headrule{\nointerlineskip\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi%
    {\hfil\rule{0.5\headwidth}{\headrulewidth}} \vskip-\headrulewidth}
\makeatother

in order to stay as close to the original definition made by fancyhdr as possible.

3 Answers 3

6
\documentclass[12pt]{article}  
\newcommand\mytitle{Title of Document}   
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[c]{\textsc{\mytitle}}
\renewcommand{\headrulewidth}{4pt}
\renewcommand\headrule{\makebox[\textwidth]{\rule{0.5\headwidth}{\headrulewidth}} \vskip-\headrulewidth}
\pagestyle{fancy}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

Use the optional argument of \rule to shift the line up/down: \rule[shift]{width}{length}

3
  • In fact, the height of the \makebox seems to add space between the title and the rule that is undesirable...
    – cslstr
    Commented Jun 24, 2014 at 12:43
  • Along with the addition of \nointerlineskip (see tex.stackexchange.com/a/186385), this does what I need.
    – cslstr
    Commented Jun 24, 2014 at 15:03
  • USe the optional argument, e.g.: \rule[1ex]{0.5\headwidth}{\headrulewidth}
    – user2478
    Commented Jun 24, 2014 at 16:13
5

It's pretty easy to do using titlesec instead of fancyhdr: its titleps module has a\widenhead command that allows enlarging header rules (indenpendently on each side); a negative value will shrink the rule. It doesn-t add any vertical space. This supposes you don't want to write something on the left or right side of the header, but I don't see why you would with a short rule. Actually, you could still write on the sides but it would be more tricky.

Here is an example in which the header rule is exactly half textwidth wide:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier, heuristica}

\usepackage[nomarginpar]{geometry}
\newcommand\mytitle{Title of Document}
\usepackage[pagestyles]{titlesec}
\newpagestyle{mine}{%
\headrule
\widenhead{-0.25\textwidth}{-0.25\textwidth}
\sethead{}{\scshape\mytitle}{}
\setfoot{}{\thepage}{}
}

\pagestyle{mine}

\usepackage{blindtext}

\begin{document}

\blinddocument

\end{document} 

enter image description here

You also can make ‘oldstyle’ headers, with a horizontal rule a little larger than the header, with this code. It uses the facilities of the makebox package:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier,heuristica}
\usepackage{makebox}

\usepackage[nomarginpar]{geometry}
\newcommand\mytitle{Title of Document}
\usepackage[pagestyles]{titlesec}

\newpagestyle{thine}{%
\sethead{}{\rlap{\raisebox{-1ex}{\makebox*{\enspace\scshape\mytitle\enspace}{\hrulefill}}}\enspace\scshape\mytitle}{}
\setfoot{}{\thepage}{\hrulefill}
}

\pagestyle{thine}

\usepackage{blindtext}

\begin{document}

\blinddocument

\end{document} 

Result:

enter image description here

3
  • I considered this (actually, with the \fancyhfoffset option, which does essentially the same thing), but it breaks down in the case of \newcommand\mytitle{What if the Title of Document is Longer?}.
    – cslstr
    Commented Jun 24, 2014 at 13:57
  • I don't know well the details of fancyhdr, as I use titlesec for formatting section titles, so that I prefer to use a package of the same family, but it would have surprising it didn't have more or less the same functionalities… However I find titlesec/titleps easier to use on the whole.
    – Bernard
    Commented Jun 24, 2014 at 14:21
  • We all find what we are used to easier to use, don't we? Just why I started out with fancyhdr! :)
    – cslstr
    Commented Jun 24, 2014 at 14:32
2

Not sure what spacing you want, but something like

\documentclass[12pt]{article}  
\newcommand\mytitle{Title of Document}   
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[c]{\textsc{\mytitle}}
\renewcommand{\headrulewidth}{4pt}
\setlength\headheight{15pt}
\renewcommand\headrule{%
\nointerlineskip
\hbox to \headwidth{\hss\rule{.25\headwidth}{\headrulewidth}\hss}%
\vspace{-\headrulewidth}}
\pagestyle{fancy}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}
2
  • \nointerlineskip was the key to the whole problem, for me at least. Adding this also enables the solution by @Herbert perform as it should.
    – cslstr
    Commented Jun 24, 2014 at 15:01
  • @cslstr thought it might be:-) a box is spaced according to baselineskip normally but a rule is not Commented Jun 24, 2014 at 15:25

You must log in to answer this question.

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