3

My first question on this StackExchange and I hope it seems reasonable and hasn't been asked before here (I tried looking).

I'm a beginner trying to write my thesis in LaTeX. I like the idea of having bibliographic references as coloured text but would like to have borders for internal references. But when I try the following code with hyperref

\hypersetup{pdfpagemode={UseOutlines},
bookmarksopen=true,
bookmarksopenlevel=0,
hypertexnames=false,
colorlinks=true,
citecolor=blue,
linkcolor=green,
linkbordercolor={1 0 0},
urlcolor=red,
pdfstartview={FitV},
unicode,
breaklinks=true,
}

I believe colorlinks=true is blocking the border colours from coming in. Can I do this?

1 Answer 1

2

By default, hyperref has

  • three global and exclusive types of link styles: pdfborder (by default), colorlinks, and pdfborderstyle, and
  • six link types: cite (bibliographic references), file, link (internal references), menu, run, and url.

Here is a crude implementation that allows user to set one of the first two link styles per link type.

Update:

  • Patch lower macro \Hy@VerboseLinkStart instead of \hyper@link@.

    (In places like biblatex and \contentsline, \hyper@linkstart and \hyper@link are used directly. These two macros has dependencies in hxetex.def, the hyperref driver for xetex engine, but not in hpdftex.def and hluatex.def. Therefore \Hy@VerboseLinkStart, the common internal of \hyper@link[|start] in each of drivers, is patched.)

In the following example, I use

\hypersetup{
  citetype=colorlinks, 
  linktype=pdfborder
}

to set link style color for all cites, and border for all links.

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{xpatch}

\makeatletter
% change link style (color or border) per link type
\xpretocmd\Hy@VerboseLinkStart
  {\csname hyper@set@link@type@#1\endcsname}
  {}{\fail}

% set link type to color
\def\hyper@use@colorlinks{%
  \let\@pdfborder\@pdfborder@plain
  \let\Hy@colorlink\Hy@colorlink@normal
}

% set link type to border
\def\hyper@use@pdfborder{%
  \let\@pdfborder\@pdfborder@normal
  \ifHy@frenchlinks
    \let\Hy@colorlink\Hy@colorlink@french
  \else
    \let\Hy@colorlink\Hy@colorlink@plain
  \fi
}

\let\@pdfborder@normal\@pdfborder
\def\@pdfborder@plain{0 0 0}

\def\Hy@colorlink@plain#1{\begingroup}
\def\Hy@colorlink@french#1{\begingroup\fontshape{\scdefault}\selectfont}
\def\Hy@colorlink@normal#1{\begingroup\HyColor@UseColor#1}


% new \hypersetup options per link type
% usage: "citetype=[colorlinks|pdfborder]"
\def\Hy@ColorList{cite,file,link,menu,run,url}
\@for\Hy@temp:=\Hy@ColorList\do{%
  \edef\Hy@temp{%
    \noexpand\define@key{Hyp}{\Hy@temp type}{%
      \noexpand\expandafter\noexpand\let
      \noexpand\expandafter\expandafter\noexpand\csname hyper@set@link@type@\Hy@temp\endcsname
      \noexpand\csname hyper@use@##1\noexpand\endcsname%
    }%
  }%
  \Hy@temp
}
\makeatother

% user setting
\hypersetup{
  % colorlinks, % option "colorlinks" now has no effects
  citetype=colorlinks, 
  linktype=pdfborder
}

\begin{document}
Section~\ref{key}, article \cite{citekey}
\section{title}\label{key}

\begin{thebibliography}{00}
  \bibitem{citekey}
\end{thebibliography}
\end{document}

enter image description here

5
  • I tried to implement this but it seems to just remove all colours (text and border) from cites and link references. My Overleaf project can be viewed at overleaf.com/read/jwthpgnjwtbg (I put the xpatch part of your code in main.tex under "References with xpatch" and the user setting bit is in MastersDoctoralThesis.cls). The compiler also has a problem with your {}{\fail} command? Your solution looks kind of advanced but it's definitely useful both to me and I suppose anyone else who might want to do this Aug 13, 2020 at 14:30
  • @MobeusZoom In your linked overleaf project, the template loads hyperref package by \AtEndPreamble, which is very later than the patch to \hyper@link@, hence the patch code raises an error. You can manually load hyperref before patching to avoid that error. Also, to support biblatex, I updated answer to patch \hyper@linkstart instead of \hyper@link@. Aug 14, 2020 at 6:05
  • Another change: to make the patch work for all popular engines, \Hy@VerboseLinkStart is patched. Aug 14, 2020 at 10:23
  • Thank you! I've updated the Overleaf project accordingly. Now it compiles without error. However, while citation color is correctly controlled by citecolor=blue (see MastersDoctoralThesis.cls), my attempts to set linkbordercolor=red seem to be ignored (see Figure 1.1, Table 1.1 or Equation 1.1 in the compiled PDF) Aug 14, 2020 at 14:38
  • 1
    @MobeusZoom The link borders do exists, but the pdf viewer of overleaf refuses to show them. You can download the pdf from overleaf and check those borders. Aug 15, 2020 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.