If one uses

htlatex filename "xhtml,svg"

The section name in html file begins as

<h3 class="sectionHead">

Also the subsection name in html file begins as

<h4 class="subsectionHead">

Is it possible to configure htlatex in a way that the following html code gets added at the beginning of section or subsection name before h3 or h4 tags?

<div class="content" id="content-x" data-id='x'>

and at the end of section or sub-section


gets added. Here x denotes counter. For example if there are 8 sections and 3 subsections....Then x will take values from 1 to 11. The counter doesn't distinguish between section and subsection. So the problem is about adding numbered divisons for sections/subsections in produced html file.

1 Answer 1


Try this configuration file:

\Configure{section}{\stepcounter{sectid}\myendp\HCode{<div class="content" id="content-\arabic{sectid}" data-id="\arabic{sectid}">}}{\myendp\HCode{</div>}}
    \HCode{<h3 class="sectionHead">}\TitleMark\space\HtmlParOff}
   {\HCode{</h3>}\HtmlParOn\ShowPar \IgnoreIndent \par}
\Configure{subsection}{\stepcounter{sectid}\myendp\HCode{<div class="content" id="content-\arabic{sectid}" data-id="\arabic{sectid}">}}{\myendp\HCode{</div>}}
   { \myendp\IgnorePar\HCode{<h3 class="subsectionHead">}\TitleMark\space\HtmlParOff}
   {\HCode{</h3>}\HtmlParOn \IgnoreIndent \ShowPar \par}

The section configurations \Configure{sectiontype} takes four parameters, the first two are code which is inserted at the beginning and at the end of the section, so it can be used to insert your <div> element. The next parameter is to insert the heading element, together with numbering (using \TitleMark. In the last parameter the heading is closed. There is lot of macros that deal with paragraphs, \myendp, HtmlParOn etc.

Custom counter is used to insert the id numbers, it is updated at the start of each section:

 \stepcounter{sectid}\myendp\HCode{<div class="content" id="content-\arabic{sectid}" data-id="\arabic{sectid}">}

This is a sample result:

 <div class="content" id="content-1" data-id="1"><h3 class="sectionHead"><span class="titlemark">1   </span> <a 
<!--l. 8--><p class="noindent" >
   <div class="content" id="content-2" data-id="2"> <h3 class="subsectionHead"><span class="titlemark">1.1   </span> <a 
<!--l. 9--><p class="noindent" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum
ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu
neque. Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet
tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra
ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur
auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu,
accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
</p><!--l. 10--><p class="noindent" >
   </div><div class="content" id="content-3" data-id="3"> <h3 class="subsectionHead"><span class="titlemark">1.2   </span> <a 
<!--l. 11--><p class="noindent" >Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor
lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis.
Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam
tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus
</p><!--l. 12--><p class="noindent" >
   </div></div><div class="content" id="content-4" data-id="4"><h3 class="sectionHead"><span class="titlemark">2   </span> <a 
<!--l. 13--><p class="noindent" >

   <div class="content" id="content-5" data-id="5"> <h3 class="subsectionHead"><span class="titlemark">2.1   </span> <a 
<!--l. 14--><p class="noindent" >Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt
tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante.
Phasellus adipiscing semper elit. Proin fermentum massa ac quam. Sed diam turpis,
molestie vitae, placerat a, molestie nec, leo. Maecenas lacinia. Nam ipsum
ligula, eleifend at, accumsan nec, suscipit a, ipsum. Morbi blandit ligula
feugiat magna. Nunc eleifend consequat lorem. Sed lacinia nulla vitae enim.
Pellentesque tincidunt purus vel magna. Integer non enim. Praesent euismod nunc
eu purus. Donec bibendum quam in tellus. Nullam cursus pulvinar lectus.
Donec et mi. Nam vulputate metus eu enim. Vestibulum pellentesque felis eu
  • Awesome...it works like a charm. It's simple. I was trying but couldn't get it. A tweak is required on my side. I think this would be difficult though. If section contains a subsection, counter must be stepped up only for subsections. So counter will not be stepped for those sections which contain subsections, however it will be stepped up for subsections in those sections.
    – user61681
    Commented Jun 12, 2018 at 16:02
  • @user61681 I would solve that using post-processing, TeX solution would be difficult
    – michal.h21
    Commented Jun 12, 2018 at 17:27
  • Ya, I used python. The solution you provided was excellent. Is there any proper documentation/book/tutorial to learn configurations of htlatex and in general for programming (macros etc) in LaTeX? Novice learners (even with some programming background) often find it difficult to work with LaTeX.
    – user61681
    Commented Jun 13, 2018 at 1:00
  • @user61681 there is unfortunately no comprehensive guide to tex4ht, other than the official documentation and the information written to the log file when "info" option is used for tex4ht run. The log file contains main configuration options. I've started to work on new documentation, but it doesn't contain lot of information yet.
    – michal.h21
    Commented Jun 13, 2018 at 6:46
  • @user61681 for TeX programming, I learned from some Czech books, which are not available in English. But the TeXbook and TeX by topic are also good.
    – michal.h21
    Commented Jun 13, 2018 at 6:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .