tex4ht: La opción `backref` de `hyperref` no funciona bien

tex4ht: La opción `backref` de `hyperref` no funciona bien

MWE:

\documentclass{article}
\usepackage[backref=page]{hyperref}

\renewcommand*{\backref}[1]{}
\renewcommand*{\backrefalt}[4]{[{\tiny%
      \ifcase #1 Not cited%
      \or Cited in page~#2.%
      \else Cited in pages #2.%
      \fi%
}]}

\begin{document}  
\cite{foo,bar,hole}

\begin{thebibliography}{9}
\bibitem{foo} foo

\bibitem{bar} bar

\bibitem{hole} hole

\end{thebibliography}  
\end{document}

No hay ningún problema durante la compilación en tex4ebook. Sin embargo, los enlaces de referencia están rotos. ¿Alguna solución alternativa (o la referencia inversa no se considera un buen concepto para los libros electrónicos)?

Respuesta1

Esto no es fácil. La opción backref simplemente inserta un enlace a la página. Como las páginas no existen en el archivo HTML, no apuntan a ninguna parte.

Lo que es necesario es generar una identificación única para cada \citecomando. Entonces será posible volver a vincularlo. Pruebe el siguiente .cfgarchivo:

\Preamble{xhtml}
\makeatletter

\newcounter{citelinkcnt}
\newcommand\citelink[2]{%
% Generate ID for the current citation
\stepcounter{citelinkcnt}%%
\@ifundefined{#1-\thepage}{% Save the information only once
% Save the generated ID to the xref file
\Tag{backlink-#1-\thepage}{bk-#1\thecitelinkcnt}%
% Save the current HTML file
\Tag{filename-#1-\thepage}{\FileName}%
}{}%
\global\@namedef{#1-\thepage}{}%
% Insert link to the bibliography and the current ID
\Link{#1}{bk-#1\thecitelinkcnt}%
}

% Save the current bibkey, we will need it to get the correct backref
\newcommand\mybiblink[2]{\Link{#1}{#2}\def\currentbibitem{#2}}
% Insert the back link
\renewcommand\backrefxxx[3]{\Link[\LikeRef{filename-\currentbibitem-#1}]{\LikeRef{backlink-\currentbibitem-#1}}{}#1\EndLink}
\makeatother

% Configure cite to save the citation ID
\Configure{cite}
   {\HCode{<span class="cite">}}  {\HCode{</span>}}
   {\citelink}         {\EndLink}

% Configure bibitem to save the bibkey
\Configure{bibitem}{\mybiblink}{\EndLink}
\begin{document}
\EndPreamble

Las partes importantes son las siguientes:

\newcounter{citelinkcnt}
\newcommand\citelink[2]{%
% Generate ID for the current citation
\stepcounter{citelinkcnt}%%
\@ifundefined{#1-\thepage}{% Save the information only once
% Save the generated ID to the xref file
\Tag{backlink-#1-\thepage}{bk-#1\thecitelinkcnt}%
% Save the current HTML file
\Tag{filename-#1-\thepage}{\FileName}%
}{}%
\global\@namedef{#1-\thepage}{}%
% Insert link to the bibliography and the current ID
\Link{#1}{bk-#1\thecitelinkcnt}%
}

El \citelinkcomando insertará el siguiente código en el archivo HTML generado: <a id='bk-Xfoo1' href='#Xfoo'>1</a>. El idatributo se construye utilizando prefijo bk-, clave de cita actual y \citecontador, ya que es necesario que cada idatributo sea único.

El \Tagcomando guarda información sobre el archivo HTML actual y iden el .xrefarchivo. Esta información se utilizará más adelante para vincular nuevamente. Utiliza la clave de cita y el número de página para obtener un valor único para cada uno \cite. Si la clave de cita se usa más de una vez en una página, solo se guarda la primera, ya que solo podemos vincular a una de ellas.

Los enlaces de retroceso se insertan utilizando el siguiente código:

% Save the current bibkey, we will need it to get the correct backref
\newcommand\mybiblink[2]{\Link{#1}{#2}\def\currentbibitem{#2}}
% Insert the back link
\renewcommand\backrefxxx[3]{\Link[\LikeRef{filename-\currentbibitem-#1}]{\LikeRef{backlink-\currentbibitem-#1}}{}#1\EndLink}

El \backrefxxxcomando inserta el enlace. El \Linkcomando toma el nombre del archivo de destino como argumento entre llaves cuadradas, el siguiente parámetro contiene la ID del destino. El \LikeRefcomando recupera información del .xrefarchivo. Genera el siguiente código:

 Cited in pages </span><a href='sample.html#bk-Xfoo1'><span class='cmr-5'>1</span></a> <span class='cmr-5'>and </span><a href='sample.html#bk-Xfoo5'><span class='cmr-5'>4</span></a>

información relacionada