[tex4ht] Perplexing problem with internal refs when generating ebooks

Michal Hoftich michal.h21 at gmail.com
Thu Feb 20 14:13:56 CET 2025


Hi Chet,



> I've been fighting a perplexing problem with internal refs when generating
> an ebook.  I've boiled it down to an MWE, but really don't know how to
> proceed.  It seems that people attach MWE as MIME attachments, and that's
> what I'll do.
>
> I'm not a real expert in TeX, so perhaps I've left a few things in this
> MWE that I could/should have left out. I hope it's small enough.  Also, I
> should ask if there are any good tutorials or references for how to get
> \label/\ref to work well with tex4ebook/tex4ht, esp. in the presence of
> figure, minipage, listinputlisting environments.  I left in the chapters,
> so that the generated EPUB would have multiple pages (so I could verify
> that the links were broken by testing them in ebook-viewer, as well as by
> epubcheck).
>
> Environment:
>
>  I'm using:
> Ubuntu 24.04 LTS
> texlive 2023.20240207-1
>
> and the tex4ht and tex4ebook that come with the packages
> texlive-extra-utils and texlive-binaries.
>


it seems there are multiple problems:

1. you are probably using too old version of TeX4ht and TeX4ebook, you
shouldn't get epubcheck errors about colons in id with the up-to-date
version.
2. I would change your TeX code. First of all, I would use a custom
environment that would replace the minipage, update the program counter,
and print the program title.

The declaration can look like this:

\newcounter{program}
\renewcommand{\theprogram}{\arabic{program}}
\newcommand\printprogram[1]{\theprogram\space #1\par}
\newenvironment{program}[1]{\refstepcounter{program}\minipage{\textwidth}\printprogram{#1}}{\endminipage}

You can then simplify your listings:

\begin{figure}[tp]
\begin{program}{Title}
\label{fig:label}
\lstinputlisting{/usr/share/texlive/README}%
\end{program}
\caption{Caption}
\end{figure}

Note that you need to use \label directly after \begin{program}, because
\lstinputlisting internally updates reference counters so that the link
would be wrong.

For TeX4ht, you will need to use a config file:

%%%%%%%%%%%%
\Preamble{xhtml}
\ConfigureEnv{program}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div
class="program">}\Configure{minipage}{}{}{}{}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}
\renewcommand\printprogram[1]{\ifvmode\IgnorePar\fi\EndP
\HCode{<div
class="programname">}\AnchorLabel\theprogram\quad#1\HCode{</div>}\par}

\begin{document}
\EndPreamble
%%%%%%%%%%%%

It adds some HTML tags for the program and program title, to enable CSS
styling. For example, if you want to make the title bold, you could add
something like:

\Css{.programname{text-weight: bold;}}

to the config file. Also, note the \AnchorLabel command. It will insert a
destination link for the current label. This will ensure that \ref and
\pageref will point to the right place.

The full example is attached. Compile using:

$ tex4ebook -c config.cfg sample.tex

Best regards,
Michal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://tug.org/pipermail/tex4ht/attachments/20250220/083068cc/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: config.cfg
Type: application/octet-stream
Size: 982 bytes
Desc: not available
URL: <https://tug.org/pipermail/tex4ht/attachments/20250220/083068cc/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sample.tex
Type: text/x-tex
Size: 724 bytes
Desc: not available
URL: <https://tug.org/pipermail/tex4ht/attachments/20250220/083068cc/attachment.bin>


More information about the tex4ht mailing list.