2

I have a rather big and complex document with several bibunits and recurring citations. By default, all citations target the first appearance of a reference in a bibliography, leading to awkward jumps in the flow of the document and in irritating potential readers.

Unfortunately, the solution proposed in this question and its answer does not work for me because the bibunits are essentially section*, so there is not standard LaTeX counter available to disambiguate them. The bibunits are \phantomsections but I don't know how to make use of this fact. The bibunits are also in subfiles if this helps to set something up.

Also, the solution proposed in this older question and its answer is not enough for me, because natbib's \citep is used a lot and is not covered by the macros there.

What else can I do?

2
  • 1
    well you don't need a standard counter, any counter which makes the destinations unique would do it. hyperref uses in phantomsection \Hy@linkcounter. Commented Oct 27, 2021 at 17:01
  • Thanks for the hint, it helped me finding the most cromulent counter to use. Commented Oct 27, 2021 at 18:20

1 Answer 1

2

This is a complete rewrite of the answer. I found a solution to the problem that works at least for my project. It is definitely not a general solution (it does not handle compatibility with revtex, harvard, or chicago) and was achieved by deep surgery in the guts of hyperref.sty. For disambiguation of the bibunits the value of the counter \the\@bibunitauxcnt is inserted in the destination of the reference.

%hyperbu.sty
\ProvidesPackage{hyperbu}[2021/10/28 1.0 (JKn)]
%
% Makes Hyperlinks of citations and bibliographies local to bibunits
%
\RequirePackage{natbib}
\RequirePackage{bibunits}
\RequirePackage{hyperref}
\def\hyper@natlinkstart#1{%
  \Hy@backout{#1}%
  \hyper@linkstart{cite}{cite.\the\@bibunitauxcnt.#1}%
  \def\hyper@nat@current{#1}%
}
\def\hyper@natlinkend{%
  \hyper@linkend
}
\def\hyper@natlinkbreak#1#2{%
  \hyper@linkend#1\hyper@linkstart{cite}{cite.\the\@bibunitauxcnt.#2}%
}
\def\hyper@natanchorstart#1{%
  \Hy@raisedlink{\hyper@anchorstart{cite.\the\@bibunitauxcnt.#1}}%
}\def\hyper@natanchorstart#1{%
  \Hy@raisedlink{\hyper@anchorstart{cite.\the\@bibunitauxcnt.#1}}%
}
\def\hyper@natanchorend{\hyper@anchorend}
\ltx@IfUndefined{NAT@parse}{%
  \providecommand*\@extra@binfo{}%
  \providecommand*\@extra@b@citeb{}%
  \def\bibcite#1#2{%
    \@newl@bel{b}{#1\@extra@binfo}{%
      \hyper@@link[cite]{}{cite.\the\@bibunitauxcnt.#1    \@extra@b@citeb}{#2}%
    }%
  }%
  \gdef\@extra@binfo{}%
  \let\Hy@bibcite\bibcite
  \begingroup
    \@ifundefined{bbl@cite@choice}{}{%
      \g@addto@macro\bbl@cite@choice{%
        \let\bibcite\Hy@bibcite
      }%
    }%
  \endgroup
  \providecommand*{\@BIBLABEL}{\@biblabel}%
  \def\@lbibitem[#1]#2{%
    \@skiphyperreftrue
    \H@item[%
      \ifx\Hy@raisedlink\@empty
        \hyper@anchorstart{cite.\the\@bibunitauxcnt.#2\@extra@b@citeb}%
          \@BIBLABEL{#1}%
        \hyper@anchorend
      \else
        \Hy@raisedlink{%
          \hyper@anchorstart{cite.\the\@bibunitauxcnt.#2\@extra@b@citeb}\hyper@anchorend
        }%
        \@BIBLABEL{#1}%
      \fi
      \hfill
    ]%
\@skiphyperreffalse
\if@filesw
  \begingroup
    \let\protect\noexpand
    \immediate\write\@auxout{%
      \string\bibcite{#2}{#1}%
    }%
  \endgroup
\fi
\ignorespaces
}%
\def\@bibitem#1{%
\@skiphyperreftrue\H@item\@skiphyperreffalse
\Hy@raisedlink{%
  \hyper@anchorstart{cite.\the\@bibunitauxcnt.#1\@extra@b@citeb}\relax\hyper@anchorend
    }%
    \if@filesw
      \begingroup
        \let\protect\noexpand
        \immediate\write\@auxout{%
          \string\bibcite{#1}{\the\value{\@listctr}}%
        }%
      \endgroup
    \fi
    \ignorespaces
  }%
}{}
\endinput
3
  • This works perfectly for multiple bib units; however, when I cite the same reference both inside and outside of a bibunit, the citation in the bibunit correctly links to that bibunit's bibliography but the citation outside the bibunit links to the previous bibunit that has the reference instead of the global bibliography. Is it feasible to modify your answer to have citations outside of any bibunit always point to the global bibliography? If you prefer, I could post a new question with an MWE. Thank you.
    – possum
    Commented Aug 11 at 16:06
  • 1
    I suggest asking a new question for that scenario, providing a link to this question for reference. Commented Aug 11 at 16:22
  • Thank you for your suggestion. I created a new question for this scenario.
    – possum
    Commented Aug 12 at 12:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .