3

I have a custom list of <things> detailed in the following example:

\documentclass{article}

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

\begin{document}
\cmhcommand{Here is some text}
\listofcmh
\end{document}

When I compile the code with pdflatex, I receive the desired output:

screenshot of pdflatex output

When I compile with htlatex using

htlatex myfile.tex

I receive the following output

screenshot of html output

I've also pasted the html, for reference.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  "http://www.w3.org/TR/html4/loose.dtd">  
<html > 
<head><title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)"> 
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)"> 
<!-- html --> 
<meta name="src" content="final_report.tex"> 
<link rel="stylesheet" type="text/css" href="final_report.css"> 
</head><body 
>
<a 
 id="x1-2"></a>
   <h4 class="likesubsectionHead"><a 
 id="x1-1000"></a>List of cmh</h4>

</body></html> 

The question

How can I configure htlatex to output my custom list of <things>? If possible, there would be hyperlinks between each <thing> and its associated place in the <list of things>.

cmhughes
  • 100,947

1 Answers1

4

If you want to use custom TOC like list, you need to make a configuration for it. In your case, I would create sty file with your custom commands:

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

and corresponding .4ht file with tex4ht configurations:

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

In the cmhloc.4ht file, we need to do two things:

  • redefine the \listofcmh file to print our custom table of contents. It is done using \TableOfContents[cmh]
  • we also need to configure styling of TOC entries, using \ConfigureToc{cmh}. From the info output:

\ConfigureToc{unit-name} ......................4

#1 before unit number #2 before content #3 before page number #4 at end

  • Empty arguments request the omission of the corresponding field.

  • \TocCount Specifies the entry count withing the jobname.4tc file.

  • \TitleCount Count of entries submitted to the toc file

  • An alternative to \ConfigureToc{unit-name}:

    \def\toc#1#2#3{#1#2% #3}

    Example:

    \ConfigureToc{section}
       {}
       {\Picture[*]{pic.jpg width="13"  height="13"}~}
       {}
       {\HCode{<br />}}
    

We don't want to print the page numbers, so we need to leave the third parameter empty, but we need to put something to second parameter, otherwise the content text would disappear, so I've put ~ here.

I've expanded your example a little bit:

\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}

And this is the result:

enter image description here

  <h4 class="likesubsectionHead"><a 
 id="x1-40002"></a>List of cmh</h4>
   <div class="tableofcontents"><div class="sectionToc"> <a 
href="#x1-2001">Here is some text</a></div> 
<div class="sectionToc"> <a 
href="#x1-3001">Another text</a></div> 
<div class="sectionToc"> <a 
href="#x1-3002">some more</a></div> 

   </div> 

Edit:

regarding your comment, you can use hyperref to link back to the TOC:

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

and the configuration:

\ConfigureToc{cmh}{\HCode{<div class="sectionToc">}}{~}{}{\HCode{</div>\Hnewline}} 
% \renewcommand\listofcmh{\subsection*{List of cmh}\phantomsection\label{listcmh}\TableOfContents[cmh]}
\append:def\listofcmh{\TableOfContents[cmh]}
\endinput

I used \append:def command to just append the \TableOfContents command instead of redefining the whole macro.

michal.h21
  • 50,697
  • This is superb, many thanks indeed! At present, the links in List of cmh link back to their positions -- is it possible to link from the position to the place in List of cmh? – cmhughes Sep 22 '17 at 12:13
  • @cmhughes please see the edited answer – michal.h21 Sep 22 '17 at 12:55
  • thanks so much for your time on this. Forgive me, I was trying to describe that each occurrence of \cmhcommand could link to its relevant place in the List of cmh. The idea is to have bi-directional links. I'd be happy to open a new question, if you feel it's a separate issue from that originally described. – cmhughes Sep 22 '17 at 17:11
  • @cmhughes maybe it deserves it's own question, it will be harder than the original one :) – michal.h21 Sep 22 '17 at 18:08
  • Ok, no problem :) will link it here later on :) – cmhughes Sep 22 '17 at 18:47
  • Thanks again for all of your time. If you have the time to take a look, I've posted a new question here: https://tex.stackexchange.com/questions/392824/tex4ht-custom-list-of-part-2-of-2 Thanks again – cmhughes Sep 23 '17 at 08:37
  • My apologies -- I hadn't realised I hadn't accepted this, it was an error on my part – cmhughes Jan 06 '18 at 13:23