I have my Table of contents as follows:

<div class="col-sm-2" id="leftCol">
   <div class="nav nav-stacked" id="sidebar">
       <h3 class="likesectionHead">Contents</h3>

       <div class="tableofcontents">
       <span class="chapterToc">1 <a href="#x1-20001" id="QQ2-1-2">Python Primer</a></span><br />
       <span class="sectionToc">1.1 <a href="#x1-30001" id="QQ2-1-3">Reshaping the future</a></span><br />
       <span class="subsectionToc">1.1.1 <a href="#x1-40001" id="QQ2-1-4">Exercises</a></span><br />
       <span class="subsubsectionToc"><a href="#x1-50001" id="QQ2-1-5">R-1.1</a></span>

I want to be able to access those sweet <span class="subsectionToc"> and <span class="subsubsectionToc"> to add things to them such as:

<li class="nav-header"> <a href="#" data-toggle="collapse" data-target="#userMenu">
    <span class="subsectionToc"><i class="glyphicon glyphicon-chevron-down"></i>1.1.1 <a href="#x1-40001" id="QQ2-1-4">Exercises</a></span> 
    <ul class="list-unstyled collapse in" id="userMenu">
       <li class="active"> <span class="subsubsectionToc"><a href="#x1-50001" id="QQ2-1-5">R-1.1</a></span></li>
       <li><span class="subsubsectionToc"><a href="#x1-50001" id="QQ2-1-6">R-1.2</a></span></li>

Without adding a ConfigureEnv.

I can't seem to find a way to access a specific span class using \Configure, does anybody know how?


This definitely isn't easy. By default, toc produced by tex4ht isn't hierarchic, and we must use pretty complicated code to achieve it. This is borrowed from tex4ebook. First simple helper file, hierarchictoc.sty:


main purpose of this file is to enable us to use \usepackage{hierarchictoc} in the .cfg file, which in turn will include main file, hierarchictoc.4ht:



\def\stepnavpoint{\advance\navpoint by1\relax\the\navpoint}

% Commands for support of hiearchical table of contents
% Support for: chapter, section, subsection
% 1 - section type 2 - closed sections
% We need to configure TocLink
% Configuraion of entries

whats going on here? important is this macro:


which is called like:


this is direct configuration for each level in the toc. code configured with \Configure{NavSection} is inserted and children levels are closed as needed. You can configure NavSection in the .cfg file:

\Configure{NavSection}{\HCode{<li><span class="\spanclass">}}{\HCode{</span><ul>}}{ }{\HCode{</ul></li>}}

it has four parameters. First is inserted at the beginning of toc entry. Note \spanclass command. Current class is stored here and you can use some conditionals to work with it. Second parameter is inserted after entry, as you can see, <span> element is closed here, and <ul> for choldren entries is inserted. Third parameter isn't interesting, it is code inserted between chapter number and link to section, it is space in our case. Last parameter is inserted when levels are closed.

For some sample file:

\subsection{hello, hello}
\subsection{hello, again}
\section{next section}

this is the result:

enter image description here

   <h2 class="likechapterHead"><a 
 id="x1-1000"></a>Contents</h2> <nav> 
<ul>     <li><span class="chapterToc">  1 <a 
href="#x1-20001" id="QQ2-1-2">Hello</a></span><ul>      <li><span class="sectionToc">  1.1 <a 
href="#x1-30001.1" id="QQ2-1-3">world</a></span><ul>      <li><span class="subsectionToc">  1.1.1 <a 
href="#x1-40001.1.1" id="QQ2-1-4">hello, hello</a></span><ul>  </ul></li>    <li><span class="subsectionToc">  1.1.2
href="#x1-50001.1.2" id="QQ2-1-5">hello, again</a></span><ul>  </ul></li></ul></li>    <li><span class="sectionToc">  1.2 <a 
href="#x1-60001.2" id="QQ2-1-6">next section</a></span><ul>

I don't really understand how you want decide which classes to add to individual <span> elements, but it should be possible to do various tricks using spanclass, if you add some more detail

  • Thanks Michal, I hadn't ask for your help in months, and here you are saving me again. Your tex4ebook git seems very interesting, I don't know lua but I would like to help, I'll check it out. I just want to make some sections collapsible. Here is the beta site claudiordgz.github.io/GoodrichTamassiaGoldwasser – Claudiordgz Oct 28 '14 at 20:58
  • @Claudiordgz my biggest pain with tex4ebook (and tex4ht as well) is documentation, so if you have any ideas how to improve it, it would be nice. the site looks really nice :) – michal.h21 Oct 28 '14 at 21:53
  • Would you be interested in having a front webpage using github pages? Just give me some time to understand your project. – Claudiordgz Oct 28 '14 at 22:11
  • @Claudiordgz sure, it would be great! – michal.h21 Oct 28 '14 at 22:17
  • ossom, I will get on it :D – Claudiordgz Oct 28 '14 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.