Linda Nagata: the blog at Hahví.net


Ebook How-to: Table of Contents Tip #1

April 24th, 2011

I decided to format the ebook I’m currently working on in a “flow through” style, without page breaks between the chapters. There’s just some blank space, followed by the name of the next chapter. It seems simple enough, but I discovered two challenges along the way.

(If you haven’t read my Ebook How To, I use Sigil to edit the epub file, and Calibre to convert the epub to mobi.)

Challenge #1: On my first pass I broke the book into several sections in my epub editor, Sigil. There were no page break codes at the end of these sections, nevertheless, when converting to mobi, Calibre inserted page breaks. I tried to turn this function off in Calibre, but didn’t manage to do it. Crude solution: I dumped all the text from the first to the last chapter into a single epub section. (I left the front and back matters in the usual separate sections.) I was lucky, because this is a fairly short novel of 65,000 words. At any rate, this eliminated the problem of unwanted page breaks.

Challenge #2: I’m not sure why I let this become a problem. I’ve done a lot of CSS and should have known better, but copy and paste got me in trouble. Here’s how:

When I create a new ebook, I copy some version of this code into the top of each chapter:

  <div class="chapterdiv">
    <h1 class="chapterline" id="heading_id_2" title="The Hunt">The Hunt</h1>
  </div>

The “chapterdiv” and “chapterline” classes let me define the formatting. The h1 element will be automatically picked up as part of the table of contents by Sigil’s TOC editor. The “title” in the h1 element is the text that will be displayed in the table of contents. The “id” is where I tripped up.

It’s a rule in CSS that all the IDs in a single document have to be unique–otherwise they’re not uniquely identifying a single element, and that’s their purpose.

Epub files are made up of many different documents. If you follow the usual scheme, each chapter will be in a separate document. In this case, it doesn’t matter if all the chapter headers get the same ID, i.e. “heading_id_2”. Since all the chapters are separate documents, the ID is unique in each document.

But when I dumped all the chapters into the same document, that ID suddenly wasn’t unique anymore. So when I converted the epub to mobi and sent it to my Kindle, the table of contents didn’t work. I would get sent to the last chapter, no matter what chapter I clicked on.

The fix was easy, once I realized what was going on. I went back to the epub file and used the search function to find each incident of “heading_id_2” and I incremented the number, giving me “heading_id_3”, “heading_id_4”, etc., all the way up to “heading_id_29”. Problem solved.

I’m posting this in case it’s helpful to anyone else who might run into the same problem.

What I haven’t figured out yet is how to link the cover into the table of contents. Using the h1 code above will cause “Cover” to appear in the TOC, but when I click on it in my Kindle, it tries to make a highlight, instead of taking me to the cover. If anyone knows the solution to this riddle, I’d love to hear it.

Posted on: Sunday, April 24th, 2011 at 2:42 pm
Categories: E-book How-to.

2 Responses to “Ebook How-to: Table of Contents Tip #1”

  1. Jill Engledow Says:

    Linda, you are so far ahead of me on this stuff; I can just about follow what you’re saying, but don’t think I’ll ever try to do it. You go, girl!

  2. Linda Says:

    *years* working with CSS/HTML/PHP allow me to be fussy with ebook formatting 🙂