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

Attachment: config.cfg
Description: Binary data

\documentclass[]{book}

\usepackage[]{hyperref}
\usepackage{listings}

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

\begin{document}

\chapter{One}\label{chap:label}

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

\chapter{Two}

Program~\ref{fig:label} (see page~\pageref{fig:label}).

Chapter \ref{chap:label}.

\end{document}

%%% Local Variables:
%%% mode: latex
%%% ispell-local-dictionary: "francais"
%%% End:

Reply via email to