<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stuart&#039;s Soliloquy to the void &#187; LaTeX</title>
	<atom:link href="http://hstuart.dk/category/latex/feed/" rel="self" type="application/rss+xml" />
	<link>http://hstuart.dk</link>
	<description>A blog on development, baking and the universe.</description>
	<lastBuildDate>Sun, 13 May 2012 21:46:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=0</generator>
		<item>
		<title>Readability</title>
		<link>http://hstuart.dk/2008/02/13/readability/</link>
		<comments>http://hstuart.dk/2008/02/13/readability/#comments</comments>
		<pubDate>Wed, 13 Feb 2008 13:02:49 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[readability]]></category>
		<category><![CDATA[typeface]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2008/02/13/readability/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>Since TeX was Donald Knuth&#8217;s fix to the ugly digital typesetting of his books, <em>The Art of Computer Programming</em>, 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&#8217;s <em>The TeXbook</em> simply states, ‘Go forth now and create <em>masterpieces of the publishing art</em>!’, 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.</p>
<p>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 <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=12">microtypography</a>, 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.</p>
<p>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.</p>
<div class="center" style="margin-top: 1em; margin-bottom: 1em;">
  <img src="http://hstuart.dk/images/lblog/readability/widths.png" alt="Width of lower-case English letters for different fonts" title="Width of lower-case English letters for different fonts" />
</div>
<p>‘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 <code>\usepackage{times}</code>. 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.</p>
<p>‘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.</p>
<p>‘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.</p>
<p>‘URW Garamond No8’ is an adaption of 16th century type cutter Claude Garamond&#8217;s typeface. The ‘Garamond’ is one of the most often-used type families when it comes to professional works, owing in particular to Slimbach&#8217;s beautiful digitalisation in Adobe&#8217;s Garamond Pro typeface. URW&#8217;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 <a href="http://www.ctan.org">CTAN</a> 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: <code>\renewcommand{\rmdefault}{ugm}</code>.</p>
<p>The last typeface we will look at is, ‘URW Palladio L Roman’, which is an adaption of Hermann Zapf&#8217;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 <em>mathpazo</em>. 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’).</p>
<p>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&#8230; 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).</p>
<p>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.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/readability/comp1.png" alt="Comparison between Nimbus Roman No9 and Computer Modern Roman" title="Comparison between Nimbus Roman No9 and Computer Modern Roman" />
</div>
<p>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.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/readability/comp2.png" alt="Comparison between SIL Gentium and URW Garamond No8" title="Comparison between SIL Gentium and URW Garamond" />
</div>
<p>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.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/readability/comp3.png" alt="Comparison between Nimbus Roman No9 and URW Palladio L Roman" title="Comparison between Nimbus Roman No9 and URW Palladio L Roman" />
</div>
<p>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.</p>
<p>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 <em>a4wide</em> 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:</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/readability/nw.png" alt="Standard margin text" title="Standard margin text" /><br />
  <img src="http://hstuart.dk/images/lblog/readability/w.png" alt="a4wide margin text" title="a4wide margin text" />
</div>
<p>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.</p>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2008/02/13/readability/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>BibTeX</title>
		<link>http://hstuart.dk/2008/02/10/bibtex/</link>
		<comments>http://hstuart.dk/2008/02/10/bibtex/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 20:14:00 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[bibliography]]></category>
		<category><![CDATA[BibTeX]]></category>
		<category><![CDATA[publishing]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2008/02/10/bibtex/</guid>
		<description><![CDATA[In academia one of the most important points in any work that you want to have published is citing other works that you have used to reach your conclusions, or works whose conclusions that you have sought to disprove in your work. Doing this (hopefully) confers credibility to your work as well. As with most [...]]]></description>
			<content:encoded><![CDATA[<p>In academia one of <em>the</em> most important points in any work that you want to have published is citing other works that you have used to reach your conclusions, or works whose conclusions that you have sought to disprove in your work. Doing this (hopefully) confers credibility to your work as well. As with most other things in academia, there is an approach to cite and reference works that you use. Namely, the approach is that pretty much everyone is doing their own thing. This means that the same work may have to be formatted radically different depending on whether you are publishing your work with publisher A, B, or C, nevermind it is, in fact, the same work you are citing.</p>
<p>To most people, this situation is, of course, untenable, and since there usually are rules from each publisher that describe how references should be written down, it is almost apparent that you should be able to use some kind of software that automates this process for you. In fact, there are many software packages doing exactly this, with EndNote and RefWorks being some of the more well-known commercial packages if you are a Microsoft Word user. Fortunately, we are not! So for us the main contender to do bibliographies with LaTeX is BibTeX (there are several other tools, but BibTeX is the most often-used tool for this purpose, so we will focus on that).</p>
<p>Before we take a look at BibTeX, let us, for good measure and for those not too corrupted by academia yet, do a quickly summarise some of the different ways to create a bibliography and the corresponding ways to cite this work inside the text. Our example work will be the book <em>Design Patterns: Elements of Reusable Object-Oriented Software</em> by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, published by Addison-Wesley in 1995 with an ISBN 0-201-63361-2. Each pair of lines in the image below will show how it is (or can be) referenced inside the text, and the other line will show how it is listed in the bibliography—the list at the end of your work that gives a thorough list of the works you have cited in your text.</p>
<div style="margin-left: 0.5cm;">
<div style="margin-bottom: 1em;">
<p><b>Chicago Manual of Style, 13th Edition style:</b><br />
    (Gamma, Helm, Johnson, and Vlissides 1995)<br />
    Gamma, E., R. Helm, R. Johnson, and J. Vlissides (1995). <em>Design Patterns: Elements of Reusable Object-Oriented Software</em>. Addison-Wesley.
    </p>
</div>
<div style="margin-bottom: 1em;">
<p><b>Plain LaTeX style:</b></p>
<p>    [1]<br />
    [1] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. <em>Design Patterns: Elements of Reusable Object-Oriented Software</em>. Addison-Wesley, 1995.
    </p>
</div>
<div style="margin-bottom: 1em;">
<p><b>Alpha LaTeX style:</b><br />
    [GHJV95]</p>
<p>    [GHJV95] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. <em>Design Patterns: Elements of Reusable Object-Oriented Software</em>. Addison-Wesley, 1995.
    </p>
</div>
<div style="margin-bottom: 1em;">
<p><b>Abbreviated LaTeX style:</b><br />
    [1]<br />
    [1] E. Gamma, R. Helm, R. Johnson, and J. Vlissides. <em>Design Patterns: Elements of Reusable Object-Oriented Software</em>. Addison-Wesley, 1995.
    </p>
</div>
</div>
<p>And there are many, many, many more variants than these to choose from, but it gets a bit tiresome to write them all out. To the first of these (the Chicago Manual of Style styled citation) there is of course also an alternative contextual representation that goes like this: ‘Gamma, Helm, Johnson, and Vlissides (1995)’ so that you can use it as part of a normal sentence. And there are more rules governing these things when you add more information to the citation, like a page number/range or a chapter. Verily it is much better to let a program take care of all these technicalities than have to remember them yourself.</p>
<p>Before we move on to BibTeX and how it works, it might be prudent to take a quick look at how we can create bibliographies manually in LaTeX, as BibTeX actually just automates creating bibliographies for us.</p>
<p>The environment for a bibliography in LaTeX is simply called <em>thebibliography</em>. It works very much like an <em>enumerate</em> environment, but with a few more bells and whistles and some slightly different command names. To see how it works, let us just look at a full example up-front:</p>
<pre><span class="keyword">\begin</span>{thebibliography}{1}

<span class="keyword">\bibitem</span>{DP} Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
  <span class="keyword">\textit</span>{Design Patterns: Elements of Reusable Object-Oriented Software}.
  Addison-Wesley, 1995.
<span class="keyword">\end</span>{thebibliography}
</pre>
<p>So let us cover this one step at a time. The <code>{1}</code> after the <em>thebibliography</em> is the visual key of the widest element in the bibliography list. Since we only have a single entry and we&#8217;re creating a bibliography after the plain LaTeX style above, this is 1. If we were using the alpha style, it would have been <code>{GHJV95}</code> instead. Instead of <code>\item</code> we have <code>\bibitem</code> and it takes a key that you will be using to refer to that work when you cite something. Citing is done with the <code>\cite</code> command, so to cite the <em>Design Patterns</em> book, you would write <code>\cite{DP}</code>. The rest of the text is merely a standard list text and nothing fancy happens to that. We will cover the <code>\cite</code> command in more detail later on.</p>
<p>With this in mind, it is time to move on to BibTeX. BibTeX uses a bibliography file that is provided by the user with a .bib suffix, a style file that ends in a .bst suffix, this file is typically chosen from the large set of existing style files, and finally the actual BibTeX program that understands the .bib file format and the .bst file format and processes them together to create a .bbl file. On a subsequent run, LaTeX reads in the .bbl file, which contains a finished <em>thebibliography</em> environment. This may seem convoluted, but remember that TeX is an old program from long before we measured system resources in gigabytes, so this was the only really tenable solution for integrating new tools back then.</p>
<p>Let us start at the .bib file format and take a look at how this is organised.</p>
<pre>@worktype{reference-key,
  key_1 = value_1,
  key_2 = value_2,
  ...
  key_n = value_n
}

...
</pre>
<p>Here the worktype specifies the type of work, for example article, phdthesis, inproceedings, and many other types. It is the .bst file that defines what worktypes are supported. Likewise the keys are dependent on the worktype and are also defined by the .bst file. Normally a lot of these worktypes and keys will be the same across different .bst files, so it is quite normal that all of them has support for an article and support for keys such as author, title, publisher, year, month, isbn, etc. The values are, on the other hand, are a lot less well-defined and there are several ways to write them. For our initial purposes, though, it will most likely be sufficient to write values like this: <code>{value test}</code>. So to get a feeling of how this all will work out for our book reference above, let us write it up in BibTeX:</p>
<pre>@book{DP,
  author = {Erich Gamma and Richard Helm and Ralph Johnson and John Vlissides},
  title = {Design Patterns: Elements of Reusable Object-Oriented Software},
  publisher = {Addison-Wesley},
  year = {1995},
  isbn = {0-201-63361-2}
}
</pre>
<p>Authors here are merely delimited with the keyword <em>and</em>, which allows BibTeX to automatically figure out the things it needs about the authors. Do note that BibTeX has some weaknesses in guessing where to break some names, like Domonique Galouzeau de Villepin, whose first name is Dominique and last name is Galouzeau de Villepin. If entered as ‘Dominique Galouzeau de Villepin’ in BibTeX as-is, it would see ‘Dominique Galouzeau’ as first name, ‘de’ as a delimiter for the last name and ‘Villepin’ as the last name. To disambiguate this you can also enter names in BibTeX like this: ‘Galouzeau de Villepin, Dominique’. For many more details on the finer trickeries of BibTeX you may want to refer to Xavier Décoret&#8217;s excellent <a href="http://artis.imag.fr/%7EXavier.Decoret/resources/xdkbibtex/bibtex_summary.html">summary</a>.</p>
<p>For now, let us return to the problem at hand: using this .bib file with our text in LaTeX. As before, <code>DP</code> is the key of the work, so inside our LaTeX text we can just refer to the work using the command <code>\cite{DP}</code>. So what do we use to hook us up to the .bib and .bst files? This is, in fact, very easy, it just requires two commands:</p>
<pre><span class="keyword">\bibliographystyle</span>{plain}
<span class="keyword">\bibliography</span>{myfile}
</pre>
<p>So the argument for <code>\bibliographystyle</code> is the .bst file without the .bst suffix, and the argument for <code>\bibliography</code> is the name of your .bib file, likewise without the .bib suffix. If we presume that you use pdflatex to compile your LaTeX documents to PDF, this is a typical run of what you would do when you use BibTeX:</p>
<ol>
<li>pdflatex mytexfile.tex
<p>This generates mytexfile.aux with special bibliography commands that BibTeX will process.</p>
</li>
<li>bibtex mytexfile
<p>This is done without any suffix to mytexfile. What it does is that it reads the generated .aux file for bibliography commands in order to figure out what .bst and .bib files to use. Once it finds these, it generates a .bbl file with a finished <em>thebibliography</em> environment.</p>
</li>
<li>pdflatex mytexfile.tex
<p>This includes the contents of the .bbl file at the location where you have the <code>\bibliography</code> command and writes citation location information to the .aux file.
  </p>
</li>
<li>pdflatex mytexfile.tex
<p>Like the previous step, except now the citation location informations are available in the .aux file, so the <code>\cite</code> commands can be translated to an actual reference with the relevant text.</p>
</li>
</ol>
<p>In some rarer cases you may need another run of pdflatex to sort out some kinds of references, but the TeX engine will generally let you know if it needs to be run again to resolve references. There is, of course, no real need to understand all these technicalities. In practice you will just run pdflatex once, then bibtex and then pdflatex a number of times until LaTeX stops complaining about references. The important thing is that you can now just replace <code>\bibliographystyle{plain}</code> with <code>\bibliographystyle{alpha}</code> if that is what suits your fancy, and run pdflatex and bibtex again as above. Do note that in some cases you may need to delete the .aux and .bbl files in order to be able to compile again after you have changed to a new bibliography style, as some bibliography styles use special-purpose commands that may be removed by your changes. This will, however, usually not be a problem in practice unless you vacillate endlessly between bibliography styles, but who does that, really? (The author looks around suspiciously).</p>
<p>Citing works is fairly easy by just doing <code>\cite{work}</code>, but what if we want to indicate that we are only really referencing a single page or chapter in the work. We could of course write something like: ‘See chapter 1 in \cite{work}’, but unfortunately for us, there are also guidelines governing how to write these things in relations to citations by the different publishers, so we will need to come up with another approach. Fortunately, <code>\cite</code> takes an optional argument that tells us exactly this: <code>\cite[info]{work}</code>. So, for instance, referring to page 120 of <em>Design Patterns</em> would be written like this: <code>\cite[p.~120]{DP}</code>. Note the non-breaking space, ~, which makes sure that there isn&#8217;t a line break between the ‘p.’ and the ‘120’, which would look fairly ugly in most cases and disrupt the general flow of reading. It is also customary to place a non-breaking space before the <code>\cite</code> command if it isn&#8217;t the first thing in a sentence, e.g.: ‘&#8230; as described in~\cite[p.~120]{DP}’.</p>
<p>In order to get really going with using BibTeX now, you only really need to know the standard worktypes and keys. Fortunately, Wikipedia provides a nice, if short, overview of them <a href="http://en.wikipedia.org/wiki/BibTeX">here</a>.</p>
<p>That&#8217;s basically what there is to using BibTeX for most purposes. Hopefully I will find the time to write something on creating custom .bst files later on, as those are fairly interesting as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2008/02/10/bibtex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microtypography</title>
		<link>http://hstuart.dk/2007/11/27/microtypography/</link>
		<comments>http://hstuart.dk/2007/11/27/microtypography/#comments</comments>
		<pubDate>Tue, 27 Nov 2007 16:37:59 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[kerning]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/11/27/microtypography/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>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).</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/microtypography/oo.png" alt="OpenOffice typesetting" />
</div>
<p>Looking at the interword spaces on line three, we see that they&#8217;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.</p>
<p>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).</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/microtypography/basic.png" alt="Basic LaTeX typesetting" />
</div>
<p>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.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/microtypography/protrude1.png" alt="Simple character protrusion" />
</div>
<p><a href="http://www.pdftex.org">Pdftex</a> 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&#8217;s a much larger space in the left column after the full stop.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/microtypography/protrude2.png" alt="Comparison of character protrusion levels 1 and 2" />
</div>
<p>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 <em>‘At first I could not believe my’</em>. 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.</p>
<p>Some of the lines are spaced too tightly in my opinion in the right column above, example the line reading <em>‘my very feelings changed to repulsion and ter-’</em> 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.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/microtypography/expand.png" alt="Full character protrusion and font expansion" />
</div>
<p>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.</p>
<div class="center">
  <img src="http://hstuart.dk/images/lblog/microtypography/full.png" alt="Complete example" />
</div>
<p>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: <em>microtype</em>. Using it is exceptionally easy:</p>
<pre><span class="keyword">\usepackage</span>{microtype}
</pre>
<p>This will enable both character protrusion and font expansion. For more advanced manipulative changes, you can refer to <a href="http://www.ctan.org/tex-archive/help/Catalogue/entries/microtype.html">microtype&#8217;s</a> documentation. (There is even a way to make it look like it was created in Word or OpenOffice!)</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/11/27/microtypography/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Due to lack of time&#8230;</title>
		<link>http://hstuart.dk/2007/09/22/due-to-lack-of-time/</link>
		<comments>http://hstuart.dk/2007/09/22/due-to-lack-of-time/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 09:54:19 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/09/22/due-to-lack-of-time/</guid>
		<description><![CDATA[Due to a great shortage of time on my part, due to work and writing my thesis, this blog hasn&#8217;t received the attention it deserves. I, therefore, regrettably have to put it on hiatus for the time being. I hope to find time to update it again, eventually, but do not dare to hope for [...]]]></description>
			<content:encoded><![CDATA[<p>Due to a great shortage of time on my part, due to work and writing my thesis, this blog hasn&#8217;t received the attention it deserves. I, therefore, regrettably have to put it on hiatus for the time being. I hope to find time to update it again, eventually, but do not dare to hope for it.</p>
<p>Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/09/22/due-to-lack-of-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming packages</title>
		<link>http://hstuart.dk/2007/08/11/programming-packages/</link>
		<comments>http://hstuart.dk/2007/08/11/programming-packages/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 08:00:36 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/08/11/programming-packages/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we have seen how to both write <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=1">commands</a> and <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=2">environments</a>, 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 <a href="http://www.ctan.org">CTAN</a>, and we have seen several of them already: <em>tikz</em>, <em>graphicx</em>, <em>fontenc</em>, <em>mathdesign</em>, and many others. Common to each package is that it provides a number of commands and environments that gives you some form of functionality. <em>Tikz</em> allows us to create vector-based drawings directly from inside LaTeX, <em>graphicx</em> allows us to interface with a number of existing graphics formats, <em>mathdesign</em> provides new math fonts that correspond to a number of free and non-free serif fonts.</p>
<p>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).</p>
<p>Every package starts with either the first or both of the following lines:</p>
<pre><span class="keyword">\ProvidesPackage</span>{mypackage}[2006/05/01 version 1.0 by Someone]
<span class="keyword">\NeedsTeXFormat</span>{LaTeX2e}
</pre>
<p><code><span class="keyword">\ProvidesPackage</span></code>&#8216;s name is required to be the same as the file of the package, and <code><span class="keyword">\NeedsTeXFormat</span></code> 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.</p>
<p>Packages can, of course, also depend on other packages, but unlike in the main document, we do not use <code><span class="keyword">\usepackage</span></code> to include these, rather, a separate command, <code><span class="keyword">\RequirePackage</span></code> 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 <em>ifthen</em> package, we could use the following line of code inside our package:</p>
<pre><span class="keyword">\RequirePackage</span>{ifthen}
</pre>
<p>With some packages it is also possible to pass them options, for instance for the <em>babel</em> package that takes care of renaming various commands to a specific language:</p>
<pre><span class="keyword">\usepackage</span>[british,french]{babel}
</pre>
<p>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 <code><span class="keyword">\DeclareOption</span></code> command. So if we were to declare a <em>theorem</em> option, it might look like this:</p>
<pre><span class="keyword">\DeclareOption</span>{theorem}{<span class="comment">%</span>
  <span class="comment">% code goes here</span>
}
</pre>
<p>And when we subsequently use the package in a document we can write:</p>
<pre><span class="keyword">\usepackage</span>[theorem]{mypackage}
</pre>
<p>All the options aren&#8217;t executed at the point of declaration though, it requires a separate command, <code><span class="keyword">\ProcessOptions</span></code> that can occur anywhere in the package code (though naturally after the <code><span class="keyword">\DeclareOption</span></code> 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.</p>
<p>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 <code><span class="keyword">\makeatletter</span></code> and <code><span class="keyword">\makeatother</span></code>. Also, remember that commands must be unique, there are no overloading, so try to pick names that don&#8217;t conflict with other packages. Of course, given the sheer number of packages in existence, this may prove rather difficult.</p>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/08/11/programming-packages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming LaTeX — writing environments</title>
		<link>http://hstuart.dk/2007/08/04/programming-latex-%e2%80%94-writing-environments/</link>
		<comments>http://hstuart.dk/2007/08/04/programming-latex-%e2%80%94-writing-environments/#comments</comments>
		<pubDate>Sat, 04 Aug 2007 18:00:34 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/08/04/programming-latex-%e2%80%94-writing-environments/</guid>
		<description><![CDATA[We have previously seen how to create commands in LaTeX. Today, we will be looking at how we can create environments, that is the things that are typically started with a \begin and ended with an \end command. In the course of writing a document with a barebones LaTeX setup, we may see several of [...]]]></description>
			<content:encoded><![CDATA[<p>We have previously seen <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=1">how to create commands</a> in LaTeX. Today, we will be looking at how we can create environments, that is the things that are typically started with a <code><span class="keyword">\begin</span></code> and ended with an <code><span class="keyword">\end</span></code> command. In the course of writing a document with a barebones LaTeX setup, we may see several of these environments: <em>document</em>, <em>figure</em>, <em>table</em>, <em>itemize</em> and <em>enumerate</em>. Each of these environments dictates some form of structure that helps abstract the actual layout of your document from the contents and structure of your document, an important tenant in the LaTeX world.</p>
<p>We can, for instance, imagine that we are writing a document where all figures are centered before they are drawn. In many cases this is accomplished by people using roughly the following code:</p>
<pre><span class="keyword">\begin</span>{figure}
  <span class="keyword">\begin</span>{center}
    <span class="comment">% figure code goes here</span>
  <span class="keyword">\end</span>{center}
  <span class="keyword">\caption</span>{...}
  <span class="keyword">\label</span>{...}

<span class="keyword">\end</span>{figure}
</pre>
<p>But with this, we have mixed together structure and layout, so let us drag the author out back and shoot him before it is too late. Rather, what we would want to do is somehow create a new environment, say <em>myfigure</em>, that takes care of centering the contents. In order to introduce a new environment, we may use the <code><span class="keyword">\newenvironment</span></code> command, which functions very much like <code><span class="keyword">\newcommand</span></code> except that it takes two arguments: what happens before the content, and what happens after the content. So using this knowledge, we can wrap up the centering code:</p>
<pre><span class="keyword">\newenvironment</span>{myfigure}{
  <span class="keyword">\begin</span>{figure}<span class="keyword">\begin</span>{center}
}{
  <span class="keyword">\end</span>{center}<span class="keyword">\end</span>{figure}
}
</pre>
<p>Do note that the order of appearance of the <em>figure</em> and <em>center</em> environments matter. If you try to end a <em>figure</em> environment with a <em>center</em> environment, LaTeX will throw errors your way. With our brand new environment, we can now write our figure using our own environment:</p>
<pre><span class="keyword">\begin</span>{myfigure}
  <span class="comment">% figure code goes here</span>
  <span class="keyword">\caption</span>{...}
  <span class="keyword">\label</span>{...}
<span class="keyword">\end</span>{myfigure}
</pre>
<p>Thus, we nicely separate our layout and our structure. Normally it would seem prudent to find a better name than <em>myfigure</em>, one that better describes the purpose of the structural component, but sometimes no nifty names come to mind.</p>
<p>Like with commands, environments can also be given a number of parameters. These parameters are, however, only available in the ‘before content’ code. If we often customise our <em>itemize</em> environments to use different kinds or itemisation symbols, we could create an environment that lets us specify these easily:</p>
<pre><span class="keyword">\makeatletter</span>
<span class="keyword">\newenvironment</span>{myitemize}[1]{
  <span class="keyword">\begin</span>{itemize}
    <span class="keyword">\expandafter</span><span class="keyword">\renewcommand</span><span class="keyword">\expandafter</span>{
      <span class="keyword">\csname</span> labelitem<span class="keyword">\romannumeral</span><span class="keyword">\the</span><span class="keyword">\@itemdepth</span><span class="keyword">\endcsname</span>}{#1}
}{
  <span class="keyword">\end</span>{itemize}
}

<span class="keyword">\makeatother</span>
</pre>
<p>Now we can use this to change the item symbol like this:</p>
<pre><span class="keyword">\begin</span>{myitemize}{$<span class="keyword">\star</span>$}
  <span class="keyword">\item</span> Test
<span class="keyword">\end</span>{myitemize}
</pre>
<p>And there would be a nice little star to the left of ‘Test’.</p>
<p>This is basically what there is to environments at the surface. This leaves the technical details of how they are actually implemented (as they are not a part of plain TeX). In fact, <code><span class="keyword">\newenvironment</span></code> is just a cover around creating two different commands: <code><span class="keyword">\environmentname</span></code> and <code><span class="keyword">\endenvironmentname</span></code>. Here the parameters are passed to the <code><span class="keyword">\environmentname</span></code> exclusively, as that is typically where they are needed. Now, invoking <code><span class="keyword">\begin</span>{environmentname}</code> is not entirely the same as invoking <code><span class="keyword">\environmentname</span></code> as <code><span class="keyword">\begin</span></code> also sets up some other variables and starts a new group (scope in regular programming).</p>
<p>The primary goal of using environments, though, is to separate the content from the layout. If you keep this in mind when creating documents it will be extremely easy to tweak the layout without having to go through the entire document every time you wish to change all your figures to be centered, for instance.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/08/04/programming-latex-%e2%80%94-writing-environments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming LaTeX — writing commands</title>
		<link>http://hstuart.dk/2007/08/03/programming-latex-%e2%80%94-writing-commands/</link>
		<comments>http://hstuart.dk/2007/08/03/programming-latex-%e2%80%94-writing-commands/#comments</comments>
		<pubDate>Fri, 03 Aug 2007 15:04:31 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/08/03/programming-latex-%e2%80%94-writing-commands/</guid>
		<description><![CDATA[Most typesetting software lets the user operate within its functionality and thorough programming knowledge is required to write any form of extension to the system, typically in the form of a module written in C. With LaTeX this form of extensibility is built into the language, allowing you to program your own solutions, and to [...]]]></description>
			<content:encoded><![CDATA[<p>Most typesetting software lets the user operate within its functionality and thorough programming knowledge is required to write any form of extension to the system, typically in the form of a module written in C. With LaTeX this form of extensibility is built into the language, allowing you to program your own solutions, and to easily use other peoples&#8217; solutions to problems. One of the key places to find these other solutions is at places like <a href="http://www.ctan.org/">CTAN</a> (the Comprehensive TeX Archive Network).</p>
<p>While many premade solutions exist, there will invariably be times where you need something custom made. Today we will look at the basics of creating new commands in LaTeX. Previous familiarity with programming will be a benefit, but, I hope, not an absolute requirement.</p>
<p>A command in LaTeX can accomplish pretty much anything (for the really interested, LaTeX is <a href="http://en.wikipedia.org/wiki/Turing_completeness">Turing complete</a>), but for our purposes it is mostly used to abstract away formatting or change the values of counters or lengths. As such, familiar things in LaTeX such as <code><span class="keyword">\section</span></code>, <code><span class="keyword">\usepackage</span></code> and the like are all commands that abstract some behaviour. To familiarise ourselves with how to introduce commands, we will look at a fictional problem: We are writing a larger document containing a lot of acronyms, and we&#8217;d like to be able to standardise on the typography for printing acronyms, and ensure that the first time an acronym is used its full definition is written out.</p>
<p>Introducting a command in LaTeX is done using the <code><span class="keyword">\newcommand</span></code> command. Its full definition is on the following form:</p>
<pre><span class="keyword">\newcommand</span>{command-name}[number-of-arguments]{body}
</pre>
<p>Inside the body, the arguments can be referenced by typing <code>#n</code> where n is the n&#8217;th argument to the command. With this in mind, let us create an <code><span class="keyword">\ac</span></code> command for typesetting acronyms.</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\ac</span>}[<span class="number">1</span>]{<span class="keyword">\textbf</span>{#1}}
</pre>
<p>What this does it create a command, <code><span class="keyword">\ac</span></code> that takes one argument, and it expands to boldfacing the argument in the text. If you find using bold type for typesetting acronyms bad style, there is, fortunately, a single place to correct the formatting with this command, rather than having to go through the entire document and correct every single acronym.</p>
<p>While this lays the foundation for the command, what we would really like is to be able to define a number of acronyms, i.e. both their short forms and their expanded forms, and have LaTeX make sure that we have always presented the long form of an acronym the first time we use it. This is a good deal tricker, as we will have to introduce commands that are constructed dynamically. Basically we would like to be able to define an acronym like: <code><span class="keyword">\acronym</span>{ETA}{Expected Time of Arrival}</code>, and when we use it the first time have it print something like: <code>ETA (Expected Time of Arrival)</code> and just <code>ETA</code> at subsequent occurrances in the text.</p>
<p>So at the core we have something like:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\acronym</span>}[<span class="number">2</span>]{
}
</pre>
<p>And we need to fill something into the body. If we just do something like this:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\acronym</span>}[<span class="number">2</span>]{
  <span class="keyword">\newcommand</span>{<span class="keyword">\acronym</span>#1}{#2}
}
</pre>
<p>Then we&#8217;re told that the command <code><span class="keyword">\acronym</span></code> already exists. So we need some way of splicing <code>acronym</code> and our command together to form a whole command. This can be done with the <code><span class="keyword">\csname</span></code> and <code><span class="keyword">\endcsname</span></code> constructs, like this:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\acronym</span>}[<span class="number">2</span>]{
  <span class="keyword">\newcommand</span>{<span class="keyword">\csname</span> acronym#1<span class="keyword">\endcsname</span>}{#2}
}
</pre>
<p>However, now this tells us that the <code><span class="keyword">\csname</span></code> command is already defined. So we need some way to indicate to LaTeX that it should resolve and concatenate <code>acronym#1</code> first, then invoke <code><span class="keyword">\csname</span></code> and finally call <code><span class="keyword">\newcommand</span></code>. We can accomplish this using the fairly low-level command <code><span class="keyword">\expandafter</span></code>. This command does, when placed in front of a command indicate that what comes after it should be expanded before we call the command. So we have to jump over <code><span class="keyword">\newcommand</span></code> and <code><span class="keyword">\csname</span></code>, so we will need two uses of <code><span class="keyword">\expandafter</span></code>, like this:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\acronym</span>}[<span class="number">2</span>]{
  <span class="keyword">\expandafter</span><span class="keyword">\newcommand</span><span class="keyword">\expandafter</span>{<span class="keyword">\csname</span> acronym#1<span class="keyword">\endcsname</span>}{#2}
}
</pre>
<p>Using this with our call to <code><span class="keyword">\acronym</span>{ETA}{Expected Time of Arrival}</code> creates a new command, <code><span class="keyword">\acronymETA</span></code>, which we can call normally, and this command will print ‘Expected Time of Arrival’.</p>
<p>The next thing we need to accomplish is to only print this expanded text the first time we use the acronym. To aid us slightly, we use the <code>ifthen</code> package&#8217;s commands <code><span class="keyword">\newboolean</span></code> and <code><span class="keyword">\setboolean</span></code> as follows:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\acronym</span>}[<span class="number">2</span>]{
  <span class="keyword">\newboolean</span>{acronym#1}
  <span class="keyword">\setboolean</span>{acronym#1}{true}
  <span class="keyword">\expandafter</span><span class="keyword">\newcommand</span><span class="keyword">\expandafter</span>{<span class="keyword">\csname</span> acronym#1<span class="keyword">\endcsname</span>}{#2}
}
</pre>
<p>Here the <code><span class="keyword">\newboolean</span></code> figures out that the name of the boolean should be expanded before the creation of a new boolean on its own, so we don&#8217;t have to place manual <code><span class="keyword">\expandafter</span></code> commands throughout the code.</p>
<p>This means that we have everything in place to create a new command that prints the actual acronym:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\ac</span>}[1]{
  <span class="keyword">\ifthenelse</span>{<span class="keyword">\boolean</span>{acronym#1}}
  {#1 (<span class="keyword">\csname</span> acronym#1<span class="keyword">\endcsname</span>)
    <span class="keyword">\setboolean</span>{acronym#1}{false}}
  {#1}
}
</pre>
<p>Using this as follows:</p>
<pre>The <span class="keyword">\ac</span>{ETA} is now 5 minutes. The <span class="keyword">\ac</span>{ETA} is now 4 minutes.
</pre>
<p>Would result in the following: ‘The ETA (Expected Time of Arrival) is now 5 minutes. The ETA is now 4 minutes.’ This is, of course, all great and well, but recreating all of <code><span class="keyword">\ac</span></code> can be a bit taxing, if we want to change the formatting of parts of the word. So let us create two more commands to typeset the acronym with and without the expanded text:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\printlongacronym</span>}[<span class="number">2</span>]{#1 (#2)}
<span class="keyword">\newcommand</span>{<span class="keyword">\printshortacronym</span>}[<span class="number">1</span>]{#1}
</pre>
<p>And we change the definition of <code><span class="keyword">\ac</span></code> to use these as follows:</p>
<pre><span class="keyword">\newcommand</span>{<span class="keyword">\ac</span>}[1]{
  <span class="keyword">\ifthenelse</span>{<span class="keyword">\boolean</span>{acronym#1}}
  {<span class="keyword">\printlongacronym</span>{#1}{<span class="keyword">\csname</span> acronym#1<span class="keyword">\endcsname</span>}
    <span class="keyword">\setboolean</span>{acronym#1}{false}}
  {<span class="keyword">\printshortacronym</span>{#1}}
}
</pre>
<p>This now lets us change the formatting of acronyms merely by changing the definition of <code><span class="keyword">\printlongacronym</span></code> and <code><span class="keyword">\printshortacronym</span></code>, thus abstracting away all the technical issues of changing boolean values and whatnot from the end user. The user just needs to focus on how to adapt these two commands for his formatting needs. And, if these things are wrapped away in a package, the user can use <code><span class="keyword">\renewcommand</span></code> to change the meaning of the commands.</p>
<p>This is basically all there is to commands: give it a name, some arguments and do something in the body. Even a simple command such as typesetting some code inline in the text might be a boon if you have to go over the document at a later time to fix up the layout.</p>
<h2>Advanced commands and TeXing</h2>
<p>Now that we have our fancy <code><span class="keyword">\ac</span></code> function, it is time to consider what happens if we write something like: ‘Some of the <code><span class="keyword">\ac</span>{CIA}'s archives have recently been opened up.’ The outcome could be one of two things, depending on whether this is the first use of the CIA acronym or not, namely:</code></p>
<blockquote><p>Some of the CIA&#8217;s archives have recently been opened up.</p></blockquote>
<p>or</p>
<blockquote><p>Some of the CIA (Central Intelligence Agency)&#8217;s archives have recently been opened up.</p></blockquote>
<p>In the latter case, the possessive should still have been attached to CIA rather than the parenthesis. One way to cope with this is to use TeX&#8217;s mechanism for defining functions and in particular functions with optional parameters. This is done using the TeX function <code><span class="keyword">\def</span></code>. If we were to code a command with optional parameters (keeping in mind that optional parameters are typically given in []&#8216;s as the first part of a command, in TeX), it would look something like this:</p>
<pre><span class="keyword">\def</span><span class="keyword">\acopt</span>[#1]#2{
 <span class="keyword">\ifthenelse</span>{<span class="keyword">\boolean</span>{acronym#1}}
  {<span class="keyword">\printlongacronym</span>{#2#1}{<span class="keyword">\csname</span> acronym#2<span class="keyword">\endcsname</span>}
    <span class="keyword">\setboolean</span>{acronym#2}{false}}
  {<span class="keyword">\printshortacronym</span>{#2#1}}
}
</pre>
<p>This basically means we can call it like this: <code><span class="keyword">\acopt</span>['s]{CIA}</code> and we&#8217;d get:</p>
<blockquote><p>CIA&#8217;s (Central Intelligence Agency)</p></blockquote>
<p>if this was the first use of the acronym. Now, we could just call <code><span class="keyword">\acopt</span></code> whenever we need to add some fancy possessive or what have you to an acronym. However, we&#8217;d like to be able to extend <code><span class="keyword">\ac</span></code> so that we only have to type one command, and that&#8217;ll figure out whether to call <code><span class="keyword">\acopt</span></code> sensibly. In order to do this, we must use a built-in command <code><span class="keyword">\@ifnextchar</span></code>. This command is primarily meant to be used from inside a package, but we can use it in normal document code by surrounding it by the two commands <code><span class="keyword">\makeatletter</span></code> and <code><span class="keyword">\makeatother</span></code>. These two commands are necessary, as @ isn&#8217;t treated as a normal letter in LaTeX commands and thus we&#8217;re required to change @&#8217;s code group (if you&#8217;re getting confused now, don&#8217;t worry, you can just use the two commands around the location where you need to use a command with an @ in it and worry about the details at some later time). With this we can define <code><span class="keyword">\ac</span></code> like this:</p>
<pre><span class="keyword">\makeatletter</span>
<span class="keyword">\def</span><span class="keyword">\ac</span>{<span class="keyword">\@ifnextchar</span>[<span class="keyword">\acopt</span><span class="keyword">\acnoopt</span>}
<span class="keyword">\makeatother</span>
</pre>
<p>What this does it it queries the <em>token stream</em> (the next bunch of characters in the document, for a very loosely hand-waved definition) and if the next character is a left-bracket we call <code><span class="keyword">\acopt</span></code>, or otherwise we call <code><span class="keyword">\acnoopt</span></code>, the latter of which we can define very easily:</p>
<pre><span class="keyword">\def</span><span class="keyword">\acnoopt</span>#1{<span class="keyword">\acopt</span>[]{#1}}
</pre>
<p>With this we can now both write <code><span class="keyword">\ac</span>['s]{CIA}</code> and <code><span class="keyword">\ac</span>{CIA}</code> and everything will sort itself out nicely.</p>
<h2>On the horison</h2>
<p>Apart from these few commands we&#8217;ve seen, it is also possible to define commands globally using <code><span class="keyword">\gdef</span></code> (commands are otherwise local to the environment they&#8217;re declared in), redefine LaTeX commands using <code><span class="keyword">\renewcommand</span></code>, create commands that cannot include paragraph changes using <code><span class="keyword">\newcommand*</span></code> and many, many others. However, the command construction you&#8217;ve seen above goes for pretty much all the other commands as well, so presuming you&#8217;ve gleaned some meaning from my ramblings, you should be able to get somewhere fast no matter the situation.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/08/03/programming-latex-%e2%80%94-writing-commands/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An example book style</title>
		<link>http://hstuart.dk/2007/06/20/an-example-book-style/</link>
		<comments>http://hstuart.dk/2007/06/20/an-example-book-style/#comments</comments>
		<pubDate>Wed, 20 Jun 2007 18:26:22 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/06/20/an-example-book-style/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we have made our way through <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=351">how to style a document</a>, 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 <a href="http://www.lipsum.com">Lorem Ipsum</a> can get a tad boring in the long run, we will create a book with some works that have passed into the <a href="http://en.wikipedia.org/wiki/Public_domain">public domain</a>, namely some by <a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a>, a significant American horror author of the early 20th century. None other than the man behind <a href="http://en.wikipedia.org/wiki/Cthulhu">Cthulhu</a>.</p>
<p>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:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/plain.png" alt="Plain chapter style" />
</div>
<p>As I&#8217;ve written before: while this is certainly acceptable, it&#8217;s not really too unique of fascinating, so let us think up a way to get it to feel more 1920&#8242;s and horror-ish. The last part is of course hard to quantify unless you pick some cheesy font that&#8217;s impossible to read, so let&#8217;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:</p>
<pre><span class="keyword">\makechapterstyle</span>{bookstyle}{ <span class="comment">% Yes it's a very imaginative name</span>
  <span class="keyword">\setlength</span>{<span class="keyword">\beforechapskip</span>}{<span class="number">0em</span>}
  <span class="keyword">\setlength</span>{<span class="keyword">\midchapskip</span>}{<span class="number">1em</span>}
  <span class="keyword">\renewcommand</span>{<span class="keyword">\chapnumfont</span>}{<span class="keyword">\normalfont</span><span class="keyword">\large</span><span class="keyword">\bfseries</span><span class="keyword">\fscshape</span>}
  <span class="keyword">\renewcommand</span>{<span class="keyword">\chaptitlefont</span>}{<span class="keyword">\normalfont</span><span class="keyword">\huge</span><span class="keyword">\bfseries</span><span class="keyword">\itshape</span>}
  <span class="keyword">\renewcommand</span>{<span class="keyword">\printchaptername</span>}{}
  <span class="keyword">\renewcommand</span>{<span class="keyword">\printchapternum</span>}{<span class="comment">%</span>

    <span class="keyword">\centering</span><span class="keyword">\chapnumfont</span><span class="keyword">\numtoname</span>{<span class="keyword">\thechapter</span>}
  }
  <span class="keyword">\renewcommand</span>{<span class="keyword">\printchaptertitle</span>}[1]{<span class="keyword">\centering</span><span class="keyword">\chaptitlefont</span> ##1}
}
<span class="keyword">\chapterstyle</span>{bookstyle}
</pre>
<p>This verily will take a bit further description. The <code><span class="keyword">\fscshape</span></code> 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 <code><span class="keyword">\scshape</span></code> instead. The <code><span class="keyword">\fscshape</span></code> I am using here is provided by the <code>mathdesign</code> package together with the URW Garamond typeface. The only other surprise here should be the <code><span class="keyword">\numtoname</span></code> command that is provided by <code>memoir</code>, and this command converts a number into a corresponding name, for instance <code><span class="keyword">\numtoname</span>{13}</code> will be turned into ‘thirteen’.</p>
<p>With this modest change in place, our chapter page suddenly looks like this:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/chap.png" alt="Modified chapter style" />
</div>
<p>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&#8217;t what I&#8217;d like them to be, we shall skip this step for now.</p>
<p>With this out of the world, let us take a look at the existing table of contents:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/plaintoc.png" alt="Plain table of contents" />
<p>  <img src="http://hstuart.dk/files/latex/post11/plaintoc2.png" alt="Plain table of contents, cont." /></p>
<p>
  <img src="http://hstuart.dk/files/latex/post11/plaintoc3.png" alt="Plain table of contents, cont." />
</p>
</div>
<p>From this we see that our book is divided in two parts: one for some of Lovecraft&#8217;s essays, and another for a very few of his poems. Since most of his essays aren&#8217;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:</p>
<pre><span class="keyword">\settocdepth</span>{chapter}
</pre>
<p>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 <code>memoir</code>, 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.</p>
<p>The first part is, unfortunately, rather hard. It requires us to override two different commands: <code><span class="keyword">\l@part</span></code> and <code><span class="keyword">\partnumberline</span></code>. The reason for this is two-fold. When we issue a <code><span class="keyword">\part</span></code> command, the following is written to the .aux-file: <code><span class="keyword">\@writefile</span>{toc}{<span class="keyword">\contentsline</span>{part}{<span class="keyword">\partnumberline</span> {I}Essays}{1}}</code>, for instance, for part 1 called Essays. When this is going to be typeset then <code><span class="keyword">\l@part</span></code> is called with the last two <code>{}</code>&#8216;s above as arguments.</p>
<p>Using the <code>tikz</code> 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 <code>memoir.cls</code>:</p>
<pre><span class="keyword">\newlength</span><span class="keyword">\sb@partheight</span>

<span class="keyword">\renewcommand*</span>{<span class="keyword">\l@part</span>}[<span class="number">2</span>]{<span class="comment">%</span>

        <span class="keyword">\ifnum</span> <span class="keyword">\c@tocdepth</span> &gt; <span class="number">-2</span><span class="keyword">\relax</span>
        <span class="keyword">\addpenalty</span>{-<span class="keyword">\@highpenalty</span>}<span class="comment">%</span>
        <span class="keyword">\addvspace</span>{<span class="keyword">\cftbeforepartskip</span>}<span class="comment">%</span>

        <span class="keyword">\begingroup</span>{
        <span class="keyword">\centering</span>
        <span class="keyword">\settoheight</span>{<span class="keyword">\sb@partheight</span>}{<span class="keyword">\Large</span><span class="keyword">\bfseries</span><span class="keyword">\MakeUppercase</span>{#1}}
        <span class="keyword">\setlength</span>{<span class="keyword">\sb@partheight</span>}{<span class="number">.5</span><span class="keyword">\sb@partheight</span>}
        <span class="keyword">\addtolength</span>{<span class="keyword">\sb@partheight</span>}{<span class="number">5pt</span>}
        <span class="keyword">\begin</span>{tikzpicture}
                <span class="keyword">\draw</span> (<span class="number">0</span>,<span class="number">0</span>) node { <span class="keyword">\Large</span><span class="keyword">\bfseries</span><span class="keyword">\MakeUppercase</span>{#1} };
                <span class="keyword">\draw</span> (<span class="number">-.25</span><span class="keyword">\textwidth</span>,<span class="keyword">\sb@partheight</span>) -- (<span class="number">.25</span><span class="keyword">\textwidth</span>,<span class="keyword">\sb@partheight</span>);
                <span class="keyword">\draw</span> (<span class="number">-.25</span><span class="keyword">\textwidth</span>,-<span class="keyword">\sb@partheight</span>) -- (<span class="number">.25</span><span class="keyword">\textwidth</span>,-<span class="keyword">\sb@partheight</span>);
        <span class="keyword">\end</span>{tikzpicture}
        <span class="keyword">\par</span>

        }<span class="keyword">\endgroup</span>
        <span class="keyword">\fi</span>
}

<span class="keyword">\renewcommand</span>{<span class="keyword">\partnumberline</span>}[<span class="number">1</span>]{}
</pre>
<p>So, I guess this requires some explanation. The test on <code><span class="keyword">\c@tocdepth</span></code> 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 <code><span class="keyword">\addpenalty</span></code> command here makes TeX prefer to create a page break \emph{before} the part rather than after, so we don&#8217;t have a part title dangling alone at the bottom of a page, since that doesn&#8217;t look particularly nice. Then we use <code><span class="keyword">\begingroup</span></code> to localise our changes so they don&#8217;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 <code><span class="keyword">\settoheight</span></code>. Then we calculate the half height and add some space, and this we use as a measurement in the following <code>tikzpicture</code>. The <code>tikzpicture</code> code should be familiar, as we have already seen it <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=346">here</a>. The redefinition of <code><span class="keyword">\partnumberline</span></code> merely discards the part number as we aren&#8217;t really interested in that.</p>
<p>Our changes to the actual chapters are a lot easier to accomplish. Indeed, it only requires the following two lines:</p>
<pre><span class="keyword">\setlength</span>{<span class="keyword">\cftbeforechapterskip</span>}{<span class="number">.2em</span>}
<span class="keyword">\renewcommand</span>{<span class="keyword">\cftchapterfont</span>}{<span class="keyword">\normalfont</span><span class="keyword">\small</span><span class="keyword">\itshape</span>}
</pre>
<p>Taking all this together and using it with our book, we get the following table of contents for it:</p>
<div class="center"><img src="http://hstuart.dk/files/latex/post11/toc1-2.png" alt="First page of the styled TOC" /><br /><img src="http://hstuart.dk/files/latex/post11/toc1-3.png" alt="Second page of the styled TOC" /></div>
<p>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&#8217;s essays don&#8217;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’.</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/sec1.png" alt="Plain section title" />
</div>
<p>First off, the numbering of the section doesn&#8217;t really make any sense in a fictional work as the title is usually sufficient in and of itself, so let&#8217;s do away with that. Apart from that, the standard section title layout looks quite decent. This is an extremely easy change then:</p>
<pre><span class="keyword">\maxsecnumdepth</span>{chapter}
<span class="keyword">\setsecnumdepth</span>{chapter}
</pre>
<p>And we get the following:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/sec2.png" alt="Modified section title" />
</div>
<p>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.</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/final.png" alt="Default page header and footer" />
</div>
<p>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&#8217;ll place the page number on the page edge in the footer. And this is easily accomplished with the following code:</p>
<pre><span class="keyword">\makepagestyle</span>{mybookstyle}
<span class="keyword">\makeoddhead</span>{mybookstyle}{}{<span class="keyword">\itshape</span><span class="keyword">\rightmark</span>}{}

<span class="keyword">\makeevenhead</span>{mybookstyle}{}{<span class="keyword">\itshape</span><span class="keyword">\leftmark</span>}{}
<span class="keyword">\makeoddfoot</span>{mybookstyle}{}{}{<span class="keyword">\thepage</span>}
<span class="keyword">\makeevenfoot</span>{mybookstyle}{<span class="keyword">\thepage</span>}{}{}
<span class="keyword">\makepsmarks</span>{mybookstyle}{<span class="comment">%</span>

  <span class="keyword">\def</span><span class="keyword">\partmark</span>##1{<span class="keyword">\markboth</span>{##1}{}}
  <span class="keyword">\def</span><span class="keyword">\chaptermark</span>##1{<span class="keyword">\markright</span>{##1}}
  <span class="keyword">\def</span><span class="keyword">\sectionmark</span>##1{}
}

<span class="keyword">\pagestyle</span>{mybookstyle}
</pre>
<p>And presto, we get a much nicer output:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post11/final2.png" alt="Styled page header and footer" />
</div>
<p>Throughout all these examples, we have used the @-sign repeatedly in commands. This is ok if you&#8217;re implementing this in a package file, but if you&#8217;re pasting them into your document&#8217;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 <code><span class="keyword">\makeatletter</span></code> and <code><span class="keyword">\makeatother</span></code>. 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.</p>
<p>This concludes the styling example for today. If you&#8217;re in a need for a lot of sensible material to try out different stylings on, then I suggest that you head over to <a href="http://en.wikisource.org/wiki/Main_Page">WikiSource</a> or <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenberg</a> and grab some works that have entered the public domain and try to make your own compilation of them. And for your viewing pleasure, <a href="http://hstuart.dk/files/latex/post11/lovecraft.pdf">here</a> is the book typeset with the style above.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/06/20/an-example-book-style/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Styling the page footer and header</title>
		<link>http://hstuart.dk/2007/06/12/styling-the-page-footer-and-header/</link>
		<comments>http://hstuart.dk/2007/06/12/styling-the-page-footer-and-header/#comments</comments>
		<pubDate>Tue, 12 Jun 2007 16:47:27 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/06/12/styling-the-page-footer-and-header/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post10/standard-1.png" alt="Standard page header" style="border: 1px solid black;" />
</div>
<p>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.</p>
<p>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).</p>
<p>These items (chapter number and title and section number and title) isn&#8217;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.</p>
<p>The first step to creating a new page header and footer is to create a new page style using the <code>\makepagestyle{name}</code> 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:</p>
<pre>\makepagestyle{mypagestyle}</pre>
<p>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: <code>\makeevenhead{style}{left}{center}{right}</code>, <code>\makeoddhead</code>, <code>\makeevenfoot</code>, and <code>\makeoddfoot</code>. All with the same number of arguments. So, this means we can extend our page style as follows:</p>
<pre>\makeevenhead{mypagestyle}{\thepage\hskip.5cm\vrule\hskip.5cm\leftmark}{}{}
\makeoddhead{mypagestyle}{}{}{\rightmark\hskip.5cm\vrule\hskip.5cm\thepage}
</pre>
<p>If we tack on a <code>\pagestyle{mypagestyle}</code> now, we will get some output like this:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post10/mypagestyle-1.png" alt="Initial version of our own page style" style="border: 1px solid black;" />
</div>
<p>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.</p>
<p>Storing information to be read by <code>\leftmark</code> and <code>\rightmark</code> is accomplished using <code>\markboth</code> and <code>\markright</code> (no, there is no separate <code>\markleft</code>). And these two commands are meant to be invoked inside some specific commands that are in turn invoked from <code>\chapter</code>, <code>\tableofcontents</code>, etc. These commands include: <code>\tocmark</code>, <code>\partmark</code>, <code>\chaptermark</code>, and <code>\sectionmark</code>. There are, of course, more marks and you can generate more by creating new lists, so if you&#8217;re overriding marks you need to make sure to catch all of them appropriately.</p>
<p>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.</p>
<p>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&#8217;re in the need to using commands with @ in them you can use the command <code>\makeatletter</code> and then once you&#8217;re done return it to its original state with <code>\makeatother</code>. I will try to remember to visit this topic in more detail at some later time.</p>
<p>The use of @ becomes relevant when we need to test whether we&#8217;re in the main matter, because this is done (with memoir, remember @ means we&#8217;re overriding the internals of some package) using the <code>\if@mainmatter</code> where <code>@mainmatter</code> is a boolean value.</p>
<p>Now we&#8217;re <em>finally</em> 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:</p>
<pre>\makeatletter

\makepsmarks{mypagestyle}{
  \def\chaptermark##1{\markboth{%
    \ifnum \value{secnumdepth} &lt; -1
      \if@mainmatter
        \chaptername\ \thechapter\ --- %
      \fi
    \fi
    ##1}{}}

  \def\sectionmark##1{\markright{%
    \ifnum \value{secnumdepth} &lt; 0
      \thesection. \ %
    \fi
    ##1}}
}

\makeatother
</pre>
<p>The <code>secnumdepth</code> is a counter that contains information about what depth of sections should be numbered. Chapters is equal to <code>secnumdepth</code> 0 and sections to 1, hence the comparisons. Using this in our document we get the following output:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post10/mypagestyle2.png" alt="Page style with changed marks" />
</div>
<p>And for those with bad eye-sight, a courtesy close-up of the verso page:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post10/closeup.png" alt="Close-up of changed marks" />
</div>
<p>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&#8217;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:</p>
<pre>\makepagestyle{myruledpagestyle}
\makeevenhead{myruledpagestyle}{\thepage}{}{\leftmark}
\makeoddhead{myruledpagestyle}{\rightmark}{}{\thepage}
\makeatletter
\makepsmarks{myruledpagestyle}{
  \def\chaptermark##1{\markboth{%
        \ifnum \value{secnumdepth} &gt; -1
          \if@mainmatter
            \chaptername\ \thechapter\ --- %
          \fi
        \fi
        ##1}{}}
  \def\sectionmark##1{\markright{%
        \ifnum \value{secnumdepth} &gt; 0
          \thesection. \ %
        \fi
        ##1}}
}
\makeatother
</pre>
<p>This is almost exactly the same as <em>mypagestyle</em> 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:</p>
<pre>\makerunningwidth{myruledpagestyle}{1.1\textwidth}
\makeheadposition{myruledpagestyle}{flushright}{flushleft}{flushright}{flushleft}
\makeheadrule{myruledpagestyle}{1.1\textwidth}{\normalrulethickness}
</pre>
<p>Here <code>\makerunningwidth</code> changes the width of the headers and footers of the specified style. As standard it&#8217;s <code>\textwidth</code> long, here we make it 1.1 times longer. <code>\makeheadposition</code> changes the placement of the header and footer. It has the form <code>\makeheadposition{even-head-position}{odd-head-position}{even-foot-position}{odd-foot-position}</code> where the different positions can be chosen between <em>flushleft</em>, <em>center</em> or <em>flushright</em>. This command is <em>only</em> useful, if the width of the page header is different from <code>\textwidth</code>. 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.</p>
<p>With all this trickery, this is what we get as output:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post10/myruledpagestyle.png" alt="Ruled page style" />
</div>
<p>As an incidental note, the last style presented here is almost equivalent to the <em>Ruled</em> 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.</p>
<p>This fairly much concludes the <a href="http://hstuart.dk/view.hspx?bid=12&amp;id=351">styling the document</a> 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.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/06/12/styling-the-page-footer-and-header/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Styling the other document divisors</title>
		<link>http://hstuart.dk/2007/06/02/styling-the-other-document-divisors/</link>
		<comments>http://hstuart.dk/2007/06/02/styling-the-other-document-divisors/#comments</comments>
		<pubDate>Sat, 02 Jun 2007 10:04:17 +0000</pubDate>
		<dc:creator>Henrik Stuart</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://localhost/index.php/2007/06/02/styling-the-other-document-divisors/</guid>
		<description><![CDATA[While sectional titles aren&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>While sectional titles aren&#8217;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.</p>
<p>While memoir is indeed very configurable, it doesn&#8217;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 <code>\setXheadstyle</code> where X can be sec, subsec, or subsubsec (and possibly more). So writing up the section style mentioned above would look like this:</p>
<pre>\newcommand{\ruledsection}[1]{%
  \noindent%
  \parbox[t]{\textwidth}{%
    \hrule\vskip1em
    \sffamily\Large\bfseries #1\vskip1em%
    \hrule%
  }
}

\setsecheadstyle{\ruledsection}
</pre>
<p>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:</p>
<div class="center">
  <img src="http://hstuart.dk/files/latex/post9/boxed-1.png" alt="Boxed section style" style="border: 1px solid black;" />
</div>
<p>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: <code>\setbeforeXskip</code>, <code>\setafterXskip</code>, and <code>\setXindent</code>. 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.</p>
<p>To see how these lengths can be used in practice, please refer to the memoir <a href="http://www.ctan.org/tex-archive/macros/latex/contrib/memoir/memman.pdf">manual</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hstuart.dk/2007/06/02/styling-the-other-document-divisors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

