Page LayoutPage numbers "beside" headers

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
nvx
Posts: 25
Joined: Mon Dec 17, 2007 7:49 pm

Page numbers "beside" headers

Post by nvx »

Hi there,

Using fancyhdr it is very easy to produce the following layout:

Code: Select all

          left (even) page                       right (odd) page
____________________________________   ____________________________________
|                                   |  |                                   |
|          [#  chapter          ]   |  |   [          section  #]          |
|          .--------------------.   |  |   .--------------------.          |
|          |                    |   |  |   |                    |          |
|          |        body        |   |  |   |        body        |          |
|          |                    |   |  |   |                    |          |

The necessary code is this:

Code: Select all

\usepackage{fancyhdr}
\pagestyle{fancy}
  % Let's get rid of "Chapter X." in the headers...
  \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
  % ...and also of the trailing dots after section numbers
  \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}{}}
  \fancyhf{}  % Delete current setting for header and footer
  \fancyhead[LE]{\thepage\hspace{1em}\leftmark}
  \fancyhead[RO]{\rightmark\hspace{1em}\thepage}
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
  \addtolength{\headheight}{1.6pt}  % Space for the rule and larger letters
\fancypagestyle{plain}{
  \fancyhead{}  % Get rid of headers on plain pages...
  \fancyfoot[C]{\thepage}
  \renewcommand{\headrulewidth}{0pt}  % ...and of the lines
  \renewcommand{\footrulewidth}{0pt}
}
\fancypagestyle{empty}{
  \fancyhead{}  % Get rid of headers on plain pages...
  \fancyfoot{}  % Get rid of footers on plain pages...
  \renewcommand{\headrulewidth}{0pt}  % ...and of the lines
  \renewcommand{\footrulewidth}{0pt}
}
However, how does one get the layout below?

Code: Select all

          left (even) page                       right (odd) page
____________________________________   ____________________________________
|                                   |  |                                   |
|     [#]  [chapter             ]   |  |   [             section]  [#]     |
|          .--------------------.   |  |   .--------------------.          |
|          |                    |   |  |   |                    |          |
|          |        body        |   |  |   |        body        |          |
|          |                    |   |  |   |                    |          |

I would like there to be a fixed "gap" (say, 1em) between chapter/section titles and page numbers. In other words, chapter title should be justified left according to the left edge of the text and similarly section title should be justified right according to the right edge of the text. Page number on the left (i.e., even) page should be justified right, page number on the right (i.e., odd) page should be justified left.

Any help would be much appreciated.

Best regards,
nvx
Last edited by nvx on Thu Mar 31, 2011 8:39 pm, edited 1 time in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Page numbers "beside" headers

Post by meho_r »

Did you try something like this:

Code: Select all

\fancyhead[LE]{\hspace*{-2.5em}\makebox[1.5em][r]{\thepage}\hspace{1em}\leftmark}
\fancyhead[RO]{\rightmark\hspace{1em}\makebox[1.5em][l]{\thepage}\hspace*{-2.5em}}
nvx
Posts: 25
Joined: Mon Dec 17, 2007 7:49 pm

Page numbers "beside" headers

Post by nvx »

Dear meho_r,

Thanks for the hint with \mbox you provided. Although the code did not work as expected, it reminded me of \fancyheadoffset. The neccessary code is as follows:

Code: Select all

\fancyheadoffset[LE]{3em}
\fancyhead[LE]{\makebox[2em][r]{\thepage}\hspace{1em}\leftmark}
\fancyheadoffset[RO]{3em}
\fancyhead[RO]{\rightmark\hspace{1em}\makebox[2em][l]{\thepage}}
Best regards,
nvx
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Page numbers "beside" headers

Post by localghost »

I can offer a more „TeXnical” solution.

Code: Select all

\documentclass[11pt,a4paper,twoside,english]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[
  headheight=14pt,
  includeheadfoot,
  inner=3cm,
  outer=4cm,
  vmargin=3cm
]{geometry}
\usepackage{fancyhdr}
\usepackage{blindtext}

\fancyhf{}
\fancyhead[LE]{\leavevmode\llap{\thepage\qquad}\nouppercase{\leftmark}}
\fancyhead[RO]{\leavevmode\nouppercase{\rightmark}\rlap{\qquad\thepage}}
\pagestyle{fancy}

\begin{document}
  \blinddocument  
\end{document}
The blindtext package is only for creating dummy text thus not part of the solution.


Best regards and welcome to the board
Thorsten
nvx
Posts: 25
Joined: Mon Dec 17, 2007 7:49 pm

Re: Page numbers "beside" headers

Post by nvx »

Dear Thorsten,

Thanks for your solution. I like its simplicity -- compared to the convoluted thing I came up with... ;)

Have a nice day,
nvx
Post Reply