Featured Post

We've moved to tex.my!

It’s been great writing on Blogger.com, but we’ve decided to move to a new platform, thanks to some valuable help from a great, great friend...

Monday, November 30, 2009

Customising running headers and footers


By default, the running headers in a two-sided book (resp. article) produced by LaTeX displays the chapter (resp. section) number + title and the page number on even pages; and the page number and section (resp. subsection) number + title on odd pages. On chapter pages, however, the running header is blank, and the page number is displayed in the centre of the footer.

All these default headers and footers can be customised, either with the facilities already provided by the memoir class, or via the fancyhdr package. Basically, you need to redefine the "plain" pagestyle for chapter pages, and the "headings" pagestyle for the non-chapter pages. Using memoir, you'd do something like

\makeevenhead{plain}{left}{centre}{right}
\makeoddhead{plain}{left}{centre}{right}
\makeevenfoot{plain}{left}{centre}{right}
\makeoddfoot{plain}{left}{centre}{right}

% respectively \makeevenhead{headings}...

While if you're using the fancyhdr package:

\fancypagestyle{plain}{
  %% Clear all headers and footers
  \fancyhf{}
  %% e.g. if right footers on odd pages and left footers
  %% on even pages are some content:

  \fancyfoot[RO,LE]{...}
  %% this applies to right headers on odd pages:
  \fancyhead[RO]{...}
  %% and this to left headers on even pages:
  \fancyhead[LE]{...}
  %% and this to centre headers on both even and odd:
  \fancyhead[C]{...}
}

\fancypagestyle{headings}{...}

As always, see the respective manuals/user guides for extensive instructions.

Here are some examples of what I've done:






(Download links of the .tex source files are at the end of this post)

Here are my specs:
  • On chapter pages:
    • page number in a coloured box on the right in the footer on odd pages;
    • ditto, but on the left in the footer on even pages;
    • blank running headers.
  • On non-chapter pages:
    • same footers as above;
    • on even pages, the chapter title on the left in the header, and a decorative line extending to the right all the way to the paper margin;
    • on odd pages, the section title on the right in the header, and a decorative line extending to the left all the way to the paper margin.
I used the tikz package for the coloured box containing the page number, as well as the decorative lines in the header.

You'll probably notice that on the non-chapter pages, I've got some coloured background going on, too. I could've used the wallpaper package to achieve this effect, but just for kicks, I did them using the tikz package, using the headings pagestyle as a hook.

So here are the .tex source (plus the public domain images used) for producing the sample pages above:
Note that my examples require wallpaper.sty v2.5. If you're using MikTeX, your wallpaper.sty is most likely v2.4, so you'll have to grab the latest version yourself.

11 comments:

  1. Hi..I could find only Wallpaper v1.10 as the lates in the CTAN link...Can you please point or post to the v2.5 file?

    ReplyDelete
  2. Hi..I could find only Wallpaper v1.10 as the lates in the CTAN link...Can you please point or post to the v2.5 file?

    ReplyDelete
  3. That's really very very nice, thanks! How about giving some code for a cover page that goes well with the examples, please?

    ReplyDelete
  4. hummm...
    fantastic, but one drawback, don't have table of contents. How we can add ToC whiteout damaging the body of chapters?

    ReplyDelete
  5. Oops... Sorry I didn't notice there were new comments!

    Muthu, OK I think I've got my version numbers mixed up... but as long as you grab the current version posted on CTAN you'll be fine (I just checked that again).

    Rafael and Reza, a new post that covers the, er, cover pages and the ToC will be coming up soon. (I hope anyway.)

    ReplyDelete
  6. Could you make more detailed tutorial about designing books/newspapers in latex? There is so many thinks that are easy with indesign or even scribus but not well described in latex. For example: printing bleeds, master pages, opentype/trutype fonts. Please make step-by-step tutorial about how to make advanced layouts of book or newspaper and how prepare them for printing house. Maybe you know some good sources on this subject?

    ps
    I'm not a latex beginner, i know how to type maths and write articles for journals/reviews.

    ReplyDelete
  7. @mar, This post is actually part of a series (http://latex-my.blogspot.com/search/label/bookdesign) which described my experiences designing a book with LaTeX and sent to a printing house. (The book itself is available at http://liantze.penguinattack.org/ebooks.html#GridReport .)

    See e.g. the post on "Setting page size and margins" for setting stock paper sizes (even though I didn't mention bleeding explicitly...). As for Opentype/Truetype fonts, I just grabbed whatever I had on my LaTeX platform, though I suppose we really should do a XeLaTeX post one of these days.

    I'm not sure what you mean by 'master pages' though, sorry.

    Having said that, there are some things that are just easier with e.g. InDesign, such as grid typesetting (though there now exists grid.sty which helps). At the end of the day it's really up to the designer which tool to use.

    ReplyDelete
  8. @mar In addition, the supplementary memoir manual (packaged in separate 'memdesign' on CTAN) would be a very helpful read. I'll remember to send any interesting links your way!

    ReplyDelete
  9. @Lian, my congratulations, you did excellent work! I need a favor, can you put link to source code of this book? Perhaps few chapters would be enough. I'm just curious how you did it and want to see complete source.

    ReplyDelete
  10. Hi,

    Do you have experimented including pdf file ? Usually, I use
    \usepackage[final]{pdfpages}

    \includepdf[pages=-, scale=0.95, pagecommand={\chapterstyle{FancyChap}}]{file.pdf}
    to include pdf in a tex file. With this, I can normally keep header and footer.

    With this template,at first reload, I have the file correctly inserted but at a second reload, I got empty pages.

    When I dont usr parameter "pagecommand", Had no problem, but no header and footer.

    Do you have any idea ?

    thanks

    Mike

    ReplyDelete
    Replies
    1. Hi Mike, this is because the "frame" around the page was actually achieved by first drawing a milky-brown rectangle over the entire page, and then drawing a big white rectangle in the middle. When using \includepdf, the page style is applied *after* the PDF is laid out on the page. So these two rectangles (especially the big white rectangle in the middle) would cover up the PDF.

      If you'd still like to use a frame-like page style, you'll need an alternative way of drawing the frame, e.g. by drawing 4 separate rectangles along the 4 sides. But we can't easily reproduce the "rounded white rectangle" effect here, though, so I'm going to skip that.

      Replace the following lines in \makeoddhead{headings} :

      \fill[MilkTea!25!white] (current page.north east)
      rectangle (current page.south west);
      \fill[white, rounded corners]
      ([xshift=-10mm,yshift=-20mm]current page.north east) rectangle
      ([xshift=15mm,yshift=17mm]current page.south west);

      and use instead:

      \fill[MilkTea!25!white] (current page.north east) rectangle ([yshift=-20mm]current page.north west);
      \fill[MilkTea!25!white] (current page.south east) rectangle ([yshift=17mm]current page.south west);
      \fill[MilkTea!25!white] (current page.north east) rectangle ([xshift=-10mm]current page.south east);
      \fill[MilkTea!25!white] (current page.north west) rectangle ([xshift=15mm]current page.south west);


      Similarly for \makeevenhead{headings}, replace

      \fill[MilkTea!25!white] (current page.north east) rectangle (current page.south west);
      \fill[white, rounded corners] ([xshift=-15mm,yshift=-20mm]current page.north east) rectangle ([xshift=10mm,yshift=17mm]current page.south west);

      and use instead

      \fill[MilkTea!25!white] (current page.north east) rectangle ([yshift=-20mm]current page.north west);
      \fill[MilkTea!25!white] (current page.south east) rectangle ([yshift=17mm]current page.south west);
      \fill[MilkTea!25!white] (current page.north east) rectangle ([xshift=-15mm]current page.south east);
      \fill[MilkTea!25!white] (current page.north west) rectangle ([xshift=10mm]current page.south west);


      Hope that helps!

      Delete