1

The hyperlinks work in compiled pdf file for the following tex document. The section names are blank. However when it is compiled to html using make4ht, the hyperlinks won't work. So there is problem with hyperref and make4ht when section titles are blank.

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\section{}
\section{}
\section{}
\end{document}

How can this be solved?

2
  • What is the use case for this? The link to sections is realized through their title, so you need to provide something, at least non-breakable space (~).
    – michal.h21
    Commented Sep 16, 2018 at 11:56
  • I just want that link gets opened when one clicks on section number. In other words, section numbers are titles of sections. This works in pdf but not in html. Is there any way around? Is it possible to hyperlink section to its section number?
    – user61681
    Commented Sep 16, 2018 at 12:03

1 Answer 1

2

It seems that the configuration for TOC links is quite hard-wired, so it will be easiest to post-process the HTML using make4ht build file. The following code uses the luaxml-domobject library to process the HTML using DOM functions:

local domfilter = require "make4ht-domfilter"

local process = domfilter {function(dom)
  -- find the table of contents
  local toc = dom:query_selector("div.tableofcontents")[1]
  if toc then
    -- process all TOC lines
    for _, span in ipairs(toc:query_selector("span")) do
      -- get a full title
      local text = span:get_text()
      for  i, curr in ipairs(span._children) do
        -- remove the section numbers
        if curr:is_text() then 
          curr:remove_node()
        -- set a full title to the link
        elseif curr:is_element() then
          curr._children = {}
          curr:add_child_node(curr:create_text_node(text))
        end
      end
    end
  end
  return dom
end}

Make:match("html$", process)

This is the result:

enter image description here

<h3 class='likesectionHead'><a id='x1-1000'></a>Contents</h3>
   <div class='tableofcontents'>
   <span class='sectionToc'><a id='QQ2-1-2' href='#x1-20001'>1 </a></span>
<br />   <span class='sectionToc'><a id='QQ2-1-3' href='#x1-30002'>2 </a></span>
<br />   <span class='sectionToc'><a id='QQ2-1-4' href='#x1-40003'>3 </a></span>
   </div>
4
  • Would you please help for how to use this?
    – user61681
    Commented Sep 16, 2018 at 13:29
  • @user61681 Save the build file as mybuild.mk4 and call make4ht -e mybuild.mk4 filename.tex
    – michal.h21
    Commented Sep 16, 2018 at 15:14
  • Thanks for the help. Is it possible to use two mk4 files? Something like this make4ht -e mybuild.mk4 mybuild2.mk4 filename.tex
    – user61681
    Commented Sep 17, 2018 at 2:21
  • @user61681two build files are not possible. but you can use the .make4ht config file, where you can define custom commands or filters, for example. see page 15 of the documentation
    – michal.h21
    Commented Sep 17, 2018 at 6:04

You must log in to answer this question.

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