2

This is a direct follow-up to tex4ht: custom list of .... (part 1 of 2)

I have a custom list of <things>. The code below provides a link from each item in List of cmh to its place on the page. I would like (in the html version) for each occurrence of the output of \cmhcommand to link to its relevant place in the List of cmh.

mwe.tex

\documentclass{article}

\usepackage{cmhloc}
\begin{document}
\tableofcontents

\section{normal section}
\cmhcommand{Here is some text}
some text
\section{another secton}
\cmhcommand{Another text}
\cmhcommand{some more}

\listofcmh
\end{document}

cmhloc.sty

\ProvidesPackage{cmhloc}
\newcommand{\cmhcommand}[1]{\addcontentsline{cmh}{subsection}{#1}#1}
\newcommand{\listofcmh}{\subsection*{List of cmh}\@starttoc{cmh}}
\endinput

cmhloc.4ht

\ConfigureToc{cmh}{\HCode{<div class="sectionToc">}}{~}{}{\HCode{</div>\Hnewline}} 
\renewcommand\listofcmh{\subsection*{List of cmh}\TableOfContents[cmh]}
\endinput

The compilation I use to generate the html version is

htlatex mwe.tex
cmhughes
  • 100,947

1 Answers1

2

In this case, we need to construct unique id for each \cmhcommand, which will serve as link to the TOC. We will modify the sty file slightly:

\ProvidesPackage{cmhloc}
\newcommand\printcmhentry[1]{#1}
\newcommand{\cmhcommand}[1]{\addcontentsline{cmh}{subsection}{#1}\printcmhentry{#1}}
\newcommand{\listofcmh}{\subsection*{List of cmh}\@starttoc{cmh}}
\endinput

In this way, we need to redefine only the \printcmhentry command, without need to duplicate the logic of \cmhcommand in the 4ht file:

\RequirePackage{etoolbox}
\def\toccmh#1#2#3{%
  \bgroup% make the changes local
    \Configure{TocLink}{% This will print the link in TOC.
      \Link{##2}{\@nameuse{cmhcn-##4}}##4\EndLink% We need to  construct the ID parameter in second parameter for \Link
    }
    \HCode{<div class="sectionToc">}#2\HCode{</div>\Hnewline}% Print the entry
  \egroup%
}%


\newcount\cmhcount % each cmhentry will have it's unique idntifier
\renewcommand\printcmhentry[1]{%
  \advance\cmhcount by1\relax%
  \edef\cmhlink{cmh-\the\cmhcount}% this will be the id for the corresponding entry in the \listcmh
  \csxdef{cmhcn-#1}{\cmhlink}\Link{\cmhlink}{}#1\EndLink} % we use the entry text in csname, 
                                                          % in order to be able to retrieve the id later
                                                          % in the \listcmh entries

\append:def\listofcmh{\TableOfContents[cmh]}
\endinput

In this case, \toccmh macro is defined instead of \ConfigureToc{cmh}, it is more flexible. We use \Configure{TocLink} to configure the hyperlinks in the TOC entries, the first argument is id of the \addcontentsline, the second one is anchor for the entry. \@nameuse{cmhcn-##4} exapnds to \nameuse{cmhcn-Here is some text} for example. This macro is defined in \renewcommand\printcmhentry and it contains the id the text links to.

This is the result:

<h3 class="sectionHead"><span class="titlemark">1   </span>  <a 
 id="x1-20001"></a>normal section</h3>
<a 
 id="x1-2001"></a>
<!--l. 9--><p class="noindent" ><a 
href="#cmh-1">Here is some text</a> some text
</p>
   <h3 class="sectionHead"><span class="titlemark">2   </span>  <a 
 id="x1-30002"></a>another secton</h3>
<a 
 id="x1-3001"></a>
<!--l. 13--><p class="noindent" ><a 
href="#cmh-2">Another text</a>
</p><!--l. 16--><p class="indent" >   Inside paragraph: <a 
 id="x1-3002"></a><a 
href="#cmh-3">some more</a>. It doesn’t work well.
</p><!--l. 25--><p class="noindent" >
</p>
   <h4 class="likesubsectionHead"> <a 
 id="x1-40002"></a>List of cmh</h4>
   <div class="tableofcontents"> <div class="sectionToc"><a 
href="#x1-2001" id="cmh-1">Here is some text</a></div> 
 <div class="sectionToc"><a 
href="#x1-3001" id="cmh-2">Another text</a></div> 
 <div class="sectionToc"><a 
href="#x1-3002" id="cmh-3">some more</a></div> 

   </div> 
michal.h21
  • 50,697
  • Thank you very much indeed, this is exactly what I need! I hope you'll accept the small bounties I've awarded on this and the other question as tokens of my thanks. I'll award them after a few days, in an attempt to maximise exposure. Thanks again! – cmhughes Sep 25 '17 at 10:12
  • I am glad that it works for you. It was interesting exercise in digging in tex4ht sources, because this stuff isn't really well documented. Documentation is worst pain of tex4ht, I am not really good in writing :( And really thanks for the bounties, I really appreciate it. – michal.h21 Sep 25 '17 at 10:15
  • no worries :) In case you're interested, the final use case is here: http://users.mct.open.ac.uk/chris.hughes/sonification-phase1-report/final_report.html in particular, the Figure description command uses your code :) – cmhughes Sep 25 '17 at 12:51
  • @cmhughes it is actually quite interesting topic. I have only few comments: you should use different address for MathJax script, the cdn.mathjax.org is abandoned. I guess that the old address is still advertised somewhere in my older posts. – michal.h21 Sep 25 '17 at 13:17
  • @cmhughes I think there is a issue with configuration for \textbf and \textit commands, because <b> and <i> tags are sometimes outside paragraphs. see https://tex.stackexchange.com/a/66172/2891 for more information – michal.h21 Sep 25 '17 at 13:20
  • @cmhughes it might be also interesting to use ARIA to help the disabled readers. tex4ht doesn't have support for it yet, but it would be nice to work on it. – michal.h21 Sep 25 '17 at 13:23
  • thanks again for all of your help. If I can trespass on your time a little further, I've posted a follow-up: https://tex.stackexchange.com/questions/460907/tex4ht-custom-list-of-part-2-of-3 – cmhughes Nov 20 '18 at 10:02