typography
Readability
Wednesday, February 13th, 2008 | LaTeX | 2 Comments
Before we get too well underway, let me start by warning that I am not a typographer by trade. What follows here is my understanding and subjective opinion on the choice of typefaces for academic articles, working papers, books and pretty much anything with a high amount of content that needs to be presented. Many of my observations here will most likely not matter to those who are creating flyers, posters, etc., as there are pretty much no rules that I am aware of that governs those materials.
Since TeX was Donald Knuth’s fix to the ugly digital typesetting of his books, The Art of Computer Programming, it seems relevant that we take a bit of a pause from the intricacies of TeX to look at readability of the text we are producing with this system. The last task in Knuth’s The TeXbook simply states, ‘Go forth now and create masterpieces of the publishing art!’, but like Stefan A. Revets remarks in the Octavo package manual, then Knuth probably did not intend that everything published with TeX was supposed to ‘shriek “TeX” from every page’. So why is it that almost every TeX-based publication is so easy to identify and for many, so easy to dislike. Apart from the standard document layout that we have already covered how to customise with memoir in earlier posts, this what we will try to take a look at today.
Typefaces come in all sorts of varieties, light, heavy, condensed, wide, and in as many standard configurations that there are typefaces. The norm for legible typefaces used to be that the width of the English lower-case letters, a to z, would take up approximately 13 em. Narrower than this can be good for shorter lines and for saving on paper, wider than this gives more whiteness on the page, which can be used to provide a greater sense of space. Like we saw in the post on microtypography, then a uniform ‘grayness’ of the page can help to improve the aesthetics of your text. With too narrow fonts the blackness of the page will usually take an overhand, and too wide fonts, the whiteness will be too much. So we would like to find the ‘just right’ balance, which is typefaces where the lower-case alphabet takes up about 13 em of space when set. Of course, this being an art-related topic, we should note that the rules are here to be broken, so if something works better for you or for something you are producing, by all means, go ahead.
So, for these purposes, let us take a look at a number of standard available LaTeX fonts, and a couple free, but non-standard fonts, and how they measure up against this 13 em measurement.
‘Nimbus Roman No9’ is an adaption of the world famous ‘Times New Roman’, although this version is a bit wider than the original. You can use this typeface by issuing the command \usepackage{times}. In order to understand the use of ‘Times New Roman’, it is necessary to understand its origin. In 1931, ‘The Times’ commissioned a new typeface that should be legible and economise space, because the less space articles can be printed in while remaining legible, the less will ‘The Times’ have to pay in publishing costs (since you could run fewer pages or more advertisements). ‘The Times’ has since then had several other typefaces created to succeed ‘Times New Roman’. ‘Times New Roman’ has also served much popular use since Microsoft has been including it as the default serif typeface in all versions of Windows since Windows 3.1. It should be remembered, though, that the typeface is created to save money while maintaining legibility, not for optimal legibility, which we may also see in the figure above as it is the narrowest of the ‘normal’ serif typefaces in use in the LaTeX world.
‘Computer Modern’ is the typeface that Donald Knuth created together with TeX. The version depicted in the figure above is from the cm-super package that contains Type1 outlines of the ‘Computer Modern’ font family, rather than the original METAFONT definitions from Knuth. ‘Computer Modern’ is the standard typeface in use in any TeX or LaTeX document, it is the hallmark of the thing that shrieks ‘TeX’. The glyphs are thin and the contrast of the font is rather low for a serif typeface, in effect causing the typeface to feel ‘too light’. There are, thus, other factors than merely the ‘13 em width’ we are currently looking at, that influences the quality of a typeface. For the mere purpose of not shrieking TeX, we will recommend the use of another typeface for texts.
‘Gentium’ is a relative new-comer to the typeface scene of freely available typefaces, and is designed by the award-winning type designer Victor Gaultney. It has been designed for good legibility, to be reasonably compact, attractive, and freely available. It is also intended to embrace the Latin alphabet world with all its plethora of diacritics and provide a common OpenType font that can be used in all these countries. It is not trivial to get this font to work with LaTeX, but it is a reasonably nice alternative to the free fonts that are available in the LaTeX distributions and the non-free ones that come with a bunch of strings attached (like ‘URW Garamond No8’ below). Its compactness is almost neglible and does not cause legibility to suffer, it is only a few points short of the 13 em width.
‘URW Garamond No8’ is an adaption of 16th century type cutter Claude Garamond’s typeface. The ‘Garamond’ is one of the most often-used type families when it comes to professional works, owing in particular to Slimbach’s beautiful digitalisation in Adobe’s Garamond Pro typeface. URW’s adaption of ‘Garamond’ fits exactly within the 13 em measure, but the different adaptions vary somewhat to both sides. Unlike the previous three fonts we have looked at, ‘URW Garamond No8’ is not free and does not come with LaTeX distributions by default. It can, however, be downloaded from CTAN under the non-free fonts. Be sure to investigate the license that accompanies it for whether your use is applicable. In order to use this font in LaTeX, after it has been installed, you can add the following code to your preamble: \renewcommand{\rmdefault}{ugm}.
The last typeface we will look at is, ‘URW Palladio L Roman’, which is an adaption of Hermann Zapf’s Palatino. It is a somewhat wider typeface than the rest that we have looked at, which in some opinions may improve legibility. Like ‘Garamond’ and ‘Times New Roman’, ‘Palatino’ is one of the most often used typefaces, probably also owing to the fact that Microsoft also provides it since Windows 2000. LaTeX provides this together with a correspondingly designed maths typeface in the package mathpazo. There are several versions of Palladio available in LaTeX, including versions with smallcaps and old-style figures (both absent from ‘URW Garamond No8’, and the latter unavailable in ‘Gentium’).
So if we want to save a lot of space while maintaining legibility, we should probably be using ‘Nimbus Roman No9’ (or if you have professional typefaces one of the ‘Times New Roman’ successors like ‘Times Modern’), if we want good general-purpose text with a classical feel to it, we should probably be using ‘Gentium’ or ‘Garamond’ (or ‘Plantin’, or… there are really many great serif typefaces in this category, but most of them are fairly expensive, unfortunately), and if we want a lighter feel to our text while consuming more space, there is ‘Palladio’ (or many other more recent typefaces).
As I hinted to in the discussion of ‘Computer Modern’ there are several more measures for the fitness of a typeface for a particular purpose, but being an artform in itself, there are so many varying and opposing opinions that only very vague guidelines exist. So instead of trying to uncover all of these, let us do like the rest of the crowd seems to do and let us rely on our subjectivity and take a look at a bit of text typeset with each of these typefaces.
Here we see that the wider ‘Computer Modern Roman’ takes up a line more for the paragraph excerpt; this will run up in cost when you publish, but the cost of the narrower font is of course it is not as pleasant to read most of the time. However, we can also see that ‘Computer Modern Roman’ feels a lot more light than ‘Nimbus Roman No9’, not making it particularly ideal either for typesetting a paper, let alone a book. So let us continue and take a look at some of the more traditional book typefaces.
Here, ‘URW Garamond No8’, at least, has proven its worth by surviving from the 16th century until now, in widespread use. However, both typefaces present strongly and have each their distinctive style while maintaining familiar, pleasant and good readability. When you want to choose a typeface for publishing an article, book, or the like, then picking a typeface that falls in line with either of these two will most likely be a good bet.
There is a rather stark contrast between the narrow ‘Nimbus Roman No9’ and the wide ‘URW Palladio L Roman’. To some, the wider ‘Palatino’ may be too wide, but if you need to provide your pages with a lighter feel (without increasing the line spacing), then it and other wider typefaces may prove very useful. In the end, though, the typeface you choose will be a matter of personal preferences and what you think look good. But do not use ‘Comic Sans’ for anything serious, ever.
Now, with that out of the way, I will not go long into a discussion on the merits of using sans-serif typefaces for the main text. Several psychologists (a bunch of them working for Microsoft) has done various studies that indicate that there is no discernible difference in reading speeds or preferences between the two. However, for the sake of tradition and the beauty of the serifs, I suggest we keep using serif typefaces for main text. There is, however, another thing that is relevant to look at, namely the length of lines. One of the LaTeX packages that sees widespread use in a lot of Europe is the a4wide package. For those who are uninitiated into this monstrosity, it makes the margins a lot smaller than the LaTeX default. Let us look at the difference between standard and wide margins:

So what is the difference here, other than saving paper by printing more. It is the distance that our eyes have to travel when we reach the end of one line and skip to the beginning of the next line. The longer the lines are, the easier it is for the eye to get lost and you have to backtrack and pick up at the right spot again, either because you reread the same line again or because you skip a line too far ahead. So unless there is a really good reason for the much smaller margins, do not do that.
This is about as far as we will go into the intricacies of typefaces and layout today, there is much, much more to expound on the topic, though, so I might return to it some day.
Microtypography
Tuesday, November 27th, 2007 | LaTeX | 3 Comments
Type setting is an art, and as most arts, it is extremely subjective. However, I will try to venture a few of my subjective opinions here as absolute truth (just because a bit of controversy is fun; feel free to disagree). Today we will look a bit at how interword spacing changes the feeling of a text, why margin protrusion makes things more even (and what margin protrusion is), as well as what font expansion is and why that helps as well. In particular, we will see how these things mean the difference of badly typeset text and nicely typeset text.
While most people I know who read this blog use LaTeX predominantly, most of us make acquaintance with the mainstream text tools, OpenOffice and Word. They create equally shoddy text (I guess it is their prerogative given that they are text processors rather than type setting programs, but it is still depressing). Still, looking at the output from one of these programs may serve as an (extremely good) motivating factor for considering LaTeX. The image below illustrates a standard text in OpenOffice, without hyphenation (with hyphenation did not change the result radically).
Looking at the interword spaces on line three, we see that they’re radically different from the spaces in the last line, for instance. This gives a very uneven reading and is not particularly nice looking at any rate. If we furthermore look down along the right margin, we see that, even though we have selected a justified text formatting, that the visual appearance of the text against the margin is not even. For instance, the ‘y’ and comma seem to be futher to the left than the ‘d’ and ‘r’. Furthermore, the fourth last line seem to be shorter than the others when you glance down along the right margin. This detracts from the aesthetic qualities of the text as well.
We are not going to be able to crank anything much more useful out of OpenOffice, so let us switch to our favourite typesetting language, LaTeX, and see how it looks there. This gives us what we can see in the following figure (margins adjusted to fit better with the OpenOffice defaults – normally margins are larger in LaTeX for better aesthetic value).
This still gives us a somewhat jagged appearance in the right margin. What we want to do is to let some characters, in particularly punctuation, fall into the margin, causing more uniform lines to appear (visually, not mathematically). This is called character protrusion, or margin kerning.
Pdftex 1.40 and newer supports two levels of character protrusion: level 1 that does not take line breaking into account, and level 2 that does. The details of this is easier to see if we are writing a two-column page. Below we see level 1 in the left column and level 2 in the right column. The interword spacing is clearly superior at level 2. In particular, this is illustrated in line 4 where there’s a much larger space in the left column after the full stop.
Looking at line 15 in the right column, we do, however, see that spacing is not entirely even, as there is a larger space after the full stop, at the text starting ‘At first I could not believe my’. These ‘artifacts’ happen when a proper line breaking cannot be made (breaking ‘eyes’ would be unnatural), and thus the line is padded with a bit of extra space, here after a full stop since we are more forgiving about extra spaces here as there is already some form of pause.
Some of the lines are spaced too tightly in my opinion in the right column above, example the line reading ‘my very feelings changed to repulsion and ter-’ is pushed too tightly together, particular the change from ‘feelings’ to ‘changed’ seem too close. As a way to improve the spacing we can turn on font expansion. What this feature does is to use a more narrow version of a font when there is little space, and a wider version of a font when there is ‘too much’ space. Since this change is, for the most part, automatically generated from the original font, great caution should be applied as it may distort the original font shapes, however, with the defaults, all this happens to a maximum of a three per cent change from the original shape, making it almost invisible to the naked eye. The results below are, however, fairly compelling. The left column is the same as the right column above, and the right column is with font expansion turned on at full level.
This is miles from the look of OpenOffice that we saw at the beginning of this post. Indeed, returning to default margin size in full width, we can see the text typeset below with full character protrusion and font expansion.
These things are, of course, just one among many of microtypographic finnesses that can be controlled in later versions of pdftex. What we have not looked at here is, of course, how you can use these things. Fortunately there is a nice package that wraps up a lot of these issues: microtype. Using it is exceptionally easy:
\usepackage{microtype}
This will enable both character protrusion and font expansion. For more advanced manipulative changes, you can refer to microtype’s documentation. (There is even a way to make it look like it was created in Word or OpenOffice!)
Programming packages
Saturday, August 11th, 2007 | LaTeX | 1 Comment
Now that we have seen how to both write commands and environments, it is time to look at how we can reuse this code in several different documents. The basis of reusability in LaTeX is handled through packages, otherwise known as .sty-files. A copious amount of packages exist for LaTeX at CTAN, and we have seen several of them already: tikz, graphicx, fontenc, mathdesign, and many others. Common to each package is that it provides a number of commands and environments that gives you some form of functionality. Tikz allows us to create vector-based drawings directly from inside LaTeX, graphicx allows us to interface with a number of existing graphics formats, mathdesign provides new math fonts that correspond to a number of free and non-free serif fonts.
So, if you have some form of functionality that you use repeatedly in different projects and would like to abstract it away in a separate package that you can easily use in your other projects, a package is what you are looking for. The basis of creating a package is to pick a filename, say, mypackage.sty (on some systems you may have to keep to the 8.3 filename standard, so it may be prudent to name your packages to fit inside that, but it is not a requirement).
Every package starts with either the first or both of the following lines:
\ProvidesPackage{mypackage}[2006/05/01 version 1.0 by Someone] \NeedsTeXFormat{LaTeX2e}
\ProvidesPackage‘s name is required to be the same as the file of the package, and \NeedsTeXFormat specifies what document format is required in order to use the package (if it is using functionality specific to a certain format). Here, LaTeX2e is what most of us use when we write documents, but plenty of others exist as well.
Packages can, of course, also depend on other packages, but unlike in the main document, we do not use \usepackage to include these, rather, a separate command, \RequirePackage exist instead that otherwise does the same thing (more or less). So if we were to require some higher level constructs for directing command flow using the ifthen package, we could use the following line of code inside our package:
\RequirePackage{ifthen}
With some packages it is also possible to pass them options, for instance for the babel package that takes care of renaming various commands to a specific language:
\usepackage[british,french]{babel}
These options dictate that we should load both the british and french modules and that we want to pre-select french (as that is the last option) for the language to write in. Likewise it might be relevant for your package to provide a number of options that can be used to control overall functionality of the package or pre-define certain environments or commands. Declaring a package option is done using the \DeclareOption command. So if we were to declare a theorem option, it might look like this:
\DeclareOption{theorem}{% % code goes here }
And when we subsequently use the package in a document we can write:
\usepackage[theorem]{mypackage}
All the options aren’t executed at the point of declaration though, it requires a separate command, \ProcessOptions that can occur anywhere in the package code (though naturally after the \DeclareOption and before the end of the package). A reason to place it later in the document than immediately after your declared options could be that some options redefine commands that are introduced later on in the package, for instance.
Most of the remaining code in a package is just a collection of introducing new commands and environments. There is one thing that is worthy of notice, though, namely that command names that are internal to packages are typically given a @-sign inside them, as these are not directly available in normal documents, as we have seen before. Inside a package the @-sign behaves as a letter and you do not need to surround all such uses with \makeatletter and \makeatother. Also, remember that commands must be unique, there are no overloading, so try to pick names that don’t conflict with other packages. Of course, given the sheer number of packages in existence, this may prove rather difficult.
This is basically all there is to creating packages. The rest is pretty much up to what you need to abstract for kinds of behaviour. Looking back at the code we have seen in this blog, we might, for instance, pick out the layout of a book to package. That way we can reuse this layout at a later time, merely by including a package. And that is, in short, the basic idea of packages.
An example book style
Wednesday, June 20th, 2007 | LaTeX | 2 Comments
Now that we have made our way through how to style a document, it is time to look at how we can actually use some of this knowledge in practice, by typesetting an actual book. Since all that Lorem Ipsum can get a tad boring in the long run, we will create a book with some works that have passed into the public domain, namely some by H. P. Lovecraft, a significant American horror author of the early 20th century. None other than the man behind Cthulhu.
Much like with the last four blog posts, we will take a look at styling the book in the same order, namely: chapter, table of contents, sections, and footers/headers. Remembering the plain chapter style, we have something like this, for our book:
As I’ve written before: while this is certainly acceptable, it’s not really too unique of fascinating, so let us think up a way to get it to feel more 1920′s and horror-ish. The last part is of course hard to quantify unless you pick some cheesy font that’s impossible to read, so let’s just stick to making it look a bit like a book on good literature. We will try to accomplish this by just writing the chapter number as a word (one, two, three, etc.), and omit the ‘Chapter’ prefix, centered, and put the chapter title in italics, also centered. We can do that with the following code:
\makechapterstyle{bookstyle}{ % Yes it's a very imaginative name \setlength{\beforechapskip}{0em} \setlength{\midchapskip}{1em} \renewcommand{\chapnumfont}{\normalfont\large\bfseries\fscshape} \renewcommand{\chaptitlefont}{\normalfont\huge\bfseries\itshape} \renewcommand{\printchaptername}{} \renewcommand{\printchapternum}{% \centering\chapnumfont\numtoname{\thechapter} } \renewcommand{\printchaptertitle}[1]{\centering\chaptitlefont ##1} } \chapterstyle{bookstyle}
This verily will take a bit further description. The \fscshape command introduces fake small-caps, and its existence depends on what font package you may or may not be using. If you have a commercial grade font, or one of the built-in fonts, then you will want to use \scshape instead. The \fscshape I am using here is provided by the mathdesign package together with the URW Garamond typeface. The only other surprise here should be the \numtoname command that is provided by memoir, and this command converts a number into a corresponding name, for instance \numtoname{13} will be turned into ‘thirteen’.
With this modest change in place, our chapter page suddenly looks like this:
This seems a tad more classic literature-ish to me, at least. So we will chalk one up for success on the chapter style. We could, of course, add something to the fancy and automatically insert an image for each chapter to complement the story, but since my freehand drawing skills aren’t what I’d like them to be, we shall skip this step for now.
With this out of the world, let us take a look at the existing table of contents:

From this we see that our book is divided in two parts: one for some of Lovecraft’s essays, and another for a very few of his poems. Since most of his essays aren’t subdivided, we probably have no need to indicate any of these, so let us turn them off and simplify the table of contents a bit. This may be done using:
\settocdepth{chapter}
Now, as long as you only want to tweak the distance between table of content entries, perhaps remove the dots on the line to the page numbers and move the page numbers a bit back and forth on the page, then everything is easy to do with memoir, but as soon as you move beyond that, things start to get really complicated, unfortunately. Not one to shy away from hard things, let us look at what it takes to center the part and draw lines around it and to typeset all the chapters in italics.
The first part is, unfortunately, rather hard. It requires us to override two different commands: \l@part and \partnumberline. The reason for this is two-fold. When we issue a \part command, the following is written to the .aux-file: \@writefile{toc}{\contentsline{part}{\partnumberline {I}Essays}{1}}, for instance, for part 1 called Essays. When this is going to be typeset then \l@part is called with the last two {}‘s above as arguments.
Using the tikz package that we have covered earlier to draw the lines about the part title, we will give the entire definition of the two commands here and then proceed with an explanation of the key points. The overall structure has been ‘borrowed’ from memoir.cls:
\newlength\sb@partheight \renewcommand*{\l@part}[2]{% \ifnum \c@tocdepth > -2\relax \addpenalty{-\@highpenalty}% \addvspace{\cftbeforepartskip}% \begingroup{ \centering \settoheight{\sb@partheight}{\Large\bfseries\MakeUppercase{#1}} \setlength{\sb@partheight}{.5\sb@partheight} \addtolength{\sb@partheight}{5pt} \begin{tikzpicture} \draw (0,0) node { \Large\bfseries\MakeUppercase{#1} }; \draw (-.25\textwidth,\sb@partheight) -- (.25\textwidth,\sb@partheight); \draw (-.25\textwidth,-\sb@partheight) -- (.25\textwidth,-\sb@partheight); \end{tikzpicture} \par }\endgroup \fi } \renewcommand{\partnumberline}[1]{}
So, I guess this requires some explanation. The test on \c@tocdepth tests for whether the part should actually be displayed in the table of contents, and -2 is the code for the part in this instance. The \addpenalty command here makes TeX prefer to create a page break \emph{before} the part rather than after, so we don’t have a part title dangling alone at the bottom of a page, since that doesn’t look particularly nice. Then we use \begingroup to localise our changes so they don’t seep into the remaining document, and inside this group, we do a bit of slight trickery to compute how heigh the part title is, using the \settoheight. Then we calculate the half height and add some space, and this we use as a measurement in the following tikzpicture. The tikzpicture code should be familiar, as we have already seen it here. The redefinition of \partnumberline merely discards the part number as we aren’t really interested in that.
Our changes to the actual chapters are a lot easier to accomplish. Indeed, it only requires the following two lines:
\setlength{\cftbeforechapterskip}{.2em} \renewcommand{\cftchapterfont}{\normalfont\small\itshape}
Taking all this together and using it with our book, we get the following table of contents for it:


This looks a good deal more like some of the older fictional works. So far so good. The next thing to turn pretty is the section titles. For the most part Lovecraft’s essays don’t have actual sections, but a few of them have sections named I, II, III, etc., and even fewer have actual section titles. One of the essays that do, is ‘Herbert West: Reanimator’.
First off, the numbering of the section doesn’t really make any sense in a fictional work as the title is usually sufficient in and of itself, so let’s do away with that. Apart from that, the standard section title layout looks quite decent. This is an extremely easy change then:
\maxsecnumdepth{chapter} \setsecnumdepth{chapter}
And we get the following:
This leaves us with just the page headers and footers. As we can see below, and as I have written previously, the standard page headers are just ugly.
So, to rectify this, we will place the part title on the left page and the chapter title on the right page, both centered in the header, and we’ll place the page number on the page edge in the footer. And this is easily accomplished with the following code:
\makepagestyle{mybookstyle} \makeoddhead{mybookstyle}{}{\itshape\rightmark}{} \makeevenhead{mybookstyle}{}{\itshape\leftmark}{} \makeoddfoot{mybookstyle}{}{}{\thepage} \makeevenfoot{mybookstyle}{\thepage}{}{} \makepsmarks{mybookstyle}{% \def\partmark##1{\markboth{##1}{}} \def\chaptermark##1{\markright{##1}} \def\sectionmark##1{} } \pagestyle{mybookstyle}
And presto, we get a much nicer output:
Throughout all these examples, we have used the @-sign repeatedly in commands. This is ok if you’re implementing this in a package file, but if you’re pasting them into your document’s preamble, the @ will not be interpreted as a character and a lot of errors will follow. To rectify this, surround the code in the preamble with the commands \makeatletter and \makeatother. This will make everything right. In a future post, I will try to talk about implementing packages to better hide this functionality away in a reusable component.
This concludes the styling example for today. If you’re in a need for a lot of sensible material to try out different stylings on, then I suggest that you head over to WikiSource or Project Gutenberg and grab some works that have entered the public domain and try to make your own compilation of them. And for your viewing pleasure, here is the book typeset with the style above.
Styling the page footer and header
Tuesday, June 12th, 2007 | LaTeX | 6 Comments
This leads us to the last post in my series of styling documents, namely the one on how to customise page headers and footers. This is one of the most visible style elements as it is (typically) present in some form or the other on almost all pages except chapter pages. As with the section titles it is also relevant for the page headers and footers to mimick the aesthetic feel of the chapter page, thus keeping a uniform expression that can be tied exclusively to your work (this is the really hard part, by the way). Before we delve into the intricacies, let us take a look at the standard page header:
For those of you with bad eye-sight, the chapter name and title is printed in all-caps. Absolutely ghastly. I mean, what is this, a tribute to AOL users?! We are definitely going to have to change something here indeed.
This is probably as good a time as any to bring this up. When we are typesetting a book it contains pages that will be to the left (verso pages) and on the right (recto pages). These two pages have different margins (try to open one of your books on your bookshelf and check for yourself), and furthermore books tend to have different things in the verso and recto headers and footers. Typically this means that the chapter number and title will be displayed on the verso page and the section number and title on the recto page (or part and chapter in other books, or something entirely different in yet other books).
These items (chapter number and title and section number and title) isn’t grabbed out of thin air in LaTeX, but is written into internal commands that can be read using the \leftmark and \rightmark commands (named for their use on left and right pages, obviously). Thus, the all-caps chapter title on the default page header has been written into the left mark here, so we will need to modify how the marks are stored.
The first step to creating a new page header and footer is to create a new page style using the \makepagestyle{name} command. So if we want to create a new page style where the header contains the page number and the chapter/section number (if any) and title, we would start out with the following:
\makepagestyle{mypagestyle}
To create a nice separation between the page number and the title—rather than typesetting them at opposite ends of the header—we will separate them with a vertical rule. Now the commands to modify the headers and footers of a page style are: \makeevenhead{style}{left}{center}{right}, \makeoddhead, \makeevenfoot, and \makeoddfoot. All with the same number of arguments. So, this means we can extend our page style as follows:
\makeevenhead{mypagestyle}{\thepage\hskip.5cm\vrule\hskip.5cm\leftmark}{}{}
\makeoddhead{mypagestyle}{}{}{\rightmark\hskip.5cm\vrule\hskip.5cm\thepage}
If we tack on a \pagestyle{mypagestyle} now, we will get some output like this:
Better than the standard, but it still feels partly like an AOL user tribute, what with all the caps and stuff. Before we fix this, it is time to discuss marks in a bit more detail.
Storing information to be read by \leftmark and \rightmark is accomplished using \markboth and \markright (no, there is no separate \markleft). And these two commands are meant to be invoked inside some specific commands that are in turn invoked from \chapter, \tableofcontents, etc. These commands include: \tocmark, \partmark, \chaptermark, and \sectionmark. There are, of course, more marks and you can generate more by creating new lists, so if you’re overriding marks you need to make sure to catch all of them appropriately.
Getting to the actual code now would be too easy. When we need to print the chapter number we have to remember that a book is divided into several ‘sections,’ namely the front matter (table of contents, lists, foreword, etc.), main matter (the actual book chapters), and back matter (bibliography, indices, etc.). Of these it is only customary to give the chapters in the main matter numbers in the heading.
Before we venture on, we also need to look at one of the ‘reserved’ symbols in LaTeX: @. This section is vague on purpose in order not to spend too much time on this detail. The @ is used for ‘reserved’ commands inside class-files and packages and its default ‘code’ to something that cannot be used. If, however, you’re in the need to using commands with @ in them you can use the command \makeatletter and then once you’re done return it to its original state with \makeatother. I will try to remember to visit this topic in more detail at some later time.
The use of @ becomes relevant when we need to test whether we’re in the main matter, because this is done (with memoir, remember @ means we’re overriding the internals of some package) using the \if@mainmatter where @mainmatter is a boolean value.
Now we’re finally ready to create our own marks! To recoup, we want to write the chapter mark on the verso page and the section mark on the recto page. This is done like this:
\makeatletter
\makepsmarks{mypagestyle}{
\def\chaptermark##1{\markboth{%
\ifnum \value{secnumdepth} < -1
\if@mainmatter
\chaptername\ \thechapter\ --- %
\fi
\fi
##1}{}}
\def\sectionmark##1{\markright{%
\ifnum \value{secnumdepth} < 0
\thesection. \ %
\fi
##1}}
}
\makeatother
The secnumdepth is a counter that contains information about what depth of sections should be numbered. Chapters is equal to secnumdepth 0 and sections to 1, hence the comparisons. Using this in our document we get the following output:
And for those with bad eye-sight, a courtesy close-up of the verso page:
Rather much of an improvement to the earlier AOL user tribute. There are, of course, plenty of other things we could want to do: add a ruler below the header, place the page number in the margin to make it easier to find a page when flipping through a book, or whatever else we might imagine. Even though I’ve carried this post on for quite a while, let us take a brief look at accomplishing this. First we create a page style very much like above:
\makepagestyle{myruledpagestyle}
\makeevenhead{myruledpagestyle}{\thepage}{}{\leftmark}
\makeoddhead{myruledpagestyle}{\rightmark}{}{\thepage}
\makeatletter
\makepsmarks{myruledpagestyle}{
\def\chaptermark##1{\markboth{%
\ifnum \value{secnumdepth} > -1
\if@mainmatter
\chaptername\ \thechapter\ --- %
\fi
\fi
##1}{}}
\def\sectionmark##1{\markright{%
\ifnum \value{secnumdepth} > 0
\thesection. \ %
\fi
##1}}
}
\makeatother
This is almost exactly the same as mypagestyle above, with the only change that we now place the page number and the mark on opposite sides of the header. This leaves us with the task of moving the page number into the margin and adding a ruler below the header. This is fairly easily accomplished:
\makerunningwidth{myruledpagestyle}{1.1\textwidth}
\makeheadposition{myruledpagestyle}{flushright}{flushleft}{flushright}{flushleft}
\makeheadrule{myruledpagestyle}{1.1\textwidth}{\normalrulethickness}
Here \makerunningwidth changes the width of the headers and footers of the specified style. As standard it’s \textwidth long, here we make it 1.1 times longer. \makeheadposition changes the placement of the header and footer. It has the form \makeheadposition{even-head-position}{odd-head-position}{even-foot-position}{odd-foot-position} where the different positions can be chosen between flushleft, center or flushright. This command is only useful, if the width of the page header is different from \textwidth. Finally we apply the actual rule, stating that it should be the width of the header and to use the normal rule thickness. This is, unless you have changed something, typically 0.4pt in LaTeX.
With all this trickery, this is what we get as output:
As an incidental note, the last style presented here is almost equivalent to the Ruled page style that comes with the memoir package (remember casing matters, there is both a ruled and Ruled page style). So if you want exactly this style, use that instead of defining all this yourself.
This fairly much concludes the styling the document series, but I am sure that I will, sooner or later, revisit how to style other parts of the document, or show even more involved styling solutions. But until then, let your imagination run free and bring some personal style to your documents.
Styling the other document divisors
Saturday, June 2nd, 2007 | LaTeX | No Comments
While sectional titles aren’t as stylistically free as the chapter page, it is still important to carry over the style of the chapter page to sections, subsections and other such entries you have throughout your text. Thus, if the chapter style contained sans-serif text and a line below the text, it might be relevant to make the sections be sans-serif and possibly with both a line on top and below the section title.
While memoir is indeed very configurable, it doesn’t contain quite as an elaborate amount of commands to configure the sections, subsections and so forth. The primary command to customise with here is the \setXheadstyle where X can be sec, subsec, or subsubsec (and possibly more). So writing up the section style mentioned above would look like this:
\newcommand{\ruledsection}[1]{%
\noindent%
\parbox[t]{\textwidth}{%
\hrule\vskip1em
\sffamily\Large\bfseries #1\vskip1em%
\hrule%
}
}
\setsecheadstyle{\ruledsection}
Here we set the style to our custom command that will be called with the section number and title as an argument. This gives us the following output when compiled:
Since styling in this way is fairly simple, I will not go into painstriking detail of how to create more section styles. It is, though, useful to note that there are three more useful commands to know of: \setbeforeXskip, \setafterXskip, and \setXindent. The two first commands take lengths such as ‘-3.5ex plus -1ex minus -.2ex’ as arguments, and the last command just takes a regular length as an argument.
To see how these lengths can be used in practice, please refer to the memoir manual.
Styling the table of contents
Saturday, May 26th, 2007 | LaTeX | 2 Comments
Styling the table of contents isn’t quite as trivial as the chapter page, as there are several more things to consider. Before we embark on this, however, let us take a look at how the table of contents are usually set up in a typical LaTeX book.
While this seems to be a fairly tradition-bound way to set up the table of contents, it may feel rather out of pace with the rest of the styling of the document. Or it may just be that you feel more avant-garde than the default LaTeX styles lead people to believe. Either way, restyling the table of contents is just as fascinating as the chapter page!
Something that’s important to keep in mind is that the table of contents, list of figures and other front matter lists are not typeset using the chapter styles we saw in the last post, but using their own setup. The TOC (for instance) is set up as follows in memoir:
\tocheadstart
\printtoctitle{\contentsname}
\tocmark
\thispagestyle{chapter}
\aftertoctitle
And thus, to restyle these front matter lists, you have to also override these commands. For the other lists, replace toc with the appropriate list.
This is, however, not the primary focus of this post, so let’s get on with the show. If we look above at the standard layout of the table of contents we see a couple of recurring items: a page number, a chapter/part/section number, a chapter/part/section title, and a separation between each of these items, which may or may not contain a series of dots. Now, in my personal opinion, the dots are absolutely terrible and ruin the overall aesthetic flow of the page, but that might just be me.
The control over the entries is extremely fine-grained and you can thus define stylings for the following things: part, chapter, section, subsection, subsubsection, paragraph, subparagraph, figure, subfigure, table, and subtable.
To start out we will try something fairly straightforward. We will remove page numbers from the \part entries and typeset them in colour, and we will also move the page numbers for the remaining entries closer to the actual text. This is a bit more involved than just changing the chapter style:
\usepackage{xcolor}
\renewcommand{\cftdotsep}{\cftnodots}
\cftpagenumbersoff{part}
\renewcommand{\cftpartpresnum}{\sffamily\large\bfseries\color{blue}\partname\ }
\renewcommand{\cftpartaftersnumb}{\\}
\cftsetindents{part}{0em}{0em}
\renewcommand{\cftchapterleader}{\hskip1cm}
\renewcommand{\cftchapterafterpnum}{\cftparfillskip}
\renewcommand{\cftsectionleader}{\hskip1cm}
\renewcommand{\cftsectionafterpnum}{\cftparfillskip}
This requires a bit of explanation. The \cftdotsep sets the separator spacing, and \cftnodots is a ‘big enough’ value that we will never see any dots. \cftpartpresnum specifies what goes before the part section number (I, II, etc.). Here we make the font sans-serif, blue and we print \partname (which in English amounts to ‘Part’) and a space after it. \cftpartaftersnumb specifies what happens after the section number block (that’s what the last b is for), and here we print a newline so the part title is displayed below the part name and number. There is also a \cftpartaftersnum where you can, for instance, place a colon so you’ll get something like ‘Part I:’. Lastly we set the indentation for the part lines to 0em leading space to the section number, and 0em subsequential space to the beginning of the title (since we’re printing the title on the following line). The \cftchapterleader contains the code for what happens after the section title and the page number (the default is for it to print the dots or blank space to right-align the page number). We make this empty. If this is all we do we’ll get the title spaced out over the line (like in a bad case of justified paragraphs in Microsoft Word), so we will have to specify that after the page number is printed we want to fill the rest of the line with blank space, we do this using \cftchapterafterpnum and the \cftparfillskip command.
Whew, that was a lot of text. What all this gives us is a table of contents page that looks like this:
While the balance of the page seems a bit more skewed, I actually find this kind of table of contents easier to use as you don’t have to trace across the page to find the right page number, and the dots across the page look even more ugly than this.
As a last practice, let us try another way to setup tables of contents, namely without part, chapter and section numbers. The section number is printed inside a \numberline command for all list types, except for part and chapter, which have their own \partnumberline and \chapternumberline. Thus to create a table of contents that doesn’t contain any of these numbers, we need to override all these commands. Furthermore we’ll keep the page numbers close to the titles like in the previous example, and no page number for the part. This amounts to the following:
\renewcommand{\cftdotsep}{\cftnodots}
\cftpagenumbersoff{part}
\renewcommand{\partnumberline}[1]{}
\renewcommand{\chapternumberline}[1]{}
\renewcommand{\cftchapterleader}{\hskip1cm}
\renewcommand{\cftchapterafterpnum}{\cftparfillskip}
\renewcommand{\numberline}[1]{}
\renewcommand{\cftsectionleader}{\hskip1cm}
\renewcommand{\cftsectionafterpnum}{\cftparfillskip}
This gives us the following output:
Depending on your need the section number-less style can be preferable while retaining part, chapter and section numbers inside the actual document. If none of these are needed you can use the \setsecnumdepth macro instead and just turn off the numbers everywhere. That’s a lot more easy.
As a rule of thumb, it is important to remember to keep fonts consistent between section numbers, section titles and page numbers. Mixing and matching makes for a very jumbled expression and is (for the most part) not what you’re interested in.
Again, for more technical details, consult the memoir manual or class file.
Styling the chapter
Monday, May 21st, 2007 | LaTeX | 13 Comments
When we write larger works, one of the places we have the most stylistic freedom is with chapter pages. The chapter page is meant to break off the flow of pages and present something new. Looking through books on the book-case we see a wide-ranging difference in chapter styles. While the standard chapter style (as seen below) is pretty decent, there’s a long way to the chapter styles of books like Unicode Standard 5.0, A History of Mathematics, or Fundamentals of Human Neuropsychology.
There are plenty of different styles we can think up to create on our own, or by mimicking others’ designs (do note that exactly reproducing someone else’s design is probably a copyright infringement in plenty of places. Yay for copyright law). So instead of showing you just one style, I will try to explain the fundamentals of chapter styling by looking at the commands used, and then giving several examples ranging from the simple to the quite complex.
Before we continue, it would probably be prudent for me to point out that LaTeX contains two kinds of chapters: numbered (the normal chapters) and unnumbered (the table of contents, the bibliography, the index, and chapters placed in the front matter). The main difference is, of course, that numbered chapters contain the text ‘Chapter 1’ (for chapter one, obviously), and unnumbered chapters do not. At least in the default setup.
In memoir, the default numbered chapter is defined as written below (from page 86 of the manual):
\chapterheadstart
\printchaptername \chapternamenum \printchapternum
\afterchapternum
\printchaptertitle{Title goes here}
\afterchaptertitle
In their default definitions the print methods also make use of the following commands that define font settings: \chapenamefont, \chapnumfont, and \chaptitlefont. This information is almost all we need in order to create our own fantastic chapter styles. The last thing before we embard on experimenting with styles is to know how to create and use different chapter styles. This is accomplished using the \makechapterstyle{stylename}{commands} command, where commands is a series of redefinitions of the above-mentioned commands, and in order to use this style, we can use the command \chapterstyle{stylename}.
As a very easy beginning, let’s add some colour to the chapter style, by creating a ‘colour’ chapter style:
\usepackage{xcolor}
\makechapterstyle{colour}{
\renewcommand*{\chapnamefont}{\normalfont\huge\bfseries\color{blue}}
\renewcommand*{\chaptitlefont}{\normalfont\Huge\bfseries\color{blue}}
}
\chapterstyle{colour}
This style only redefines the fonts and leaves the rest of the chapter style intact as default. If we only had numbered chapters then redefining the \chapnamefont would be adequate, as the colour seeps through to the other commands, but since \chapnamefont is never called for unnumbered chapters, we also need to provide the colour for the \chaptitlefont command. For those not deeply into the LaTeX command architecture, then the star after \newcommand and \renewcommand means that the content of the command cannot contain a paragraph change, so for simple inline commands, tag the star on for verification purposes. Adding our new chapter style to our document we get the following page:
This, actually, did little to make the chapter style prettier, if I have to be a bit self-critical for a moment. Let us instead try to be a tad more artistic with our usual black and white palette. Using the tikz package that I have written about before, we can draw a black box with the chapter number inside and then have the title below, and this time let us make it right-aligned.
\usepackage{tikz}
\makechapterstyle{box}{
\renewcommand*{\printchaptername}{}
\renewcommand*{\chapnumfont}{\normalfont\sffamily\huge\bfseries}
\renewcommand*{\printchapternum}{
\flushright
\begin{tikzpicture}
\draw[fill,color=black] (0,0) rectangle (2cm,2cm);
\draw[color=white] (1cm,1cm) node { \chapnumfont\thechapter };
\end{tikzpicture}
}
\renewcommand*{\chaptitlefont}{\normalfont\sffamily\Huge\bfseries}
\renewcommand*{\printchaptertitle}[1]{\flushright\chaptitlefont##1}
}
This probably requires a bit more explanation. We disable printing ‘Chapter’ for the chapter style by overriding the \printchaptername command with an empty body. Then we change the font for the chapter number to be sans-serif (\sffamily), and the same for the chapter title. Also we ensure that the chapter title is flushed right, that is it aligns to the right margin. Lastly we have the tikzpicture in the \printchapternum. Inside that we draw a black triangle that’s a 2cm square, and after that we print the chapter number in the middle of it. Fairly straightforward, eh? Using this chapter style in your document yields the following output:
This is, indeed, a lot better, but there are plenty of other options for chapter styles to try out. We can combine some of the concepts in the last few chapter styles we’ve tried:
\usepackage{xcolor,calc}
\makechapterstyle{combined}{
\setlength{\midchapskip}{-60pt}
\setlength{\afterchapskip}{2.5cm}
\renewcommand*{\printchaptername}{}
\renewcommand*{\chapnumfont}{\normalfont\sffamily\bfseries\fontsize{80}{0}\selectfont}
\renewcommand*{\printchapternum}{\flushright\chapnumfont\textcolor[rgb]{.64,.79,.87}{\thechapter}}
\renewcommand*{\chaptitlefont}{\normalfont\sffamily\Huge\bfseries}
\renewcommand*{\printchaptertitle}[1]{%
\raggedright\chaptitlefont\parbox[t]{\textwidth-3cm}{\raggedright##1}}
}
\chapterstyle{combined}
First we set up some distances. The -60pt fits approximately on eye measurement the amount that you need to back up for the chapter number and title to align at the top. Normally it would be better to redefine more of the chapter style to create explicit boxes for the two things that could be top-aligned, but that’s a lot more work and would actually obscure how to use the chapter styles easily, so eye measurement it is. We make the chapter number really big (80 pt to be exact), and we print it in the RGB value (0.64, 0.79, 0.87) where colours are in [0;1] for those of mathematical inclination. Lastly we print the chapter title inside a paragraph box that is 3 cm smaller than the text width in order not to have the chapter title written on top of the chapter number as that doesn’t look particularly smashing. Adding this to our document gives us the following output:
We have now seen several different kinds of chapter styles that can be (more or less) easily customised using memoir’s functionality. Creating your own chapter style is thus just a matter of overriding a couple of methods and inserting some code. Doing this gives a tremendous difference between a cookie-cutter template of a document and a customised, personal document of high quality. For those of you who need even more examples of different chapter styles, you can refer to the Memoir Chapter Style Samples document by Lars Madsen from Århus universitets institut for matematiske fag. But the most important aspect is, of course: experiment.
In closing, these are the relevant items to override in the standard setup. You may change commands so some of them become irrelevant or not used.
- Lengths:
\beforechapskip,\midchapskip, and\afterchapskip - Fonts:
\chapnamefont,\chapnumfont, and\chaptitlefont - Printing text:
\printchaptername,\printchapternum, and\printchaptertitle - More technical surrounding blocks:
\chapterheadstart,\afterchapternum,\printchapternonum, and\afterchaptertitle
For more technical details, consult the memoir manual or class file.
Styling the document
Monday, May 21st, 2007 | LaTeX | No Comments
Over the next few blog posts we will be looking at how we can restyle an entire document without changing its contents. This is one of the key strengths of LaTeX: separating content from style. There are, of course, several aspects of a document that can be interesting to restyle, but we will focus on some of the primary elements: the table of contents, chapter headings, part, section, subsection, etc. headings, and finally the page headers and footers. As usual, we will do this by investigating the memoir document class.
Post 0: This post
Post 1: Styling the chapter
Post 2: Styling the table of contents
Post 3: Styling the other document divisors
Post 4: Styling the page footer and header
Initials and the saints
Wednesday, November 15th, 2006 | LaTeX | No Comments
Taking a short intermission from the chapter styling, we take the time to look at a time-honoured tradition in the western world, the initial, or drop capital, or lettrine, or uncial, or…. There are, indeed, many forms and variations of these majuscule letters at the beginning of a paragraph, but they still crop up here and there in books everywhere. So, since LaTeX is supposed to be this fantastic typography and typesetting tool, let’s have at it then.
Since my artistic skills for drawing custom initials are fairly limited, I will illustrate the functionality by using the initials from House of Lime, who provides linkware TrueType fonts that you can use (be sure to check his policy before you use them, though). Otherwise several other type foundries have professionel initial fonts that you can purchase.
There are two primary packages for doing initials (unless you want to code it all yourself, but we’ll save that for some other time), namely dropping and lettrine, where the latter is the more feature complete one, so we’ll be using that.
Using Fleur Corner Caps as our initial typeface, we can construct a simple document like this (we’ll cover how to get LaTeX to use the font at a later date):
\documentclass[article,12pt,oneside]{memoir} \usepackage[T1]{fontenc} \usepackage[garamond]{mathdesign} \DeclareTextFontCommand{\textfleur} {\fontencoding{T1}\fontfamily{FleurCornerCaps}\selectfont} \usepackage{lettrine} \pagestyle{plain} \begin{document} \lettrine[lines=7,findent=0.5em,loversize=-0.15,nindent=0em]% {\textfleur{L}}{orem ipsum} dolor sit amet... \end{document}
What this means is that we create an initial that occupies 7 lines, where the first line is indented by 0.5em, next lines are indented 0em further, and it’s lowered a bit by the negative amount specified with loversize. The first argument to the lettrine command is the initial, the second argument the following text that is typeset in smallcaps. This is rendered as follows (click on the image for the original PDF):
While using a typeface to provide the initial is all fine and dandy this black and white text gets a bit boring. After all, when we look at some of the medieval initials, they are a tad more colourful:
This cannot, of course, be accomplished by fonts, so we’ll have to resort to using an image of some sort. Fear not, however, for the lettrine package also supports this! (Amazing, yes). Using my extensive Photoshop skills, I have coloured one of House of Lime’s initials, namely the L from Flower and Fairy Alphabet. In order to use the image I have prepared, we add an option to the lettrine command stating to use an image, and the first argument becomes the filename of an image instead of a letter. In the version of the lettrine package I have, there is unfortunately the omission in the package that requires you to include the graphicx package manually, but fortunately this is rather easy. The modified code looks like this:
\documentclass[article,12pt,oneside]{memoir} \usepackage[T1]{fontenc} \usepackage[garamond]{mathdesign} \usepackage{graphicx} \DeclareTextFontCommand{\textfleur} {\fontencoding{T1}\fontfamily{FleurCornerCaps}\selectfont} \usepackage{lettrine} \pagestyle{plain} \begin{document} \lettrine[lines=7,findent=0.5em,loversize=-0.15,nindent=0em,image=true]% {fairy}{orem ipsum} dolor sit amet...
The fairy argument is, indeed, a filename of a PDF-file:
When the document is compiled, we get the following (click on the image for the original PDF):
While this precise image is probably better suited for a children’s fairy tale book than a bunch of lorem ipsum, it illustrates the versatility of the lettrine package. For those who wish to experiment even more, the lettrine manual contains descriptions of even more features and functionality, including using the colour packages to colour the initial, globally setting these settings (the ones in [...]) for all lettrines in the document and much, much more.
Last, a small caveat, the initial packages work by computing a box and placing it on the page, meaning that if you have an initial at the bottom of a page, it will hang into the footer rather than break properly to the next page. The only way to solve this is by manually inserting page breaks. This is, of course, a pity to do, so most people settle with using initials at the beginning of a chapter. This seems to work well, in my experience, as using them more often tends to overdo the effect to the point of it becoming vulgar. So don’t overdo it.
Categories
Archives
- February 2012
- July 2011
- June 2011
- November 2010
- October 2010
- April 2010
- November 2009
- October 2009
- June 2009
- May 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- December 2006
- November 2006
- October 2006
- September 2006
- June 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- June 2004
- April 2004
- February 2004
- November 2003
- January 2003
- November 2002

