
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}
Beim Kompilieren in gibt es kein Problem tex4ebook
. Allerdings sind die Backref-Links defekt. Gibt es irgendeinen Workaround (oder wird Backref nicht als gutes Konzept für E-Books angesehen)?
Antwort1
Das ist nicht so einfach. Die Option „backref“ fügt lediglich einen Link zurück zur Seite ein. Da die Seiten in der HTML-Datei nicht vorhanden sind, zeigen sie nirgendwohin.
Es ist notwendig, für jeden Befehl eine eindeutige ID zu generieren \cite
. Anschließend kann darauf zurückverwiesen werden. Versuchen Sie es mit der folgenden .cfg
Datei:
\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
Wichtige Teile sind die folgenden:
\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}%
}
Der \citelink
Befehl fügt den folgenden Code in die generierte HTML-Datei ein: <a id='bk-Xfoo1' href='#Xfoo'>1</a>
. Das id
Attribut wird mithilfe des Präfixes bk-
, des aktuellen Cite-Keys und \cite
des Zählers konstruiert, da jedes id
Attribut eindeutig sein muss.
Der \Tag
Befehl speichert Informationen über die aktuelle HTML-Datei und id
zur .xref
Datei. Diese Informationen werden später zum Zurückverlinken verwendet. Er verwendet den Cite-Key und die Seitenzahl, um für jeden einen eindeutigen Wert zu erhalten \cite
. Wenn der Cite-Key mehr als einmal auf einer Seite verwendet wird, wird nur der erste gespeichert, da wir nur auf einen von ihnen verlinken können.
Die Backlinks werden mit folgendem Code eingefügt:
% 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}
Der \backrefxxx
Befehl fügt den Link ein. Der \Link
Befehl verwendet den Zieldateinamen als Argument in den eckigen Klammern, der nächste Parameter enthält die Ziel-ID. Der \LikeRef
Befehl ruft Informationen aus der .xref
Datei ab. Er generiert den folgenden 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>