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.

Saturday, November 7, 2009

Fancy Chapter Headings


If you look at my post that started this series, you might notice that the first page of every chapter in the Grid Computing Cluster report is rather fancy. I've initially got my inspirations from Stefan Kottwitz's post on 'Fancy chapter headings with TikZ'.

Here are the samples of how the chapter pages would look like:





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

The chapter number and title are rendered with TikZ, similar to what's described in Stefan Kottwitz's post. I threw in a background image across the top of the page, and another one at the lower corner, using the wallpaper package.

Just so we can choose a different image for every chapter, I defined a new command that holds the image filename (\newcommand\chapterillustration{}) and re-define it at each chapter (\renewcommand\chapterillustration{cherry-tomatos}).

Notice that the positioning of the chapter title and the fern leaf background image is different depending on whether the chapter starts on an odd or even page. The memoir document class provides the commands \checkoddpage and \ifoddpage...\else\...\fi, so that you can define what to do in each case.  Don't worry if you're not using the memoir document class, these commands are also available via the changepage package.

Now to actually implement these definitions. The memoir document class lets you define chapter styles (e.g. \makechapterstyle{FancyChap}{\def\printchaptertitle##1{...}} see the manual on "Chapter Headings" for more details) in the preamble, which you would activate in the document body with \chapterstyle{FancyChap}.

On the other hand, if you're not using the memoir document class, use the \titleformat and \titlespacing commands from the titlesec package instead.

Sorry I'm too tired to even post relevant snippets; here are the links to the .tex sources to produce the sample pages above. I've incorporated the code for Changing page size and sizes, too. (By the way I used the lipsum package to generate the dummy text. You know, Lorem ipsum dolor sit amet...)
The sample public domain images are from here (cherry tomatos), here (computer chips) and here (fern leaf).

Sunday, November 1, 2009

Using Colours in LaTeX

A brief departure from the bookdesign series, as some of my future posts within and outside the series will be dealing with the use of colours, so I might as well get it out of the way first. Summarily, we'll take a look at how to use colours in LaTeX with the xcolor package. This will work with all outputs, i.e. .dvi, .ps and .pdf.

The most useful commands for applying colours made available by xcolor are:
  • \color{color} (applies color to the text in the currrent group)
  • \textcolor{color}{text} (applies color to the specified text only)
  • \pagecolor{color} (colors the entire page to be color)
  • \colorbox{color}{text} (creates a box containing the specified text, with color as the background)
  • \fcolorbox{frame color}{bg color}{text} (same as above, but with a coloured border around the box)
See Section 2.6 of the xcolor manual for full list and more details of commands for colour application. Alternatively, if you're using emacs, you can look it up with M-x list-colors-display.

The xcolor package has quite a collection of pre-defined colours; they are listed in Section 4 Colors by Name in the manual. Apart from the 19 "base" colours that are always available, you can also access more pre-defined colours via the dvipsnames, svgnames and x11names options. Here's a quick example:

\documentclass[a6paper,12pt]{article}
\usepackage[x11names]{xcolor}

\begin{document}

% 'LemonChiffon1' from x11names
\pagecolor{LemonChiffon1}

% 'magenta' is a base colour
\textcolor{magenta}{Hello World!}

% 'CadeBlue1', 'Firebrick2' and 'Goldenrod1'
% are available via x11names
What a \colorbox{CadetBlue1}{wonderful}
\fcolorbox{Firebrick2}{Goldenrod1}{world}.
\end{document}

You can mix up your own colours, too. \color{LemonChiffon1!80} means 80% LemonChiffon1 and 20% white, while \color{lime!30!yellow!60!Mahogany} would be 30% lime, 60% yellow, and 10% Mahogany. You can also give names to the colours you concoct, so that you can reuse them at various points in your document. For example:

  • \definecolor[named]{CoolBlack}{cmyk}{.3,0,0,1} defines the CMYK 'Cool Black' (useful if your printer asks for CMYK black instead of 'rich black')
  • \definecolor[named]{AquaBlue2}{rgb}{.553,.769,.98} defines 'AquaBlue2' with decimal RGB values (range [0,1])
  • \definecolor[named]{Hazelnut}{HTML}{BDA59B} defines 'Hazelnut' with HTML RGB values. Useful if you're re-using colour definitions from CSS styesheets.
If you're like me i.e. you just can't hit the right figures to get a colour just right, head over to COLOURlovers for lotsa inspirations and colour schemes. I got almost all my colours for the Grid Computing Cluster report from this website.

Have fun and happy LaTeXing!