# hyperref inside refsegment always linking to first bibliography, not current bibliography

It seems as if the citation links created by hyperref always direct to the first bibliography in which the respective entry is listed instead of the current one. Is there any way to fix this behaviour?

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{MWE.bib}
@article{articleA,
author = {Clark Kent},
title  = {On why superman is stronger than batman},
journal = {The daily planet},
year   = {2016},
}
\end{filecontents}
\usepackage[backend=biber,style=alphabetic]{biblatex}

\begin{document}

\begin{refsegment}
\section{Section Title A}
\cite{articleA}
\printbibliography[segment=\therefsegment]
\end{refsegment}

\newpage
\begin{refsegment}
\section{Section Title B}
This links to the bibliography of section A instead of B: \cite{articleA}
\printbibliography[segment=\therefsegment]
\end{refsegment}

\newpage
\begin{refsegment}
\section{Section Title C}
This links to the bibliography of section A instead of C: \cite{articleA}
\printbibliography[segment=\therefsegment]
\end{refsegment}
\end{document}


If you insist you can make links local to both refesction and refsegment.

\makeatletter
\appto\blx@mkhyperref{%
\let\blx@anchors\@empty
\ifundef\hyper@natanchorstart
{\protected\def\blx@anchor{%
\xifinlist{\the\c@refsection @\the\c@refsegment @\abx@field@entrykey}{\blx@anchors}
{}
\hypertarget{cite.\the\c@refsection @\the\c@refsegment @\abx@field@entrykey}{}}}}
{\protected\def\blx@anchor{%
\xifinlist{\the\c@refsection @\the\c@refsegment @\abx@field@entrykey}{\blx@anchors}
{}
\hyper@natanchorstart{\the\c@refsection @\the\c@refsegment @\abx@field@entrykey}%
\hyper@natanchorend}}}}

\appto\blx@mkhyperref{%
\protected\def\blx@imc@bibhyperref{%
\@ifnextchar[%]
{\blx@bibhyperref}
{\blx@bibhyperref[\abx@field@entrykey]}}%
\ifundef\hyper@natanchorstart
{\long\def\blx@bibhyperref[#1]#2{%
#2%
\blx@sfsave}\blx@sfrest}%
#2%
\blx@sfsave}\blx@sfrest}%
\protected\long\def\blx@imc@bibhypertarget#1#2{%
\blx@sfsave\hypertarget{cite.\the\c@refsection @\the\c@refsegment:#1}{\blx@sfrest
#2%
\blx@sfsave}\blx@sfrest}}%
{\long\def\blx@bibhyperref[#1]#2{%
#2%
#2%
\protected\long\def\blx@imc@bibhypertarget#1#2{%
\blx@sfsave\hyper@natanchorstart{\the\c@refsection @\the\c@refsegment:#1}\blx@sfrest
#2%
\blx@sfsave\hyper@natanchorend\blx@sfrest}}
\let\blx@imc@ifhyperref\@firstoftwo
\def\blx@sf{\spacefactor}%
\def\blx@sfsave{%
\blx@leavevmode
\numgdef\blx@sf{\spacefactor}}%
\def\blx@sfrest{%
\ifhmode\spacefactor\blx@sf\relax\fi
\gdef\blx@sf{\spacefactor}}}
\makeatother


Is just a copy of the relevant macros from biblatex_.sty with \the\c@refsection @ extended to \the\c@refsection @\the\c@refsegment @ to make the links also local to refsegments.

• This probably solves the problem as posed in the question, but I'm having serious doubts that it can help with the underlying problem. – moewe Apr 10 '16 at 13:59
• Actually, thanks alot, it already helped me greatly because now the labels are globally unique (though this was not part of the question) and within a section the links are all sound! However, there are still 24 incidences where I need a cross-refsegment link, which doesn't work because of the added @\the\c@refsegment @ to the macro. Since 24 is still manageable, is it possible to insert manual links in latex/hyperref? – jpb Apr 10 '16 at 19:30
• @jpb One could probably modify the code above to check for a certain keyword, say, and change the links accordingly. I will have to think about that. – moewe Apr 11 '16 at 6:07
• thanks, but I think I currently have a workaround: I am creating, at the beginning of the bib-entry a label with a custom text like [DS09] and instead of using the cite, I use the ref command. Neither very pretty nor very elegant, but it works for my purposes. – jpb Apr 11 '16 at 7:37

First, some clarification regarding the use of refsegmentvs. refsection. Refsections are used if you want different bibliographies at different places of a larger work; e.g. at the end of each chapter. The labels generated are always local to the refsection. In contrast, refsegments are good for having one bibliography at the end which you can then divide into parts (e.g. for each chapter) etc. To quote from the biblatex manual (p. 80):

On refsections:

The refsection environment is used in the document body to mark a reference section. is environment is useful if you want separate, independent bibliographies and bibliography lists in each chapter, section, or any other part of a document. Within a reference section, all cited works are assigned labels which are local to the environment.

On refsegments:

The refsegment environment is used in the document body to mark a reference segment. is environment is useful if you want one global bibliography which is subdivided by chapter, section, or any other part of the document.

So, as you noted in your example, with refsegment, a citation always links back to the first bibliography. But if you use reflection, you get local labels and thus the citations link to the local bibliographies.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{MWE.bib}
@article{articleA,
author = {Clark Kent},
title  = {On why superman is stronger than batman},
journal = {The daily planet},
year   = {2016},
}
@article{articleB,
author = {Bruce Wyne},
title  = {On why batman is stronger than superman},
journal = {The daily planet},
year   = {2016},
}
\end{filecontents}
\usepackage[backend=biber,style=alphabetic]{biblatex}

\begin{document}

\begin{refsection}
\section{Section Title A}
\cite{articleA}
\printbibliography[section=\therefsection]
\end{refsection}

\newpage
\begin{refsection}
\section{Section Title B}
This link to the bibliography : \cite{articleA}\\
This link to the bibliography here: \cite{articleB}
\printbibliography[section=\therefsection]
\end{refsection}

\newpage
\begin{refsection}
\section{Section Title C}
This link to the bibliography: \cite{articleA}\\
This link to the bibliography: \cite{articleB}
\printbibliography[section=\therefsection]
\end{refsection}

\end{document}

• Yes, sadly, there are some formal criterias that I cannot realize through refsections. Basically, each section is supposed to have its own bibliography (not necessarily at the end). However at the end of the document there is a list of academic profiles including a list of own publications and any reference to an "own" article should directly refer to the bibliography in the respective academic profile. It is related to this question: tex.stackexchange.com/questions/303054/… – jpb Apr 10 '16 at 13:25
• Should the “own” article only appear in the author profiles? Or is the problem “only” where the citations links to? – Daniel Apr 10 '16 at 13:39
• Yes, the "own" article should only appear in the author's profile (e.g. not in the bibliographies at the end of the section) and any citation of that article anywhere should link to the respective bibliography entry in its profile. – jpb Apr 10 '16 at 13:42
• @jpb Your situation is very tricky. You should probably want real refsections, because I assume the different sections/chapters are pretty much independent and have their own bibliography. But then the publication list can't be properly done because there is no way to get a meta-list of all the works by insert author here because everything is kept local. – moewe Apr 10 '16 at 13:52
• @jpb I suppose the way to check if you want refsections or refsegments is to consider the following situation in an author year style. Suppose the first chapter refers to a work by Elk from 2012 titled "Brontosaurus" and the second refers to a work by Elk also from 2012 titled "Triceratops". Would you want both labels to read "Elk 2012", or would you want disambiguation across the sections: "Elk 2012a" and "Elk 2012b"? For disambiguation across sections, you need to use refsegments; if you prefer to keep each section local, use refsection. – moewe Apr 11 '16 at 6:11