TransWikia.com

Both text and border colors for ref

TeX - LaTeX Asked on February 17, 2021

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?

One Answer

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
xpretocmdHy@VerboseLinkStart
  {csname hyper@set@link@type@#1endcsname}
  {}{fail}

% set link type to color
defhyper@use@colorlinks{%
  let@pdfborder@pdfborder@plain
  letHy@colorlinkHy@colorlink@normal
}

% set link type to border
defhyper@use@pdfborder{%
  let@pdfborder@pdfborder@normal
  ifHy@frenchlinks
    letHy@colorlinkHy@colorlink@french
  else
    letHy@colorlinkHy@colorlink@plain
  fi
}

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

defHy@colorlink@plain#1{begingroup}
defHy@colorlink@french#1{begingroupfontshape{scdefault}selectfont}
defHy@colorlink@normal#1{begingroupHyColor@UseColor#1}


% new hypersetup options per link type
% usage: "citetype=[colorlinks|pdfborder]"
defHy@ColorList{cite,file,link,menu,run,url}
@forHy@temp:=Hy@ColorListdo{%
  edefHy@temp{%
    noexpanddefine@key{Hyp}{Hy@temp type}{%
      noexpandexpandafternoexpandlet
      noexpandexpandafterexpandafternoexpandcsname hyper@set@link@type@Hy@tempendcsname
      noexpandcsname hyper@use@##1noexpandendcsname%
    }%
  }%
  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

Correct answer by muzimuzhi Z on February 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP