3

I'm trying to make a header on every page with text above and below the header rule, so that it looks something like this:

                                  header text above line
--------------------------------------------------------
                                  header text below line

[body of page]

Is this possible with fancyhdr? Or is there another way to do this?

I realize this may not be aesthetically pleasing, but I'm trying to emulate another document (not made with LaTeX).

1

Try this, with fancyhdr:

\pagestyle{fancy}
\fancyhead[LE,LO]{~\\\hrulefill\\~}
\fancyhead[CE,CO]{~\\\hrulefill\\~}
\fancyhead[RE,RO]{text above\\\hrulefill\\text below}

If desired, switch left and right. You will need to play with the headheight and the line skips in the header, and related dimensions.

| improve this answer | |
  • This almost works. I end up with two horizontal lines, but I can remove the default one with \renewcommand{\headrulewidth}{0pt}, and then I get the desired result. Thanks! – Eric Sep 18 '16 at 22:29
  • Right! You need to remove the default line. When I use fancyhdr, I start by zeroing or emptying everything and building up from there. – user103221 Sep 18 '16 at 23:12
1

You can set one header item using a tabular with a single paragraph column that is the width of \textwidth:

enter image description here

\documentclass{article}

\usepackage{fancyhdr,lipsum}

\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\renewcommand{\headrulewidth}{0pt}% Remove header rule
\fancyhead[C]{%
  \begin{tabular}{ @{} p{\textwidth} @{} }
    \hfill Header text above line \\
    \hline
    \hfill Header text below line
  \end{tabular}}
\setlength{\headheight}{24.4pt}% Should be at least 24.4pt

\begin{document}

\lipsum

\end{document}
| improve this answer | |
0

Here is a solution with titleps, a tabular environment and booktabs:

\documentclass[12pt,twoside, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{titleps}
\usepackage{array, booktabs, graphicx}
    \usepackage{lipsum}
\newpagestyle{mine}{\renewcommand\arraystretch{0.5}%
\sethead[][\begin{tabular}{@{}>{\raggedright\small\arraybackslash}p{\textwidth}@{}}header text above line\\\midrule header text below line\end{tabular}][]%
{}{\begin{tabular}{@{}>{\raggedleft\small\arraybackslash}p{\textwidth}@{}}header text above line\\\midrule header text below line\end{tabular}}{}
\setfoot{}{\thepage}{}
}
\pagestyle{mine}

\begin{document}

\lipsum

\end{document} 

enter image description here

| improve this answer | |
0

This is a very interesting question and I would like to provide a comprehensive solution for document class article. This solution uses the packages geometry, fancyhdr, pgf, scrextend and textpos, it looks complicated at the beginning, but trust me, it is nice and clean and do not require adjustment even when you change page geometry.

The usage of the packages are, geometry is used to setup the page geometry, fancyhdr is used to produce the header, pgf is used to define length, scrextend is used to find out if current page is an even page or odd page, and finally textpos is used to place the text.

I also added text block at the top left corner.

Here is the code

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass[UTF8]{article}

\usepackage[b4paper, twoside]{geometry}
%% you can set page to be 'a4paper' or 'c5paper' or 'legalpaper' or whatever provided by 'geometry', no other adjustment is needed
%% even and odd page are set to have different margin size, you can set margin size to be equal by removing 'twoside', no other adjustment is needed
\usepackage{lipsum} %% generate random texts

\usepackage{fancyhdr} %% produce header

\usepackage{pgf} %% define length
\usepackage[absolute, overlay]{textpos} %% put text blocks at absolute position
\usepackage{scrextend} %% check if current page is even or odd

\pgfmathsetlengthmacro{\texttop}{1.0 in + \voffset + \topmargin + \headheight + \tabcolsep}
%% this is the very top of the body

\pgfmathsetlengthmacro{\textrightodd}{1.0 in + \oddsidemargin + \hoffset + \textwidth}
%% this is the right edge of the body for odd page
\pgfmathsetlengthmacro{\textrighteven}{1.0 in + \evensidemargin + \hoffset + \textwidth}
%% this is the right edge of the body for even page
\pgfmathsetlengthmacro{\textleftodd}{1.0 in + \oddsidemargin + \hoffset}
%% this is the left edge of the body for odd page
\pgfmathsetlengthmacro{\textlefteven}{1.0 in + \evensidemargin + \hoffset}
%% this is the left edge of the body for even page

\setlength{\TPHorizModule}{1.0 pt}
%% set the unit used by 'textpos' to be 'pt' because the length returned by 'pdf' macros are in 'pt'

\pagestyle{fancy} %% set up page header
\lhead{left head}
\chead{center head}
\rhead{page \thepage}
\cfoot{}

\begin{document}

\title{title}
\date{\today}
\author{author}

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
%% set the origin used by 'textpos' at top left of page for even and odd pages differently
\pgfmathwidth{"note for page \thepage"}
%% store the length of wanted text into '\pgfmathresult'
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
%% '[0, 0]' sets top left corner of text block to be used as anchor point, '(0, 0)' sets the anchor point right at the origin of 'textpos'
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
%% set the origin used by 'textpos' at top right of page for even and odd pages differently
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
%% '[1, 0]' sets top right corner of text block to be used as anchor point, '(0, 0)' sets the anchor point right at the origin of 'textpos'
\noindent
note for page \thepage
\end{textblock}

\maketitle

\thispagestyle{fancy}

\lipsum[6]

\newpage

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\lipsum[2]

\newpage

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\lipsum[3]

\newpage

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\lipsum[4]

\end{document}

here is what is produced

text block

For the page geometry, you can refer to this figure

layout

| improve this answer | |

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.