Welcome to the second part of my Book Formatting Tutorial. The first part (steps 1-5) can be found here

Things get a little complicated from now on. Note that this is not the only, or even the best, way to make an e-book. There are simpler methods, detailed on countless other websites; there is software that almost fully automates the process. Some providers even do all the converting for you. But the instructions given below, I believe, will allow you a greater degree of flexibility and give a certain “je ne sais quoi” quality to your e-book which (so I’m told) makes it stand out from the rest.

6. Marking chapters for TOC, “hidden” class

Normally you don’t even need to do this. Any proper converting software should detect your headers from a structure of the manuscript. But if you really want to make sure the table of contents looks exactly like you want it, here’s how (this is especially important for Kindle, which is very specific in how it wants the TOC to show)

There are two ways to do this: an easy one, and a complicated but pretty one.

The easy way is to mark your chapter headlines with the “chapter” class, like so:

<p class="chapter">CHAPTER I</p>

But let’s assume that you want to do it the hard way and have the chapter headings show in a different font, or as pretty little images? We’ll go through inserting images below, but for now something else is important: if you don’t name your chapters in raw text, how will they appear in Table of Contents?

This is where “hidden” class comes in handy. Just wrap the chapter names in the “pagebreak chapter hidden” class paragraph tags, and they are ready to be discovered by the formatting software despite being, well, hidden before the reader. This is also helpful if you have sections you want to show in TOC but not name explicitly in text, like Maps or Credits.

7. Maps and illustrations

We’ve mentioned maps already. Inserting any kind of illustration is possibly the easiest part of the process, if you know even the most basic html. You do it just the same as if editing a webpage: simply put <img src=”filename.jpg”> in the text.

Two things to note: you may want to centre it out using the technique shown previously (unless you have a different idea of how to align them). Also, make sure the picture is in the same folder as the html you create the ebook from. That way you don’t need to put in the full path in the code.

Aside: for a map, or a full size illustration, it’s a good idea to match its proportion to a page size. Depending on your preferred reader, these proportions will vary, but the standard for an e-ink Kindle is 0.75 x 1 (so 750×1000, 600×800 etc.)

8. Chapter vignettes and Page Breaks

Vignettes are the little pictures at the beginning of the chapter. Some like them, some don’t, but it’s as easy to put them in as the illustrations: just use the <img src> tag, center them and make sure they look nice on the page.

This is a good moment to talk about Page Breaks, and it’s a bit complicated. To make sure you put a page break in where you wanted it, for a mobi and epub format you need to use an empty paragraph marked with a “pagebreak” class. This class is introduced in the snippet I posted earlier:

p.pagebreak { page-break-before:always; }

If you look at the snippet closely, you’ll notice I also have this code in the “vignette” class. This is because a vignette marks a beginning of the chapter, hence a need for a page break. You don’t want a page break between a vignette and chapter title!

If you don’t use vignettes, copy the above line of code to the “chapter” class in the css definition instead. I also use the separate “pagebreak” class for where I definitely need a page break, regardless of what’s going on in the text (see next point).

9. Title and credits

We’re almost done! What you may want to add now is the title page. This part is easy: use the “centre” spans to centre out the text, and use the “title” class for the title. You can freely adjust font sizes at this point, as we’re only going to do it this once. One neat little trick is to cut out the lettering from your cover and paste it as image on the title page. By now you should already know how it’s done.

On the next page (marked with the <p class=”pagebreak”> </p> tag) you put credits and copyright information. There are copyright templates on the web, so choose whichever you like. Try to fit everything on one page, if possible.

Remember, this is an electronic book, so you can put actual working links into the text. If you want your readers to be able to contact you or your collaborators, just wrap their names in <a href=”url”></a> tags.

Put another page break between credits and the rest of your book, if there isn’t one already.

10. The Big One: creating epub and mobi in Calibre

We can now go through creating the actual e-book. First, load your book into the Calibre by clicking “Add Books” and loading your html file (here’s a good last moment to check you have all the graphics in the same folder).

Now, click “Convert Books”. Normally this would be pretty much it: just type in the self-explanatory meta-data, load the cover picture, select the format from a drop-down menu in the top-left corner (epub or mobi) and convert. But since we’ve played a bit with the formatting and want a nice Table of Contents, we need to make one more adjustment here.

Go to Structure Detection and paste the following code in the first line ( “Detect chapters at (XPath expression)” ) :

//*[((name()='h1' or name()='h2') and re:test(., '\s*((chapter|book|section|part)\s+)|((prolog|prologue|epilogue)(\s+|$))', 'i')) or @class = 'pagebreak chapter hidden' or @class = 'vignette']

What it does is telling Calibre to look for “hidden” and “vignette” classes as chapter markers, instead of what it usually does which is just checking for pre-defined headers.

Now go to Table of Contents and paste the following in the “Level 1 TOC (Xpath Expression)”:

//h:p[re:test(@class, "chapter", "i")]

This helps Calibre to locate your hidden chapter names, otherwise you’ll end up with a lot of “Unknown” in the Table of Contents.

For the Page Setup tab, you may want to select your device of choice (choose plain Kindle for mobi) – but I’m not certain how much it affects the final result.

Search&Replace tab is useful if you want to make last minute changes to the contents of your book – assuming you know how to use regular expressions. I would rather do that on the raw HTML, though.

Aside: one important box to check for ePub format is “Preserve cover aspect ratio” on the EPUB Output tab. This prevents your cover image from getting all stretched out, depending on the device and screen proportions. There is no such setting for mobi files.

Now press OK and in a minute or so, your book will be ready. You will find the converted files in your Calibre Library folder or, if you want to transfer it directly to your device via cable, just press the Transfer to Device button (which appears when you connect your reader to the PC)

It goes without saying, but MAKE SURE to check everything looks fine before publishing the book. Tiny errors can grow to massive bugs in the final result. A particularly annoying bug is caused by misplaced, empty <i></i> tags – this may move entire paragraphs out of place. Don’t ask me why.

If you did everything right, and the e-book reads well, congratulations! Your book is now ready for publishing!

Aside: a good thing to remember at this point: when you make any changes to the html of an already converted book, you don’t need to upload it all again to Calibre. Just find a .zip file created by Calibre in the book’s folder, open it and copy the corrected html inside, overwriting the old version. Now all you need to do is press “Convert books” button to re-create the mobi or epub with the new content.

Note about KF8 format: until Amazon makes it obligatory, I would stay away from it. Mobi is still good enough for any e-book, unless it’s image-heavy or has a lot of interactive content. A lot of software (Calibre included) doesn’t deal with KF8 very well yet and your book may be rejected by Amazon despite your best efforts.

Still confused? Not enough time to do it yourself? I’m now offering book formatting services – see here for prices.