Page Layoutfancyhdr and extending margins

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
theo moore
Posts: 72
Joined: Thu Jan 15, 2009 3:16 pm

fancyhdr and extending margins

Post by theo moore »

Hi, I'm using the fancyhdr package (I realise it's been 'superceded' with the titlesec package, but I find the documentation for that package confusing).

What I want is to extend my left and right margins and to have the page number outside the margin, but with the \leftmark or \rightmark (or name of the chapter/section) to align with the page margin.

For example, here is a crude way of doing it

Code: Select all

\documentclass[twoside]{report}

\usepackage{fancyhdr}
\pagestyle{fancy}

\fancyhead{}
\fancyfoot{}
\fancyheadoffset[LE, RO]{1cm}
\fancyhead[RO]{%
 \small\sffamily\sc\rightmark \hspace*{0.4cm} {\{\thepage\}} }
\fancyhead[LE]{%
 \small\sffamily\sc {\{\thepage\}} \hspace*{0.4cm} \leftmark }

\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}


\begin{document}

\chapter{The name of the chapter}
\section{The name of the section}

\noindent Text goes here

\newpage

\noindent Text goes here

\newpage

\flushright Text goes here

\end{document}
The left and right margins have been extended by 1cm, and by using \hspace, I can insert some space between the page number and the heading. Unfortunately, I want it to be perfectly aligned. How would I do that?

I thought that perhaps I could use something like

Code: Select all

\parbox{extensionlength}{\thepage}
Unfortunately, it does not seem to like that.

Any ideas?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

fancyhdr and extending margins

Post by gmedina »

Hi,

you could use a box with the same width that the one you specified for the head offset and place the page number inside that box; i.e., you could use something like

Code: Select all

...
\fancyhead{}
\fancyfoot{}
\fancyheadoffset[LE, RO]{1cm}
\fancyhead[RO]{%
\small\sffamily\sc\rightmark\makebox[1cm]{\{\thepage\}}} 
\fancyhead[LE]{%
\small\sffamily\sc \makebox[1cm]{\{\thepage\}}\leftmark}
...
using the second optional argument for \makebox you can control the position of the number inside the box (default: centered).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
theo moore
Posts: 72
Joined: Thu Jan 15, 2009 3:16 pm

Re: fancyhdr and extending margins

Post by theo moore »

Yup! Perfect 8-)
Post Reply