# How to add a Tables of Contents to a Kindle book using Calibre or other program?



## CS (Nov 3, 2008)

Quick question: I have a book collection - all in one Mobi/PRC file, no DRM - but it doesn't contain a Table of Contents. 

How can I add one using Calibre or another program?

I bought the collection from an author on this board a while back and it seems awesome (I still need to actually read it, hahahaha), but the lack of TOC has always annoyed me.

If anyone else bought this, they'll know exactly which one I mean.  But I don't want to out the poor author for his baffling lack of TOC-usage.


----------



## Morf (Nov 18, 2010)

Is the book DRM'ed (ie can you copy or convert it)? If it is, I don't think there's much you can do.

If it isn't, then you could try doing a Calibre conversion and use Calibre's TOC generation facility. You can convert from MOBI to MOBI. In the convert page, there is a table of contents menu; it may be sufficient to check "Force use of auto-generated Table of Contents".

If that doesn't work, it's going to get increasingly complicated. This is because you're going to have to tell Calibre what to look for as TOC entries. You're going to have to work out what marks a chapter - it may be the word "Chapter", or a bold heading or whatever -  and then learn enough about XPath expressions to be able to fill in the TOC Level 1, 2, and 3 fields as necessary.

The other approach is to convert from MOBI to RTF, open the RTF in Microsoft Word, add the TOC there and convert it back. No guarantees about how successful this will be!

It's a lot easier for the author to do it, and I'd be tempted to raise it with the author on here - send him/her a PM if you don't want to "out" them!


----------



## CS (Nov 3, 2008)

Thanks, Morf. There's no DRM, so I'm trying the auto-generate option first and I'll see how that goes, then proceed from there.


----------



## CS (Nov 3, 2008)

Well, this is odd. The auto-generate option ends up generating...nothing. It takes forever, but in the end, there's no TOC of any kind to show for it.


----------



## Morf (Nov 18, 2010)

You've got to think about the problems that Calibre (or any other software) has here. How does it know what is a chapter or section break? How can it tell what to put in a TOC? It might seem obvious to you, but a lot of the time it's pure guesswork for software.

If it didn't generate anything, then my guess is the chapters are not obvious enough for it to see them. Therefore you're going to have to look at the XPath stuff to see if you can work out how to tell it what marks a chapter.


----------



## CS (Nov 3, 2008)

Morf said:


> Therefore you're going to have to look at the XPath stuff to see if you can work out how to tell it what marks a chapter.


I know nothing about XPath, unfortunately. Do you have any tutorials you could link me to?


----------



## Morf (Nov 18, 2010)

That makes two of us! 

See if this helps: http://manual.calibre-ebook.com/xpath.html


----------



## Snorkledorf (Oct 18, 2009)

What's worked for me is to go into calibre's Convert Books --> Table of Contents pane where there's a wizard button to help set that up. In the wizard, for the "Match HTML tags with tag name" field select "h1" then "OK." Once you've selected the correct input and output formats, clicking "OK" again will begin the conversion. (calibre should rename your original mobi file as its own custom ".original_mobi" format in case you want to roll back.)

Open the resulting new mobi file in calibre's ebook viewer and click the page-with-lines button that's supposed to represent the TOC. See if it looks table-of-contents-ey.

I've gotten some good results doing that, though it's still pretty hit and miss even so. Still a lot to learn.

Recently I've been converting to txtz format with the output set to save in Markdown format, then editing the whole book manually in a text editor (BBEdit on the Mac) so the formatting is 100% under my control. But that's probably overkill if all you need is a TOC.


----------

