With ePUBs it is straightforward to add a table of contents to your book, allowing your readers to quickly navigate within your content. Interestingly how you do it is not a part of the ePUB specification itself, but highlights ePUB’s development from earlier digital book initiatives.
This article assumes you are creating/editing your ePUB source content directly. If generating an ePUB via a development tool (eg Pages, InDesign) then it can automatically generate the TOC for you. However even if doing so this article will help describe what is being built for you.
To help, I’ve included a smorgasbord of sample ePUBs to demonstrate how tables of contents are included. There’s a basic book (single level/standard TOC) as well as books with 2, 3, 4, or 5 level TOCs.
Download the one you want or take the whole pack. Note that because WordPress considers EPUBs a security risk, they’ve all be zipped.
Do I need to do this?
A table of contents is optional (your ePUB is valid and usable without one). What’s more how it actually presents for your readers is dependent on the reader. As such you should decide for each book, and your target market, whether worth the development effort.
How do I do this?
Adding a table of contents to your ePUB has two key steps:
- Creating a valid .NCX file
- Including the .NCX file in your book
Let’s work through these steps in turn.
Creating a valid .NCX file
The .NCX file defines your table of contents. There’s no rule about what filename to give it, but the default option for most eBooks appears to be TOC.NCX. So we’ll assume we’ve called it that too.
The definition for your TOC.NCX file is not part of the ePUB specification but is instead maintained by the DAISY Consortium and co-opted by IPDF who manage ePUBs for us. Browse the detailed specification and see if you can find out what NCX stands for.
In essence an .NCX file is an XML file. It lists the elements for your table of contents within a simple and standard framework:
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//NISO//DTD ncx 2005-1//EN"
-- insert required metadata
-- insert required navigation structure
Copy that and use as the basis for your TOC.NCX, while we fill in the missing bits: metadata, title, author, navigation structure.
(1) insert required metadata
Every .NCX file needs to define four metadata elements:
dtb:uid – this is the unique ID for your publication. It needs to match the ID given in content.opf.
dtb:depth – this is the number of levels in your TOC. Normally this would be 1 but you can add more levels – see hierarchical contents below
dtb:totalPageCount – this is not used: set to zero
dtb:maxPageNumber – this is not used: set to zero
For example, here’s the .NCX header for a standard table of contents with only 1 layer of entries.
<meta name="dtb:uid" content="a897sad23fd23ds" />
<meta name="dtb:depth" content="1" />
<meta name="dtb:totalPageCount" content="0" />
<meta name="dtb:maxPageNumber" content="0" />
(2) record title and author
The document title and author should match those recorded in the main metadata within content.opf (DC:Title, DC:Creator respectively). Some readers may reference these values when displaying the contents, although those in my limited test suite all seem to ignore these values and focus on those in content.opf.
In the sample ePUBs included with this article I deliberate added “(NCX)” to the end of the title and author in each .NCX file. Just to see if/where it appeared. Never did. But your mileage may vary.
(3) insert required navigation structure
navMap is where the entries in your table of contents are defined using special XML elements called
<navLabel>Text to show in the table</navLabel>
<content src="url" />
The playOrder is numeric, indicating the sequence that the content elements should be traversed in when navigating. Note that this does not need to match the order you list them (and they appear) in the TOC. Nor does it need to match the sequence in the
<spine> section of content.opf.
My testing (and the OPF specification) indicate that the playOrder is not used to determine the sequence content is browsed. When users move through the content sequentially, this sequence is defined by the <spine> element in content.opf. However, even though playOrder is not used, it is recommended that meaningful values are recorded “just-in-case”. It also ensures your ePUB validation, if using epubcheck, will be successful.
The text will present as entered in your TOC; be mindful that not all readers will have a lot of room so don’t make the entries too long.
The URL needs to be the reference to the file/location that is the target of this TOC entry. This will be the filename if you have created separate chapter files. Else you could reference separate sections within a single document via anchor tags.
For example, here are sample entries from the start of one EBook.
<content src="00-cover.xhtml" />
<content src="10-frontis.xhtml#preface" />
<content src="10-frontis.xhtml#credits" />
<navLabel><text>Chapter 1 - Background</text></navLabel>
<content src="chapter1.xhtml" />
Note: If using anchor tabs, the other end of the link needs to be to an ID, not name tag. Read this for the explanation.
Including the .NCX file in your book
Only two small changes are made to your existing eBook to include your .NCX content, both made to content.opf:
- add your TOC.NCX file to the ebook manifest. Application type needs to be
- add a reference to the new TOC.NCX to your ebook spine. This is added as an attribute in your
<item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml"/>
<itemref idref="chapter1" />
That’s it. Save CONTENT.OPF, build and test.
Once you’ve mastered the basic table of contents, you’re ready to investigate some more advanced options:
You can create multiple levels in your table of contents simply by nesting your navPoint elements.
<navPoint id="navpoint" playOrder="1">
<navPoint playOrder="2">Section 1</navPoint>
<navPoint playOrder="3">Section 2</navPoint>
Don’t forget to work through the playOrder values to retain a logical sequence to your content. Even though not sure who/where these values are used
How hierachical contents are displayed or navigated depends very much on the reader. It also appears to vary how many levels of content are supported. The standard itself sets no limits (or tells you what to call them: chapter, part, section, ..). However mileage may vary with different readers so don’t go overboard. The following table shows results with the two readers I spend most of my time with: Adobe Digital Editions (ADE) and iBooks (on the iPad). The two columns for ADE show the default view (when first opening your book) and the expanded view.
|single level (basic book)
- for iBooks, it gives up after 2 levels of contents. Unable to find anything official that states that’s a limit, but if aiming for that platform I’d stop at 2.
- for Adobe Digital Editions, the menu gets a little confused at the deeper levels – notice at 5 levels of contents the first level does not appear. Not sure if anything can be done to fix that, but also reason to avoid going that deep.
The above images are taken from the sample ebooks included with this article. Feel free to use to test how well supported in your chosen reader.
multiple tables (pageLists and navLists)
The standard table of contents is defined within a
navMap. This is the complete, logical structure of your book. However the .NCX specification also allows you add two additional types of references to your TOC:
- pageList – use if needing to offer navigation by page (rather than topic/section). Your ePUB can only include one pageList, and it cannot be hierarchical – one layer only.
- navList – use if wanting to offer other any other arbitrary collection of topics, for example a list of illustrations or a collection of chocolate recipes taken from your complete recipe book. You can have as many navLists as you want, although they too can only be single level. An additional element, navLabel, can be added to give a title to your collection.
The steps involved in defining and including these tables is complex enough to warrant their own article, coming soon.
A table of contents is a valuable addition to most books, particularly non-fiction or reference titles. Adding one is reasonably simple, and thus worth the effort. However keep to the basics if wanting to be functional in the widest range of readers.