tex4ht: `hyperref` の `backref` オプションがうまく動作しない

tex4ht: `hyperref` の `backref` オプションがうまく動作しない

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}

でのコンパイル中に問題はありませんtex4ebook。ただし、バック参照リンクが壊れています。回避策はありますか (または、バック参照は電子書籍に適した概念ではないと考えられます)?

答え1

これはそれほど簡単ではありません。backref オプションは、ページへのリンクを挿入するだけです。ページは HTML ファイル内に存在しないため、どこにも参照されません。

必要なのは、各\citeコマンドに一意の ID を生成することです。そうすれば、その ID にリンクバックできるようになります。次の.cfgファイルを試してください。

\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

重要な部分は次のとおりです。

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

この\citelinkコマンドは、生成された HTML ファイルに次のコードを挿入します: <a id='bk-Xfoo1' href='#Xfoo'>1</a>。各属性は一意である必要があるため、属性はプレフィックス、現在の引用キー、およびカウンターidを使用して解釈されます。bk-\citeid

この\Tagコマンドは、現在の HTML ファイルに関する情報と をファイルidに保存します.xref。この情報は、後でリンクバックするために使用されます。 cite キーとページ番号を使用して、それぞれに一意の値を取得します\cite。 cite キーがページで複数回使用されている場合、そのうちの 1 つにしかリンクできないため、最初の cite キーのみが保存されます。

バックリンクは次のコードを使用して挿入されます。

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

この\backrefxxxコマンドはリンクを挿入します。\Linkコマンドは、角括弧内の引数として宛先ファイル名を取り、次のパラメータには宛先 ID が含まれます。コマンドはファイル\LikeRefから情報を取得します.xref。次のコードが生成されます。

 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>

関連情報