
МВЭ:
\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
команды. Тогда можно будет ссылаться на нее. Попробуйте следующий .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-
, текущего ключа цитирования и \cite
счетчика, поскольку необходимо, чтобы каждый id
атрибут был уникальным.
Команда \Tag
сохраняет информацию о текущем HTML-файле и id
в .xref
файле. Эта информация будет позже использована для обратной ссылки. Она использует ключ cite и номер страницы, чтобы получить уникальное значение для каждого \cite
. Если ключ 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
Команда принимает имя файла назначения в качестве аргумента в квадратных скобках, следующий параметр содержит идентификатор назначения. \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>