4

I need to customize the all the five level of section ids in my html document. I will try to get the below mentioned requirement through my .cfg file. But i Can't get the required output.

My Required Section ID output:

\section{} - <secone id="j_\jobname_s_001"> - 1
\subsection{} - <sectwo id="j_\jobname_s_001_s_001"> - 1.1
\subsubsection{} - <secthree id="j_\jobname_s_001_s_001_s_001"> - 1.1.1
\paragraph{} - <secfour id="j_\jobname_s_001_s_001_s_001_s_001"> - 1.1.1.1
\subparagraph{} - <secfive id="j_\jobname_s_001_s_001_s_001_s_001_s_001"> - 1.1.1.1.1 

My Current Output:

<secone id="j_Sample_s_1">
<title>
   <a 
 id="x1-10001"></a>Heading Level 1</title>

The below mentioned tag should be removed: <a id="x1-10001"></a>

My required html output is:

<secone id="j_\jobname_s_001">
<title>1 Heading Level 1</title>
<p>Section Paragraph.</p>
<sectwo id="j_\jobname_s_001_s_001">
<title>1.1 Heading Level 2</title>
<p>SubSection Paragraph.</p>
<secthree id="j_\jobname_s_001_s_001_s_001">    
<title>1.1.1 Heading Level 3</title>
<p>SubSubSection Paragraph.</p>
<secfour id="j_\jobname_s_001_s_001_s_001_s_001">
<title>1.1.1.1 Heading Level 4</title>
<p>Paragraph Sample.</p>
<secfive id="j_\jobname_s_001_s_001_s_001_s_001_s_001">
<title>1.1.1.1.1 Heading Level 5</title>
<p>SubParagraph Sample.</p>
</secfive>
</secfour>
</secthree>
</sectwo>
</secone> 

My CFG file is:

\Configure{section}
   {\EndP\IgnorePar\Tg<secone id="j_\jobname_s_\thesection">}
   {\EndP\Tg</secone>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{subsection}
   {\EndP\IgnorePar\Tg<sectwo id="j_\jobname_s_\thesubsection">}
   {\EndP\Tg</sectwo>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{subsubsection}
   {\EndP\IgnorePar\Tg<secthree id="j_\jobname_s_\thesubsubsection">}
   {\EndP\Tg</secthree>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{paragraph}
   {\EndP\IgnorePar\Tg<secfour id="j_\jobname_s_\theparagraph">}
   {\EndP\Tg</secfour>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{subparagraph}
   {\EndP\IgnorePar\Tg<secfive id="j_\jobname_s_\thesubparagraph">}
   {\EndP\Tg</secfive>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

MWE:

\documentclass{article}



\begin{document}

\section{Heading Level 1}

Section Paragraph. 

\subsection{Heading Level 2}

SubSection Paragraph.

\subsubsection{Heading Level 3}

SubSubSection Paragraph. 

\paragraph{Heading Level 4}

Paragraph Sample. 

\subparagraph{Heading Level 5}

SubParagraph Sample. 

\end{document}

If the section is unnumbered the number should not be viewed in the html output. For Example:

\section*{Heading Level 1}

The output should be  
<secone id="j_\jobname_s_001">
<title>Heading Level 1</title>

Kindly advice how to automate the section level ids.

3

The <a id="x1-10001"></a> tags are destinations from the TOC to a section. You can get rid of it, but note that you won't get working links from the TOC this way (at least not in an easy way). If that's OK for you, you can use a following configuration:

\def\gobbleone#1#2{}
\Configure{toTocLink}{\gobbleone}{}

Command which produces links is used in toTocLink by default, it gets two parameters, so we use command which discards two arguments instead.

To get section numbers, you must add \TitleMark command to section configuration:

\Configure{section}
   {\EndP\IgnorePar\Tg<secone id="j_\jobname_s_\thesection">}
   {\EndP\Tg</secone>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

You must also configure the \TitleMark to get rid of the added markup:

\ConfigureMark{section}{\thesection\space}

There is a problem with unnumbered sections, because they would ruin your id scheme. You want to use \thesection as basis for the id, but it is not updated for them, so you would get duplicated ids. You should use independent counter instead and probably use some different scheme. Like this:

\newcount\unnumbered
\unnumbered=0

\Configure{likesection}
   {%
   \advance\unnumbered by1%
   \EndP\IgnorePar\Tg<secone id="j_\jobname_us_\the\unnumbered">}
   {\EndP\Tg</secone>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

The full config file may look like this:

\Preamble{xhtml}
\setcounter{secnumdepth}{5}
\Configure{section}
   {\EndP\IgnorePar\Tg<secone id="j_\jobname_s_\thesection">}
   {\EndP\Tg</secone>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{section}{\thesection\space}

\Configure{subsection}
   {\EndP\IgnorePar\Tg<sectwo id="j_\jobname_s_\thesubsection">}
   {\EndP\Tg</sectwo>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{subsection}{\thesubsection\space}

\Configure{subsubsection}
   {\EndP\IgnorePar\Tg<secthree id="j_\jobname_s_\thesubsubsection">}
   {\EndP\Tg</secthree>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{subsubsection}{\thesubsubsection\space}
\Configure{paragraph}
   {\EndP\IgnorePar\Tg<secfour id="j_\jobname_s_\theparagraph">}
   {\EndP\Tg</secfour>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{paragraph}{\theparagraph\space}
\Configure{subparagraph}
   {\EndP\IgnorePar\Tg<secfive id="j_\jobname_s_\thesubparagraph">}
   {\EndP\Tg</secfive>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{subparagraph}{\thesubparagraph\space}

% Get rid of 

\def\gobbleone#1#2{}
\Configure{toTocLink}{\gobbleone}{}

\newcount\unnumbered
\unnumbered=0

\Configure{likesection}
   {%
   \advance\unnumbered by1%
   \EndP\IgnorePar\Tg<secone id="j_\jobname_us_\the\unnumbered">}
   {\EndP\Tg</secone>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}
\begin{document}
\EndPreamble

Note the use of \setcounter{secnumdepth}{5}, which is needed in order to get numbered paragraphs and subparagraphs.

Now we need to fix the ids to follow your requested scheme. What you get now is for example:

 <sectwo id="j_sample_s_1.1">

While padding with zeroes and replacing dots with _s_ could be done on TeX side, I think it is easier to use Lua filters provided by make4ht. Save this file as mybuild.mk4:

Make:htlatex{}

local filter = require "make4ht-filter"

-- number of characters at each section level
-- adjust this to your needs
local zeroes = 3

local fixsecid = function(s) 
  return s:gsub('id="j_([^%"]-)_s_([0-9%.]+)"', function(a,b)
    local t = {}
    for part in b:gmatch("([0-9]+)") do
      -- print(mytonumber(part))
      t[#t+1] = string.rep("0", zeroes - string.len(part)) .. part 
    end
    local new = table.concat(t, "_s_")
    print("new section id",a,new)
    return string.format('id="j_%s_s_%s"', a,new)
  end
  ) 
end

local process = filter{fixsecid}

Make:match("html$",process)

it uses regular expressions to detect section ids and fixes the numbering.

Compile your file with

 make4ht -uc config-file.cfg -e mybuild.mk4 filename

This is what I got for your sample with some added unnumbered sections:

<secone id="j_sample_us_1">
<title>
   Unnumbered section</title>
<!--l. 9--><p class="noindent" >
</p>
</secone>
<secone id="j_sample_s_001">
<title>
   1 Heading Level 1</title>
<!--l. 11--><p class="indent" >   Section Paragraph.
</p><!--l. 14--><p class="noindent" >
</p>
<sectwo id="j_sample_s_001_s_001">
<title>
   1.1 Heading Level 2</title>
<!--l. 16--><p class="indent" >   SubSection Paragraph.
</p><!--l. 18--><p class="noindent" >
</p>
<secthree id="j_sample_s_001_s_001_s_001">
<title>
   1.1.1 Heading Level 3</title>
<!--l. 20--><p class="indent" >   SubSubSection Paragraph.
</p>
<secfour id="j_sample_s_001_s_001_s_001_s_001">
<title>
   1.1.1.1 Heading Level 4</title>
<!--l. 24--><p class="indent" >   Paragraph Sample.
</p>
</secfour>
<secfour id="j_sample_s_001_s_001_s_001_s_002">

<title>
   1.1.1.2 Heading Level 4. Again</title>
<!--l. 28--><p class="indent" >   Another paragraph sample.
</p>
<secfive id="j_sample_s_001_s_001_s_001_s_002_s_001">
<title>
   1.1.1.2.1 Heading Level 5</title>
<!--l. 32--><p class="indent" >   SubParagraph Sample.
</p><!--l. 34--><p class="noindent" >
</p>
</secfive>
</secfour>
</secthree>
</sectwo>
</secone>
<secone id="j_sample_us_2">
<title>
   again</title>
<!--l. 36--><p class="noindent" >
</p>
</secone>
<secone id="j_sample_us_3">
<title>
   and again</title>
</secone>

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.