tex4ht: opção `backref` de `hyperref` não funciona bem

tex4ht: opção `backref` de `hyperref` não funciona bem

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}

Não há problema durante a compilação em tex4ebook. No entanto, os links backref estão quebrados. Alguma solução alternativa (ou backref não é considerado um bom conceito para e-books)?

Responder1

Isto não é tão fácil. A opção backref apenas insere um link de volta para a página. Como as páginas não existem no arquivo HTML, elas não apontam para lugar nenhum.

O que é necessário é gerar um ID único para cada \citecomando. Será então possível vincular de volta a ele. Experimente o seguinte .cfgarquivo:

\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

Partes importantes são as seguintes:

\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}%
}

O \citelinkcomando irá inserir o seguinte código no arquivo HTML gerado: <a id='bk-Xfoo1' href='#Xfoo'>1</a>. O idatributo é construído utilizando prefixo bk-, chave de citação atual e \citecontador, pois é necessário que cada idatributo seja único.

O \Tagcomando salva informações sobre o arquivo HTML atual e idno .xrefarquivo. Esta informação será usada posteriormente para vincular de volta. Ele usa a chave de citação e o número da página para obter um valor exclusivo para cada um \cite. Se a chave de citação for usada mais de uma vez em uma página, apenas a primeira será salva, pois só podemos vincular a uma delas.

Os backlinks são inseridos usando o seguinte 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}

O \backrefxxxcomando insere o link. O \Linkcomando usa o nome do arquivo de destino como argumento entre colchetes, o próximo parâmetro contém o ID de destino. O \LikeRefcomando recupera informações do .xrefarquivo. Ele gera o seguinte 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>

informação relacionada