# TeX4ht / tex4ebook: labeling unnumbered equations

I am trying to understand whether it is possible to allow labels in equation*-environments as long as they are not referred. This is allowed in pdflatex, but when compiling with tex4ebook (with default parameters), it will produce an error. Here is a MWE:

\documentclass{amsart}

\begin{document}
\section{Introduction}
\begin{equation*}\label{py}
a^2+b^2=c^2
\end{equation*}
\end{document}


And the error message of tex4ebook:

[ERROR]   htlatex: Compilation errors in the htlatex run
[ERROR]   htlatex: Filename Line    Message
[ERROR]   htlatex: ./test.tex   2    Extra \endcsname.
[ERROR]   htlatex: ./test.4ct   2    Missing \endcsname inserted.
[ERROR]   htlatex: ./test.4ct   2    Missing \endcsname inserted.
[ERROR]   htlatex: ?    8    Missing \endcsname inserted.
[ERROR]   htlatex: ?    8    Missing \endcsname inserted.
[ERROR]   htlatex: ?    8    Missing \endcsname inserted.
[ERROR]   htlatex: ?    8    Missing \endcsname inserted.
[ERROR]   htlatex: ?    8    Missing \endcsname inserted.
[ERROR]   htlatex: Compilation errors in the htlatex run


Remark: if one deletes the line \section{Introduction}, then the error vanishes.

• Please be more specific what you are doing, and what the error is. Running htlatex as well as latex + tex4ht on your MWE doesn't give an error, but produces the expected result. (TeXlive 2021, Linux) Aug 20 at 9:23
• @gernot Thanks. I have added a bit more details about the context. Aug 20 at 9:34
• ah, this happens only with tex4ebook, not with make4ht, so it must be something wrong in tex4ebook itself Aug 20 at 19:54

First of all, what purpose has \label on unnumbered equation? It cannot be used to reference to that equation. When you put any object that can be referenced to, like \section in your case, the \label will point to that object, no to the equation. So it cannot work as intended.

However, even if it doesn't serve an useful function, it is indeed unfortunate that it produces an error. tex4ebook needs to track all HTML files and their order. All sections or chapters produce separate files, and tex4ebook reads special TOC file to find their order. The file has .4tc extension and it can look like this:

\doTocEntry\tocsection{1}{\csname a:TocLink\endcsname{2}{x2-10001}{QQ2-2-1}{Introduction}}{1}\relax


The a:TocLink macro has lot of parameters, but important for our use case is x2-10001. It contains the section ID. We then need to resolve the HTML filename for this section. TeX4ht keeps track of all ID's and files in the .xref file:

...
\:CrossWord{)F2F-}{samplese1.html}{1}%
...
\:CrossWord{)Qx2-10001}{2}{1}%
\:CrossWord{)Qx2-10001}{2}{1}%


TeX4ht puts some prefix for each referencable item, so files are surrounded by )F<file number>F-, and \label IDS have )Q<ID>. As you can see, there is duplicate entry for the section ID. It is produced by \label inside equation*. If you put \label directly after \section, this problem wouldn't happen. Why is it a problem? You can read value of \:CrossWord using \Ref command. \Ref{)Qx2-10001} should produce 2, which is the number of HTML file that contains this section. But if there are duplicate IDS, it will produce their list separated by commas, so it produces 2, 2 in our case.

tex4ebook uses nested \Ref commands to find the section HTML filename:

 \Ref{)F\Ref{)Q#1}F-}


Normally, it would produce samplese1.html, but because of duplicate ID, it doesn't return the filename and it produces an error message instead.

To fix that, we can get only the first item returned by \Ref{)Qx2-10001} and use that to find the filename. It needs quite bit change in the tex4ebook sources, but you can emulate it using the following configuration file:

\Preamble{xhtml}
\begin{document}
\ExplSyntaxOn
% get filename for the section label
% sometimes, TeX4ht returns list of file numbers for label. we must use just
% the first number. we use the LaTeX 3 sequence functions to achieve that
\tl_new:N\ncx:hfilename
\def\ncx:newhfile#1{
% cw:)Q#1 is csname of tag from the xref file
\seq_set_from_clist:Nc\l_tmpa_seq{cw:)Q#1}
\seq_get_left:NN \l_tmpa_seq \l_tmpa_tl
%\tl_to_str:N \l_tmpa_tl
% \RefFileNumber returns file name for the given file number
\tl_set:Nx \ncx:hfilename {\RefFileNumber{\l_tmpa_tl}}
}
\ExplSyntaxOff

\catcode\:=11
\def\ncxtable{%
\EndP
\HtmlParOff
\setcounter{tocdepth}{3}
\special{t4ht>\jobname.ncx}
% We don't want crosslinks in xml
% Basic sctructure of the ncx file
\NoFonts
{\HCode{<?xml version="1.0" encoding="utf-8"?>\Hnewline}}
{\a:NcxDoctype}
{\Tg<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">}{}
% To print document map, we customize the tableofcontents. We don't want TOC title,
% so:
\let\contentsname=\empty
\Configure{tableofcontents}{\boolfalse{tocnoempty}\Tg<navMap>}{%
\usetoclevels{part,appendix,chapter,likechapter,appendixsec,section,likesection,subsection,appendixsubsec,likesubsection,subsubsection,likesubsubsection,appendixsubsubsec,subsubsubsection,paragraph}%
\ifbool{tocnoempty}{}%
{\HCode{<navPoint id="mainentry" playOrder="1">
<navLabel><text>Document</text></navLabel>
<content src="\jobname.\:html" />
</navPoint>}}%
\Tg</navMap>}{}{}{}
% We need to configure TocLink
% in navmapsrc is link to the file and anchor, where chapter or section is located
\def\navmapsrc{}
\ncx:newhfile{##2}
\def\navmapsrc{\ncx:hfilename\:sharp ##2}
\opf:registerfilename{\ncx:hfilename}
##4
}
% Configuraion of entries
\expandafter\resettoclevels\expandafter{\a:resettoclevels}%
\confnavsections%
\HtmlParOff%
\Configure{NavSection}{\booltrue{tocnoempty}\HCode{\Hnewline<navPoint id="navPoint-}%
\stepnavpoint\HCode{" playOrder="}%
\the\navpoint\HCode{">\Hnewline<navLabel>\Hnewline<text><navmark type="\curr:sect:type">}%
}{\HCode{</text>\Hnewline%
</navLabel>\Hnewline}%
\HCode{<content src="\navmapsrc" />}%
}{\HCode{</navmark>}}{\HCode{</navPoint>\Hnewline}}
% Meta inf
% Book title
\ncx:title
\tableofcontents[part,appendix,chapter,likechapter,appendixsec,section,likesection,appendixsubsec,subsection,likesubsection]%
%Hack to get close tag working
\HCode{</ncx>}
\EndNoFonts
\special{t4ht<\jobname.ncx}
\HtmlParOn
}
\catcode\:=12
\EndPreamble


The important code is this:

\tl_new:N\ncx:hfilename
\def\ncx:newhfile#1{
% cw:)Q#1 is csname of tag from the xref file
\seq_set_from_clist:Nc\l_tmpa_seq{cw:)Q#1}
\seq_get_left:NN \l_tmpa_seq \l_tmpa_tl
%\tl_to_str:N \l_tmpa_tl
% \RefFileNumber returns file name for the given file number
\tl_set:Nx \ncx:hfilename {\RefFileNumber{\l_tmpa_tl}}
}


It uses the LaTeX 3 sequence list to parse the comma separated list and to return the first item. \seq_set_from_clist:Nc\l_tmpa_seq{cw:)Q#1} uses the control sequence used internally to keep the reference. \RefFileNumber can be used instad of \Ref{)F<number>F-}. The \ncx:hfilename token list is then defined. It can be used in register the filename in \ncxtable:

 \Configure{TocLink}{%
\ncx:newhfile{##2}
\def\navmapsrc{\ncx:hfilename\:sharp ##2}
\opf:registerfilename{\ncx:hfilename}
##4
}


This inserts correct link to the Epub TOC, and also registers the HTML file.

Compile using

 tex4ebook -c config.cfg filename.tex

• This kind of labels usually appear in the case that the equation was at first labeled and referred to, then modified since it is no longer referred to, I guess. Aug 23 at 11:08
• @Yai0Phah ah, it makes sense then. I've updated tex4ebook sources, so this should be fixed in the next release. Aug 23 at 12:32