TransWikia.com

tex4ebook: bookmarks are duplicated when titlesec package is imported

TeX - LaTeX Asked on March 24, 2021

I’m generating an .epub with tex4ebook and I need to use titlesec. When I compiled the document, I noticed that if I import titlesec and compile more than once, the bookmarks for sections and subsections are duplicated. Any hints for how to remove the duplicates would be appreciated.

mwe.tex

documentclass{book}

usepackage{titlesec}

begin{document}

part{Mammalia}
chapter{Monotremata}
section{Platypoda}
subsection{Ornithorhynchidae}
subsubsection{Ornithorhynchus}
paragraph{anatinus}
section{Tachyglossa}
subsection{Tachyglossidae}
subsubsection{Tachyglossus}
paragraph{aculeatus}

end{document}

mwe.mk4

local domfilter = require "make4ht-domfilter"
local filter = require "make4ht-filter"

local ncxprocess = domfilter {
  function(dom)
    for i, navpoint in ipairs(dom:query_selector("navPoint")) do
       local text = navpoint:query_selector("text")[1]:get_text():gsub("^%s*", ""):gsub("%s*$", ""):lower()
       if dom:query_selector("navPoint")[i+1] ~= nil then
         local next_nav = dom:query_selector("navPoint")[i+1]
         local next_text = next_nav:query_selector("text")[1]:get_text():gsub("^%s*", ""):gsub("%s*$", ""):lower()
         if next_text == text then
           navpoint:remove_node()
         end
       end
    end
    return dom
  end
}

Make:htlatex {}
Make:htlatex {}
Make:match("ncx$", ncxprocess)

Output when titlesec import is commented out:

enter image description here

.ncx file when titlesec is not imported:

<?xml version="1.0" encoding="utf-8"?> 
 <!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" 
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"> 
 <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">                                                                              <head> 
 <meta name="dtb:uid" content="http://example.com/mwe"/> 
 <meta name="dtb:depth" content="3"/> 
 <meta name="dtb:totalPageCount" content="0"/> 
 <meta name="dtb:maxPageNumber" content="0"/> 
 </head> 

<docTitle> 
<text>Unnamed</text> 
</docTitle> 
 <navMap>       
<navPoint id="navPoint-1" playOrder="1"> 
<navLabel> 
<text><navmark type="part">  I</navmark> Mammalia</text> 
</navLabel> 
<content src="mwepa1.html#x2-1000I" />        
<navPoint id="navPoint-2" playOrder="2"> 
<navLabel> 
<text><navmark type="chapter">  1</navmark> Monotremata</text> 
</navLabel> 
<content src="mwech1.html#x3-20001" />        
<navPoint id="navPoint-3" playOrder="3"> 
<navLabel> 
<text><navmark type="section">  1.1</navmark> Platypoda</text> 
</navLabel> 
<content src="mwech1.html#x3-30001.1" />
 
<navPoint id="navPoint-4" playOrder="4"> 
<navLabel> 
<text><navmark type="subsection">  1.1.1</navmark> Ornithorhynchidae</text> 
</navLabel> 
<content src="mwech1.html#x3-40001.1.1" />  </navPoint> 
</navPoint> 
     
<navPoint id="navPoint-5" playOrder="5"> 
<navLabel> 
<text><navmark type="section">  1.2</navmark> Tachyglossa</text> 
</navLabel> 
<content src="mwech1.html#x3-70001.2" />       
<navPoint id="navPoint-6" playOrder="6"> 
<navLabel> 
<text><navmark type="subsection">  1.2.1</navmark> Tachyglossidae</text> 
</navLabel> 
<content src="mwech1.html#x3-80001.2.1" />
   </navPoint> 
</navPoint> 
</navPoint> 
</navPoint> 
</navMap>

   </ncx> 

Output when titlesec is imported:

enter image description here

.ncx file when titlesec is imported:

   <?xml version="1.0" encoding="utf-8"?> 
 <!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" 
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"> 
 <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">                                                                                  <head> 
 <meta name="dtb:uid" content="http://example.com/mwe"/> 
 <meta name="dtb:depth" content="3"/> 
 <meta name="dtb:totalPageCount" content="0"/> 
 <meta name="dtb:maxPageNumber" content="0"/> 
 </head> 
 <docTitle> 
<text>Unnamed</text> 
</docTitle> 

<navMap>     
<navPoint id="navPoint-1" playOrder="1"> 
<navLabel> 
<text><navmark type="part">  I</navmark> Mammalia</text> 
</navLabel> 
<content src="mwepa1.html#x2-1000I" />        
<navPoint id="navPoint-2" playOrder="2"> 
<navLabel> 
<text><navmark type="chapter">  1</navmark> Monotremata</text> 
</navLabel> 
<content src="mwech1.html#x3-20001" />        
<navPoint id="navPoint-3" playOrder="3"> 
<navLabel> 
<text><navmark type="section">  1.1</navmark> Platypoda</text> 
</navLabel> 
<content src="mwech1.html#x3-30001" />  </navPoint> 
      
<navPoint id="navPoint-4" playOrder="4"> 
<navLabel> 
<text><navmark type="section">  </navmark>
1.1                                                 Platypoda</text> 
</navLabel> 
<content src="mwech1.html#Q1-3-4" />       
<navPoint id="navPoint-5" playOrder="5"> 
<navLabel> 
<text><navmark type="subsection">  1.1.1</navmark>
Ornithorhynchidae</text> 
</navLabel> 
<content src="mwech1.html#x3-40001" /> </navPoint> 
     
<navPoint id="navPoint-6" playOrder="6"> 
<navLabel> 
<text><navmark type="subsection"> </navmark> 1.1.1
Ornithorhynchidae</text> 
</navLabel> 
<content src="mwech1.html#Q1-3-6" />     </navPoint> 
</navPoint> 
              
<navPoint id="navPoint-7" playOrder="7"> 
<navLabel> 
<text><navmark type="section">     1.2</navmark>   Tachyglossa</text> 
</navLabel> 
<content src="mwech1.html#x3-70002" />     </navPoint> 
              
<navPoint id="navPoint-8" playOrder="8"> 
<navLabel> 
<text><navmark type="section">     </navmark>
1.2                                                 Tachyglossa</text> 
</navLabel> 
<content src="mwech1.html#Q1-3-12" />      
<navPoint id="navPoint-9" playOrder="9"> 
<navLabel> 
<text><navmark type="subsection">  1.2.1</navmark>
Tachyglossidae</text> 
</navLabel> 
<content src="mwech1.html#x3-80001" />  </navPoint> 
     
<navPoint id="navPoint-10" playOrder="10"> 
<navLabel> 
<text><navmark type="subsection">  </navmark> 1.2.1
Tachyglossidae</text> 
</navLabel> 
<content src="mwech1.html#Q1-3-14" />
   </navPoint> 
</navPoint> 
</navPoint> 
</navPoint> 
</navMap>
   </ncx> 

One Answer

Try this version of titlesec.4ht:

% titlesec.4ht (2020-11-20-14:29), generated from tex4ht-4ht.tex
% Copyright 2000-2009 Eitan M. Gurari
% Copyright 2009-2020 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
immediatewrite-1{version 2020-11-20-14:29}

ifx ttl@assign@ii:Undef
   ifx ttl@sect:UnDefelse
     pend:defIIttl@sect{%
  @ifstar {}{SkipRefstepAnchor}}
letttl:select=ttl@select
defttl@select#1#2#3#4#5{%
  edefsc:tp{ifttl@labelelse likefi #1}defc:secnumdepth{#5}%
  ttl:select{#1}{#2}{#3}{#4}{#5}}
letttl@write:gobbleIII

   fi
   ifx ttl@useclass:UnDefelse
     pend:defIIttl@useclass{%
  @ifstar {}{SkipRefstepAnchor}}
letttl:selectttl@select
defttl@select#1{%
  edefsc:tp{ifttl@labelelse likefi
             #1}defc:secnumdepth{@nameuse{ttll@#1}}%
  ttl:select{#1}}
letttl@write:gobbleII
letttl@glcmdsrelax
letttl@beginlongest@empty
letttl@midlongest@empty
letttl@endlongest@empty

   fi
   letttlh:hangttlh@hang
defttlh@hang#1#2#3#4#5#6#7#8{%
   HtmlEnv
   expandafterdefcsname thetitlesc:tpendcsname{#2}%
   csname no:sc:tpendcsname{#8}%
   par ttlh:hang{}{}{#3}{}{}{#6}{#7}{}}
let:seccntformat=@seccntformat
def@seccntformat#1{ifnum 0=0thecsname c@#1endcsnameelse
   :seccntformat{#1}fi}

else
   letttl:straight@ittl@straight@i
defttl@straight@i#1[#2]#3{%
  gdefNR:Title{a:newlabel{#3}}%
%
  ifttl@label else
     def:temp{#1}edef:tempa{expandafter
    expandafterexpandafter:gobble
    expandafterstringcsname sectionendcsname}ifx :temp:tempa else
       edef:tempa{expandafterexpandafterexpandafter:gobble
  expandafterstringcsname subsectionendcsname}ifx :temp:tempa else
     {ttl@labeltrue ttl@addcontentsline{like#1}{#3}}%
fi
%
fi
%
  fi
  edefsc:tp{ifttl@labelelse likefi #1}%
  ttl:straight@i{#1}[{#2}]{#3}%
}
defttlh@hang#1#2#3#4#5#6#7#8{%
   defc:secnumdepth{@nameuse{ttll@sc:tp}}%
   :StartSec {sc:tp}{%
         ifttl@label ifnum c:secnumdepth >c@secnumdepth
         else csname thesc:tpendcsname fi fi
       }{#8}%
}
def:tempc#1#2#3#4{%
  begingroup
  leteverypar@gobble% don't let titlesec to break our paragraph handling
  edefsc:tp{ifttl@labelelse likefi #1}%
   letttl@savewriteempty
   o:ttl@select:{#1}{#2}{#3}{#4}
   endgroup
}

HLetttl@select:tempc
letttlh@runinttlh@hang
letttlh@displayttlh@hang

fi
defttl@addcontentsline#1#2{%
  nobreak}
Hinput{titlesec}
endinput

It seems that Titlesec adds each section or subsection (not chapter or part) explicitly to TOC, but TeX4ht does this as well, so you get duplicate entries.

We can disable Titlesec's code that adds them to TOC using this redefinition:

defttl@addcontentsline#1#2{%
  nobreak}

enter image description here

Correct answer by michal.h21 on March 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP