Creating a style file in ConTeXt

This blog post explains how to create a style file in ConTeXt. I have an eink reader, IRex digital reader DR1000, and I occasionally use it read novels from Project Gutenberg. (Most of the time, I use it to read research papers, something that DR1000 is perfect for). Most novels are available as plain text files, html files, and in mobipocket and epub formats. The DR can read all of these, but the quality is usually really bad. I have become accustomed to TeX output and things like bad hyphenation, large inter word space, bad kerning and no ligatures become a big distraction. So, I thought that it will be nice to use TeX to typeset these novels.

I needed a style that

  1. Matches the paper size of DR. DR can ignore margins and stuff, but since setting paper size is easy, why not just do it once.
  2. Extremely small margins. I do not want any page numbers (the DR already shows that) or any other info in headers and footers. I want as much space as I can get to read the text
  3. A readable font with a large x-height. No, computer modern and latin modern and not readable fonts
  4. Reasonably attractive chapter headings
  5. The ability to convert the plain text file from PG to a usable tex file with the minimal amount of markup.

This is how I went about creating such a style.

Paper size

Roughly the DR screen is 8in x 6.25in. To use a non-standard paper size in ConTeXt, the paper size must be defined

\definepapersize[irexpaper][height=8in,width=6.25in]

and then we have to tell ConTeXt to use that paper size.

\setuppapersize[irexpaper][irexpaper]

Layout

As I said, I want very small margins, because the DR hides the margins. Moreover, if I ever get a marvellous idea while reading the novel, the margin will be too small to place it anyways :-). I decided to use 5mm margin on all sides. This can be done by

\setuplayout
  [location=middle,
   topspace=5mm,
   width=middle,
   height=middle,
   bottomspace=5mm,
   bottomdistace=0mm,
   bottom=0mm,
   backspace=5mm,
   cutspace=5mm,
   leftmargin=0mm,
   rightmargin=0mm,
   header=0mm,
   footer=0mm,
   headerdistance=0mm,
   footerdistance=0mm,
 ]

Since I am not going to use headers, footers, or margin notes, I actually only need to set backspace, cutspace, topspace, and bottomspace. Nevertheless, I keep all of them, in case I ever decide to modify the layout in future.

Fonts

The most important reason for moving to a TeX solution was fonts. I really like Fontin and Museo by Jos Buivenga. Fontin is good font for reading because of its large x-height. Both these fonts are Open Type fonts. To use an Open Type fonts in ConTeXt, it needs to be placed in a location where ConTeXt can find it: either
$TEXMF/fonts/opentype or $HOME/.fonts. I placed Fontin and Museo fonts in my local texmf tree $HOME/texmf/fonts/opentype/exljbris, and then ran

luatools --generate

to tell ConTeXt about these new files.

Using Open Types fonts in ConTeXt is easy. Using Fontin and Museo is particularly easy, because Wolfgang Schuster has already created typescripts for these fonts as part of his typescripts module. I installed that module. I set Fontin as the main text font by using

\usetypescriptfile[type-exljbris]

\definetypeface [mainface] [rm] [serif] [fontin] [default] [features=default]
\definetypeface [mainface] [ss] [sans]  [fontin] [default] [features=default]
\setupbodyfont    [mainface,12pt]

Museo looks good in headings with Fontin. To use a different font in headings in ConTeXt, you first need to create font synonyms.

\definefont [interludefont]   [name:museo700 at 28pt]
\definefont [chapterfont]     [name:museo700 at 32pt]

Then the headings macros should be configured to use these fonts. I will explain later how to do that.

Drop capital

A drop capital at the beginning of a novel looks nice.
In ConTeXt, such drop capitals are called initials. These need to be enabled by using

\setupinitial
  [state=start,
    n=3,
    distance=.15pc,
     font=SerifBold,
  ]

The option n=3 tells ConTeXt that the drop capital should be three lines high. Other options are self explanatory: distance is the distance between the drop capital and rest of the text, and font is the font to be used. To tell ConTeXt to use drop capital at the beginning of a paragraph, \placeinitial has to be used before the paragraph. I only want the first paragraph of a chapter to have drop capitals. So, instepad of manually keying in \placeinitial at the beginning of each chapter, this macro can be plugged into the chapter headings macro.

Headings

Most novels just have a chapter heading; no sections, or subsections. Moreover, in the plain text files at Project Gutenberg, the chapters are already numbered, so I do not want ConTeXt to do anything fancy with the chapters. Just set up them in a nice font and create a bookmark for them (the DR can view pdf bookmarks). And, plug in the \placeinitial macro. All this can be done by

\setuphead[chapter]
          [alternative=middle,
                 style=\chapterfont,
                number=no,
                before={\blank[2*big,force]},
                 after={\blank[4*big]\placeinitial},
          ]

Again, most of the options are self explanatory: alternative=middle places the chapter title in the middle, style sets the font to be used (remember, \chapterfont is defined to be a synonym for Museo 700 at 32pt), number=no tells ConTeXt not to number the chapters, the before key sets the amount of blank space before the chapter title, after sets the blank space after the chapter title and also tells ConTeXt to use the \placeinitial macro on first letter of the next paragraph.

Next, I enable interaction and tell ConTeXt to place chapters as bookmarks.

\setupinteraction[state=start]
\placebookmarks[chapter]

ConTeXt MkII had a useful feature that was lost in the translation to MkIV: any section head command could be used in two ways. Either as a conventional TeX command where the argument was written in curly brackets, like

\chapter {An example of a chapter}

The contents of the chapter ....

or as a par delimited macro, where the argument ended with a \par (or equivalently, an empty line), like

\chapter
    An example of a chapter

The contents of the chapter ...

Since I want to make minimal changes to the text files from PG, the second option is really convinient. In most cases, the chapter heading in the text file is already followed by an empty line. So, all I need to do is add a line with \chapter above the chapter title. No need to worry about the curly brackets.

Unfortunately, this feature was removed from MkIV. But, I decided to resurrect it for this style.
The main idea is to add \dowithpargument in the right place in the heading code.

\unprotect
\def\dodohandlestructureheadnop[#1][#2]%
  {\dowithpargument{\dododohandlestructureheadnop[#1][#2]}}

\def\dododohandlestructureheadnop[#1][#2]#3%
  {\setfalse\currentstructureown
   \dohandlestructurehead{#1}{\c!reference=#2,\c!title={#3}}{}} % name ref nr title --
\protect

Interlude

Most novels have a title page, and sometimes a page with just some text in the middle of the page. I did not want to write fancy macros for such things as I want to keep the markup to the minimum. So, I decided to club all of them together using a single macro: \Interlude. This macro takes it argument and typesets it in the middle of the page using a big font (interludefont defined above). I use the same trick of using \dowithpargument so that I can just type \Interlude before the text and not worry about adding curly brackets.

\def\Interlude{\dowithpargument\doInterlude}

\def\doInterlude#1%
  {\page
    \null
    \vfill \vfill
   \startalignment[center]
     \let\\\crlf
     \interludefont
       \setupinterlinespace[big]
      #1
      \endgraf
  \stopalignment
   \vfill \vfill \vfill
   \page}

Style

I am almost done. The only other thing that I want is setting indentations and inter paragraph space.

\setupindenting   
\setupwhitespace  

Thats it!

Adding markup to plain text file

My first project to typeset was The Adventures of Tom Sawyer by Mark Twain.
I downloaded the file 74.txt and just made the following changes to it:

0a1
> \starthiding
27a29
> \stophiding
28a31
> \usemodule[irex]
29a33
> \starttext
31,34c35,38
<
<                    THE ADVENTURES OF TOM SAWYER
<                                 BY
<                             MARK TWAIN
---
> \Interlude
>                    THE ADVENTURES OF TOM SAWYER \\
>                                 BY              \\
>                             MARK TWAIN          \\
39c43,44
<
---
>
> \chapter
59,62c64
<                                                             THE AUTHOR.
<
< HARTFORD, 1876.
<
---
> \rightaligned{THE AUTHOR.}
63a66
> \leftaligned{HARTFORD, 1876.}
65d67
<                           T O M   S A W Y E R
66a69,70
> \Interlude
>                           T O M\ \ \   S A W Y E R
68a73
> \chapter
423a429
> \chapter
645a652
> \chapter
865a873
> \chapter
1233a1242
> \chapter
1408a1418
> \chapter
1919a1930
> \chapter
2182a2194
> \chapter
2375a2388
> \chapter
2652a2666
> \chapter
2933a2948
> \chapter
3110a3126
> \chapter
3294a3311
> \chapter
3584a3602
> \chapter
3796a3815
> \chapter
3965a3985
> \chapter
4313a4334
> \chapter
4435a4457
> \chapter
4787a4810
> \chapter
4894a4918
> \chapter
5078a5103
> \chapter
5321a5347
> \chapter
5428a5455
> \chapter
5697a5725
> \chapter
5743a5772
> \chapter
6072a6102
> \chapter
6424a6455
> \chapter
6547a6579
> \chapter
6681a6714
> \chapter
6953a6987
> \chapter
7264c7298
< "BECKY & TOM" had been found traced upon the rocky wall with
---
> "BECKY \& TOM" had been found traced upon the rocky wall with
7312a7347
> \chapter
7651a7687
> \chapter
7757a7794
> \chapter
8171a8209
> \chapter
8287a8326
> \chapter
8476a8516
> \chapter
8494c8534
<
---
> \starthiding
8531c8571,8572
<
---
> \stophiding
> \chapter
8858a8900,8901
>
> \stoptext

As you can see, I just hid a few parts of the legal notices, and added a \chapter command before each chapter. At one place I needed to change & to \&. For the titles, I needed to add explicit spaces, or explicit newlines. Very minimal change for a novel with around 9000 lines.

I saved the resultant file as 74.tex and compiled using

context 74.tex

I am not posting the tex file or the resultant PDF because I am not sure how the copyright stuff for Project Gutenberg works.
Here is how the first four pages of the result look like:

The input of these four pages looked like

\Interlude
                   THE ADVENTURES OF TOM SAWYER \\
                                BY              \\
                            MARK TWAIN          \\
                     (Samuel Langhorne Clemens)

\chapter
                           P R E F A C E

MOST of the adventures recorded in this book really occurred; one or
two were experiences of my own, the rest those of boys who were
......................................

\Interlude
                          T O M\ \ \   S A W Y E R

\chapter
CHAPTER I

"TOM!"

No answer.

"TOM!"

No answer.
.................

As you can see, the markup is really minimal.

Summary

(yeah, this is a long post, so it deserves a summary)
In summary, the complete module is below. Save it as t-irex.tex. To use it, add

\usemodule[irex]

in the preamble.

\startmodule irex

%D Paper Size

\definepapersize[irexpaper][height=8in,width=6.25in]
\setuppapersize[irexpaper][irexpaper]

%D Layout

\setuplayout
  [location=middle,
   topspace=5mm,
   width=middle,
   height=middle,
   bottomspace=5mm,
   bottomdistace=0mm,
   bottom=0mm,
   backspace=5mm,
   cutspace=5mm,
   leftmargin=0mm,
   rightmargin=0mm,
   header=0mm,
   footer=0mm,
   headerdistance=0mm,
   footerdistance=0mm,
 ]

\setuppagenumbering[location=]

%D Fonts

\usetypescriptfile[type-exljbris]

\definetypeface [mainface] [rm] [serif] [fontin] [default] [features=default]
\definetypeface [mainface] [ss] [sans]  [fontin] [default] [features=default]
\setupbodyfont    [mainface,12pt]

\definefont [interludefont]   [name:museo700 at 28pt]
\definefont [chapterfont]     [name:museo700 at 32pt]

%D Drop Capital

\setupinitial
  [state=start,
    n=3,
    distance=.15pc,
     font=SerifBold,
  ]

%D Headings

\setuphead[chapter]
          [alternative=middle,
                 style=\chapterfont,
                number=no,
                before={\blank[2*big,force]},
                 after={\blank[4*big]\placeinitial},
          ]

\setupinteraction[state=start]
\placebookmarks[chapter]

\startmode[*mkiv]
\unprotect
\def\dodohandlestructureheadnop[#1][#2]%
  {\dowithpargument{\dododohandlestructureheadnop[#1][#2]}}

\def\dododohandlestructureheadnop[#1][#2]#3%
  {\setfalse\currentstructureown
   \dohandlestructurehead{#1}{\c!reference=#2,\c!title={#3}}{}} % name ref nr title --
\protect
\stopmode

%D Interlude

\def\Interlude{\dowithpargument\doInterlude}

\def\doInterlude#1%
  {\page
    \null
    \vfill \vfill
   \startalignment[center]
     \let\\\crlf
     \interludefont
       \setupinterlinespace[big]
      #1
      \endgraf
  \stopalignment
   \vfill \vfill \vfill
   \page}

%D Style

\setupindenting   
\setupwhitespace  

\stopmodule
About these ads

7 thoughts on “Creating a style file in ConTeXt

  1. Pingback: J├╝rgen Fenn (juergenfenn) 's status on Wednesday, 21-Oct-09 14:31:52 UTC - Identi.ca

  2. I have a DR1000 and have been meaning to create a style like this for LaTeX. Looks like you’ve done all the work for me. :) I don’t know ConTeXt but now is a good time to learn.

    Thanks for sharing this style… it will surely come in handy!

  3. I tried your drop cap suggestion and it looks great. However, the top line extends a couple of characters beyond the right margin. What do I need to do to correct that?

    • In the examples that I have tried, the right edge of top line is aligned with the rest of the paragraph. One possibility is that ConTeXt cannot find a good hyphenation point for the last work of the first line, and as result it protrudes out. You can avoid that by saying

      \setuptolerance[tolerant]

      or

      \setuptolerance[verytolerant, stretch]

      If that does not work, then perhaps you can create a small example, and ask on the context mailing list http://wiki.contextgarden.net/ConTeXt_Mailing_Lists.

  4. Pingback: A style file for eink readers « Random Determinism

  5. setuplayout has one typo: bottomdistace in stead of bottomdistance. :)

    \setuplayout
    [location=middle,
    topspace=5mm,
    width=middle,
    height=middle,
    bottomspace=5mm,
    bottomdistace=0mm,
    bottom=0mm,
    backspace=5mm,
    cutspace=5mm,
    leftmargin=0mm,
    rightmargin=0mm,
    header=0mm,
    footer=0mm,
    headerdistance=0mm,
    footerdistance=0mm,
    ]

    P.S. trying to change the left and rightmargin, made them 10mm, but there was not difference. Any ideas why?

    Thanks for the post, Guus Bonnema.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s