# tex4ht: backref option of hyperref not working well

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}


There is no problem during the compilation in tex4ebook. However, the backref links are broken. Any workaround (or backref is not considered a good concept for ebooks)?

This is not so easy. The backref option just inserts link back to the page. As pages don't exist in the HTML file, they don't point anywhere.

What is necessary, is to generate unique ID for each \cite command. It will be then possible to link back to it. Try the following .cfg file:

\Preamble{xhtml}
\makeatletter

% Generate ID for the current citation
\@ifundefined{#1-\thepage}{% Save the information only once
% Save the generated ID to the xref file
% Save the current HTML file
\Tag{filename-#1-\thepage}{\FileName}%
}{}%
\global\@namedef{#1-\thepage}{}%
% Insert link to the bibliography and the current ID
}

% Save the current bibkey, we will need it to get the correct backref
\makeatother

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

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


Important parts are the following:

\newcounter{citelinkcnt}
% Generate ID for the current citation
\@ifundefined{#1-\thepage}{% Save the information only once
% Save the generated ID to the xref file
% Save the current HTML file
\Tag{filename-#1-\thepage}{\FileName}%
}{}%
\global\@namedef{#1-\thepage}{}%
% Insert link to the bibliography and the current ID
}


The \citelink command will insert a following code to the generated HTML file: <a id='bk-Xfoo1' href='#Xfoo'>1</a>. The id attribute is construed using prefix bk-, current cite key and \cite counter, as it is necessary that each id attribute is unique.

The \Tag command saves information about the current HTML file and id to the .xref file. This information will be later used to link back. It uses the cite key and page number to get unique value for each \cite. If the cite key is used more than once on a page, only the first one is saved, as we can link only to one of them.

The back links are inserted using the following code:

% Save the current bibkey, we will need it to get the correct backref

The \backrefxxx command inserts the link. The \Link command takes the destination filename as the argument in the square braces, next parameter contains the destination ID. The \LikeRef command retrieves information from the .xref file. It generates the following code:
 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>