2

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

</div>

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.

4

Try this configuration file:

\Preamble{xhtml}
\newcounter{sectid}
\def\myendp{\ifvmode\IgnorePar\fi\EndP}
\Configure{section}{\stepcounter{sectid}\myendp\HCode{<div class="content" id="content-\arabic{sectid}" data-id="\arabic{sectid}">}}{\myendp\HCode{</div>}}
   {\myendp\IgnorePar
    \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}
\begin{document}
\EndPreamble

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 
 id="x1-10001"></a>hello</h3>
<!--l. 8--><p class="noindent" >
</p>
   <div class="content" id="content-2" data-id="2"> <h3 class="subsectionHead"><span class="titlemark">1.1   </span> <a 
 id="x1-20001.1"></a>first</h3>
<!--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
rutrum.
</p><!--l. 10--><p class="noindent" >
</p>
   </div><div class="content" id="content-3" data-id="3"> <h3 class="subsectionHead"><span class="titlemark">1.2   </span> <a 
 id="x1-30001.2"></a>second</h3>
<!--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
mauris.
</p><!--l. 12--><p class="noindent" >
</p>
   </div></div><div class="content" id="content-4" data-id="4"><h3 class="sectionHead"><span class="titlemark">2   </span> <a 
 id="x1-40002"></a>world</h3>
<!--l. 13--><p class="noindent" >
</p>

   <div class="content" id="content-5" data-id="5"> <h3 class="subsectionHead"><span class="titlemark">2.1   </span> <a 
 id="x1-50002.1"></a>third</h3>
<!--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
massa.
</p>
   </div></div> 
  • 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 Jun 12 '18 at 16:02
  • @user61681 I would solve that using post-processing, TeX solution would be difficult – michal.h21 Jun 12 '18 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 Jun 13 '18 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 Jun 13 '18 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 Jun 13 '18 at 6:48

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.