1

Here is a MWE that doesn’t do what I want:

\documentclass{article}
\usepackage[colorlinks=true,citecolor=magenta,urlcolor=cyan]{hyperref}

\begin{document} Let's quote \emph{Einstein} \cite{Einstein:1905a}.

\newpage

\begin{thebibliography}{W00} \bibitem[\href{https://einsteinpapers.press.princeton.edu/vol2-doc/311}{E05}]{Einstein:1905a} Albert Einstein, \newblock Zur {E}lektrodynamik bewegter {K}"orper. \newblock \emph{Ann. Physik. (4)} \textbf{17} (1905) 891--921. \end{thebibliography} \end{document}

Desired behavior: In the text (page 1) the label E05 is an internal (magenta) hyperlink to the bibliography entry. Whereas in the bibliography entry itself (page 2) it is an external (cyan) hyperlink to princeton.edu.

Obtained behavior: Both are external (cyan) links to princeton.edu. (When hovering the first link in Skim.app I still see that it wants to point to the second, but clicking it sends me to princeton.edu.)

Question: How do I obtain the desired behavior instead?

Note: I know very well how to obtain the desired behavior using biblatex. My problem is, I have a coauthor who insists on using a manual bbl. (Also, journals don’t like biblatex.)

1 Answers1

2

Don't use \href in the optional argument of \bibitem. The optional argument should only contain the citation label. If you use hyperref the citation label is automatically linked to the bibliography entry.

You can link the whole bibliography entry by wrapping it in \href{<link>}{...}.

\documentclass{article}
\usepackage[colorlinks=true,citecolor=magenta,urlcolor=cyan]{hyperref}

\begin{document} Let's quote \emph{Einstein} \cite{Einstein:1905a}.

\newpage

\begin{thebibliography}{W00} \bibitem[E05]{Einstein:1905a}\href{https://einsteinpapers.press.princeton.edu/vol2-doc/311}{% Albert Einstein, \newblock Zur {E}lektrodynamik bewegter {K}&quot;orper. \newblock \emph{Ann. Physik.~(4)} \textbf{17} (1905) 891--921.} \end{thebibliography} \end{document}

Linked bibliography entry.


Here is an alternative solution that links only the biblabel in the bibliography. We introduce a \linkedbibitem that works like \bibitem but has an additional mandatory argument for the URL (it can be left empty in case the is no URL, but it must always be given, that keeps the definition simpler).

With \linkedbibitem we just save the URL. Then we redefine \@BIBLABEL (the hyperref version of \@biblabel) to apply a \href if a link was given.

We can use hyperref's \hyper@normalise to make the \linkedbibitem accept otherwise special characters like $, _ and even % and # without complaints.

\documentclass{article}
\usepackage[colorlinks=true,citecolor=magenta,urlcolor=cyan]{hyperref}

\usepackage{etoolbox}

\makeatletter \newcommand*{@linkedbibitem}[1]{% \def\this@biblink{#1}% \bibitem}

\newcommand*{\linkedbibitem}{\hyper@normalise@linkedbibitem}

\renewcommand*{@BIBLABEL}[1]{% \ifdefvoid\this@biblink {[#1]} {\expandafter\href\expandafter{\this@biblink}% {[#1]}}} \makeatother

\begin{document} Let's quote \emph{Einstein} \cite{Einstein:1905a}.

\newpage

\begin{thebibliography}{W00} \linkedbibitem{https://einsteinpapers.press.princeton.edu/vol2-doc/311#a}[E05]{Einstein:1905a} Albert Einstein, \newblock Zur {E}lektrodynamik bewegter {K}&quot;orper. \newblock \emph{Ann. Physik.~(4)} \textbf{17} (1905) 891--921. \end{thebibliography} \end{document}

moewe
  • 175,683
  • Thanks, but I want E05 to be the cyan link, because it looks a lot nicer once there are a few bibliography entries. I know it’s possible, because biblatex can do it. – Consigliere ZARF Jul 12 '20 at 15:46
  • @ConsigliereZARF Ah, I see. It is possible, but a bit more tricky and the required definitions will depend on the document class you use and other bibliography-related package you load (is that MWE representative of your actual document in that regard?). (There mere fact that something is possible with biblatex does not necessarily mean that the same thing is (easily) possible with the standard thebibliography or with BibTeX: they are very different systems.) – moewe Jul 12 '20 at 15:47
  • I agree that I probably shouldn’t use \href in the optional argument of \bibitem. My problem is then, how to scrap that label and replace it by an external-hyperlinked one in the references list. – Consigliere ZARF Jul 12 '20 at 15:50
  • Thanks. If you can show me how to do it in the MWE I should be able to adapt to whatever journal style I have to use (though you are right, I may have problems with a journal that loads natbib; that might be another question then). – Consigliere ZARF Jul 12 '20 at 15:54
  • Thanks for the update. Almost there but as it is, the magenta link (in text) points nowhere (or to itself, hard to tell) rather than to the bibliography entry. I’ll wait with bated breath. – Consigliere ZARF Jul 12 '20 at 16:09
  • @ConsigliereZARF Have a look at the edited answer and tell me what you think, please. – moewe Jul 12 '20 at 16:34
  • Fantastic and exactly to spec, I accept. But no longer works with a journal document class that simply loads natbib, your warning was to the point. Do you mind if I ask a separate question for this? I suspect that natbib has the primitives for an even simpler solution (I’ve done similar things with it, but don’t have your expertise on the @ stuff...). – Consigliere ZARF Jul 12 '20 at 17:19
  • Or is there a way to unload a package that’s been loaded...? The class just contains: \usepackage[square,comma]{natbib}, as you can check this by itself is enough to mess up with your solution. – Consigliere ZARF Jul 12 '20 at 17:32
  • @ConsigliereZARF Sure, just ask a new question with the new document class and bibliography packages. The general strategy can probably also be applied in the new setting, but details may differ. It's not really possible to unload packages (I mean it is possible to simply undefine each and every command defined by the package and to reset each change it applies to existing macros, but that's quite some work), but there are methods to stop packages from being loaded (https://tex.stackexchange.com/q/39415/35864). ... – moewe Jul 12 '20 at 19:58
  • ... If you are using a journal class for a journal submission (that's the only good reason to use a heavily customised and inflexible journal class), then the publisher may not look favourably on changes to their layout and on additional complex code you load. Plus, it isn't even guaranteed that the links survive the publishing process (e.g. if the publisher doesn't actually use LaTeX for publishing and just extracts the text from your sibmission). – moewe Jul 12 '20 at 20:00
  • Yes! Fooling tex into believing it already loaded natbib seems to be doing just what I want (with no side effects that I can detect so far). Again, thank you so much. I find your solution to the initial problem very elegant. – Consigliere ZARF Jul 12 '20 at 20:14