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.

Synopsis:

(0) throughout, I'm checking the correctness of the EPUB with "epubcheck"
(version  4.2.6-2)

(1) I'm trying to format a code listing (using lstinputlisting) within a
minipage, within a figure.

(2) I want references to this figure.  I have read that to get a reference
to a figure, one must put the \label immediately after the \caption.  But
if I do that (as explained below) I get the wrong reference text ("1.1"
instead of "1") because the reference uses the wrong counter (I guess).

(3) But I have a counter for these program listings, and I want the
reference to be to the counter.  So instead of "1.1" for the reference, I
want it to be "1".

(4) When I generate a PDF from the MWE, all three \ref at the bottom of the
document work and go to the right locations.

(5) when I generate an EPUB from the MWE, the -chapter- \ref still works,
but the two refs to the program listing do not work (though they format to
the correct/desired text).  "epubcheck" outputs three errors (of which the
first does not seem to be related to this bug):
==== Errors
ERROR(RSC-005): mwe3.epub/OEBPS/mwe3ch1.html(13,59): Error while parsing
file: value of attribute "id" is invalid; must be an XML name without colons
ERROR(RSC-012): mwe3.epub/OEBPS/mwe3ch2.html(13,64): Fragment identifier is
not defined.
ERROR(RSC-012): mwe3.epub/OEBPS/mwe3ch2.html(13,132): Fragment identifier
is not defined.
==== Text of chapter 2 page
Program 1 (see page 5).

Chapter 1.
====

(6) if I move the "\label" from after the "\lstinputlisting" to after the
"\caption", the two refs to the code listing work, but the text of the
first \ref is "1.1".  with this change, epubcheck still outputs the first
error, but not the latter ones.
==== Text of chapter 2 page
Program 1.1 (see page 5).

Chapter 1.
====

I've tried about a zillion things, but cannot figure out how to get
references to code listings (where the text is based on the "program"
counter) to work.

Any advice would be greatly appreciated.

Thanks,
--chet--
\documentclass[]{book}

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

\newcounter{program}
\renewcommand{\theprogram}{\arabic{program}}

\begin{document}

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

{
\begin{figure}[tp]
{
\refstepcounter{program}
\begin{minipage}{\textwidth}
{Title}
\lstinputlisting{/usr/share/texlive/README}%
\label{fig:label}
\end{minipage}
}
\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