tex4ht:「hyperref」的「backref」選項無法正常運作

tex4ht:「hyperref」的「backref」選項無法正常運作

微量元素:

\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。但是,backref 連結已損壞。有什麼解決方法(或 backref 不被認為是電子書的好概念)?

答案1

這並不容易。 backref 選項只是插入返回頁面的連結。由於 HTML 文件中不存在頁面,因此它們不指向任何地方。

必要的是為每個\cite命令產生唯一的 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>.這個屬性使用 prefix 、 current cite key 和counterid進行解釋,因為每個屬性必須是唯一的。bk-\citeid

\Tag命令將有關目前 HTML 文件的資訊儲存id到該.xref文件中。該資訊稍後將用於連結回來。它使用引用鍵和頁碼來獲取每個 的唯一值\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>

相關內容