5

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>
       </div>
   </div>
</div>

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>
    </ul>
</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?

3

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:

\ProvidesPackage{hiearchictoc}
\RequirePackage{etoolbox}
\endinput

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:

\NewConfigure{NavSection}{4}

\newbool{tocnoempty}

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

% Commands for support of hiearchical table of contents
% Support for: chapter, section, subsection
\def\resettoclevels#1{%
\def\do##1{\csgdef{nav##1finish}{}}
\docsvlist{#1}
}
\def\usetoclevels#1{%
\def\do##1{\csuse{nav##1finish}}%
\docsvlist{#1}%
}
\def\finishtoclevel#1{%
\csgdef{nav#1finish}{\d:NavSection}
}
\def\closelevels#1{
\usetoclevels{#1}
\resettoclevels{#1}
}
% 1 - section type 2 - closed sections
\def\navsection#1#2{
\ConfigureToc{#1}%
{\closelevels{#2}%
\edef\spanclass{#1Toc}%
\a:NavSection\Ncx:Mark}
{\c:NavSection}
{}
{\b:NavSection%
\finishtoclevel{#1}%
}
}
\def\Ncx:Mark#1{%
\edef\:temp{#1}
\def\z@ro{0}
\ifx\:temp\z@ro\else#1\fi
}
%
\Configure{tableofcontents}{\HCode{<nav>\Hnewline<ul>}}{%
\usetoclevels{part,chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}%
\HCode{</ul></nav>\Hnewline}}{}{}{}
% We need to configure TocLink
% Configuraion of entries
\resettoclevels{part,chapter,section,subsection,subsubsection}
\navsection{part}{part,chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{chapter}{chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likechapter}{chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{section}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likesection}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{subsection}{subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likesubsection}{subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{subsubsection}{subsubsection,likesubsubsection}
\navsection{likesubsubsection}{subsubsection,likesubsubsection}

whats going on here? important is this macro:

\def\navsection#1#2{
\ConfigureToc{#1}%
{\closelevels{#2}%
\edef\spanclass{#1Toc}%
\a:NavSection\Ncx:Mark}
{\c:NavSection}
{}
{\b:NavSection%
\finishtoclevel{#1}%
}
}

which is called like:

\navsection{section}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}

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:

\usepackage{hiearchictoc}
\Preamble{xhtml}
\begin{document}
\Configure{NavSection}{\HCode{<li><span class="\spanclass">}}{\HCode{</span><ul>}}{ }{\HCode{</ul></li>}}
\EndPreamble

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:

\documentclass{book}
\begin{document}
\tableofcontents
\chapter{Hello}
\section{world}
\subsection{hello, hello}
\subsection{hello, again}
\section{next section}
\end{document}

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
<a 
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>
   </ul></li></ul></li></ul></nav> 

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.