3

I like to hide some type of links to not distract the reader. I want to hide, say, cite links but show \href type links (I consider cite links do not need to be highlighted visually because the reader may know, if knowing the convention used in the document, that all citations are links; whereas the reader will know that a href-type link such as this one is a link only if it is highlighted).

The links are defined thanks to hyperref. I do not want to use the option hidelinks because (AFAIU) it hides all links. And I prefer to use boxes than colors for links that are shown (because they have no effect on printed versions). Therefore, I want to disable boxes for cite links and keep boxes for other links.

I tried doing this using \hypersetup{citebordercolor={1 1 1}}, which seems to be the usually recommended strategy. Now, I am probably nitpicking here, but I would consider it more elegant to have no box than a white box, if possible. Because this strategy produces (AFAIU) a box of color white in the resulting PDF, instead of producing no box. I mean that I suspect that the box is indeed there, occupying (a very tiny bit of) space in the document and memory when rendered, while being invisible because the normal background color of a paper is white. (And possibly triggering bugs if the reader software does not perfectly produces a box that is indeed entirely white, as in my case.)

Here is a minimal example.

\documentclass{article}
\usepackage{hyperref}
\hypersetup{citebordercolor={1 1 1}}

\begin{document}
Hello, I cite \cite{key}.

\begin{thebibliography}{1}
\bibitem{key}
A.~Author.
\end{thebibliography}
\end{document}

Now, apparently this really produces a box as I expected: a probably visual bug in my reader shows a part of the box in black.

Thus, my question: is there a way to produce no box at all, rather than “white” the box?

  • As to the space taken up by the link in TeX’s memory and in the PDF file, you need not worry: at least for the hpdftex driver (the driver usually employed when the typesetting engine is pdftex), no extra (TeX) box is generated for the link. Properties such as color and border are specified by changing some attributes of the \pdfstartlink node, and might even consist (as in @AlexG’s answer) in the simple substitution, at the level of the generated PDF file, of a character 1 with the character 0, thus resulting in a fie of exacty the same size. (:-) – GuM Sep 7 '18 at 13:56
  • @GuM if you make this an answer I’ll probably end up accepting it, unless somebody else comes up with a very clever solution. It might well be that the answer to my question is that it doesn’t matter very much and that there’s no way to do it anyway (barring using ugly hacks, which I am reluctant to do in this no-stakes case). – Olivier Cailloux Sep 8 '18 at 13:22
  • I ended up wih accepting your encouragement, and posted an answer. – GuM Sep 20 '18 at 19:47
4

I would redefine \cite, after loading hyperref, to locally remove the PDF link border:

\documentclass{article}

\usepackage{hyperref}

\let\citeOrig\cite
\renewcommand\cite[2][]{{%
  \hypersetup{pdfborder=0 0 0}%
  \ifx\empty#1\empty%
    \citeOrig{#2}%
  \else%
    \citeOrig[#1]{#2}%
  \fi%  
}}

\begin{document}
Hello, I cite \cite{key}.

\begin{thebibliography}{1}
\bibitem{key}
A.~Author.
\end{thebibliography}
\end{document}
  • This might be too much of a hack, as I am actually looking for a solution that extends to other kinds of links to be hidden (such as linkbordercolor), and as I actually usually use natbib for citing. But, indeed, this answers my question as stated. And this kind of hack might be the only possible solution, I’m afraid… – Olivier Cailloux Sep 7 '18 at 13:35
  • The problem is that there is only one hyperref pdfborder option for all kinds of links. – AlexG Sep 7 '18 at 13:52
  • You could also use xparse and \IfValueTF (this is what I had done in my answer, that I was still testing in the exact moment in which you posted yours! ;-) – GuM Sep 7 '18 at 13:57
  • @GuM Yes, you are right, this would avoid the use of \ifx, which is not the LaTeX way of doing things. Feel free to add your answer as well. – AlexG Sep 7 '18 at 14:01
1

In the end, I made up my mind to accept Olivier Cailloux’s call to convert my comment into an answer.

What follows refers specifically to the hpdftex driver, the driver usually employed when the typesetting engine is pdfTeX. This driver turns a piece of text into an hyperlink by enclosing it between \pdfstartlink and \pdfendlink nodes, which are generated by primitive commands with the same (respective) names; these nodes are a subtype of “whatsit” nodes (similar to the \special nodes), which means that they are “implicitly associated with a particular position on the page, namely the reference point that would have been present if a box of height, depth, and width zero had appeared in place of the whatsit” (The TeXbook, p. 228). No extra box is generated to hold the link: indeed, even the dimensions of the area occupied by the link are recorded in the \pdfstartlink node itself, and can be specified in the source code by means of the same notation used to describe rules, e.g.

\pdfstartlink height 10pt depth 4pt width 3cm ...

If a component (height, depth, or width) is omitted, it defaults to the value “running”, that means “reaching to the boundary of the enclosing box (for height and depth) or to the corresponding \pdfendlink node (for width)”. If a \pdfstartlink node with “running” width is part of a horizontal list that is subsequently split into lines, and if the corresponding \pdfendlink node winds up in a different line, the link will automatically be split across lines, and even across pages, as necessary. Attributes such as the color used to display the link, or the thickness and the color of the border drawn around it, are specified directly as PDF commands that are similarly recorded in the \pdfstartlink node itself.

The following example leaves in the transcript (.log) file a diagnostic tracing that illustrates what we have just said.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
\usepackage[utf8]{inputenc}      % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

% \usepackage{hyperref}
% \hypersetup{colorlinks=false,citebordercolor={0 .75 .25}}

\makeatletter

\newcommand*\ShowLists{%
    \begingroup
        \showboxbreadth \@M
        \showboxdepth \sixt@@n
        \tracingonline \@ne
        \showlists
    \endgroup
}

\makeatother



\begin{document}

Hello, I~cite~\cite{key}.
\ShowLists

\begin{thebibliography}{1}
    \bibitem{key}
        A.~U.~Thor, \emph{A~short story}.
\end{thebibliography}

\end{document}

First compile it as it stands, that is, with the two lines

% \usepackage{hyperref}
% \hypersetup{colorlinks=false,citebordercolor={0 .75 .25}}

commented out, so as to generate a “benchmark” tracing that shows what happens when the hyperref package is not loaded. Looking in the .log file, you should find the following excerpt:

### horizontal mode entered at line 29
\hbox(0.0+0.0)x15.0
\T1/cmr/m/n/10 H
\T1/cmr/m/n/10 e
\T1/cmr/m/n/10 l
\T1/cmr/m/n/10 l
\T1/cmr/m/n/10 o
\T1/cmr/m/n/10 ,
\glue 3.33252 plus 2.08282 minus 0.88867
\T1/cmr/m/n/10 I
\penalty 10000
\glue 3.33252 plus 1.66626 minus 1.11084
\T1/cmr/m/n/10 c
\T1/cmr/m/n/10 i
\T1/cmr/m/n/10 t
\T1/cmr/m/n/10 e
\penalty 10000
\glue 3.33252 plus 1.66626 minus 1.11084
\T1/cmr/m/n/10 [
\hbox(6.4151+0.0)x4.99878
.\T1/cmr/m/n/10 1
\T1/cmr/m/n/10 ]
\T1/cmr/m/n/10 .
\glue 4.44336 plus 4.99878 minus 0.37027
spacefactor 3000
### vertical mode entered at line 0
### current page:
\write-{}

prevdepth 0.0

./citelink.tex:30: OK.
\ShowLists ...t@@n \tracingonline \@ne \showlists 
                                                  \endgroup 
l.30 \ShowLists

? 

As you can see, the reference “1” gets already wrapped in a \hbox, also when no hyperlink is being generated. Now uncomment the lines that load and configure the hyperref package

\usepackage{hyperref}
\hypersetup{colorlinks=false,citebordercolor={0 .75 .25}}

and compile again; the transcript should now look like this:

### horizontal mode entered at line 29
\hbox(0.0+0.0)x15.0
\T1/cmr/m/n/10 H
\T1/cmr/m/n/10 e
\T1/cmr/m/n/10 l
\T1/cmr/m/n/10 l
\T1/cmr/m/n/10 o
\T1/cmr/m/n/10 ,
\glue 3.33252 plus 2.08282 minus 0.88867
\T1/cmr/m/n/10 I
\penalty 10000
\glue 3.33252 plus 1.66626 minus 1.11084
\T1/cmr/m/n/10 c
\T1/cmr/m/n/10 i
\T1/cmr/m/n/10 t
\T1/cmr/m/n/10 e
\penalty 10000
\glue 3.33252 plus 1.66626 minus 1.11084
\T1/cmr/m/n/10 [
\hbox(6.4151+0.0)x4.99878
.\pdfstartlink(*+*)x* attr{/Border[0 0 1]/H/I/C[0 .75 .25]} action goto name{ci
te.key}
.\T1/cmr/m/n/10 1
.\pdfendlink
\T1/cmr/m/n/10 ]
\T1/cmr/m/n/10 .
\glue 4.44336 plus 4.99878 minus 0.37027
spacefactor 3000
### vertical mode entered at line 0
### current page:
\write-{}
\pdfdest name{Doc-Start} xyz

prevdepth 0.0

./citelink.tex:30: OK.
\ShowLists ...t@@n \tracingonline \@ne \showlists 
                                                  \endgroup 
l.30 \ShowLists

? 

You can see that the lines

\T1/cmr/m/n/10 [
\hbox(6.4151+0.0)x4.99878
.\T1/cmr/m/n/10 1
\T1/cmr/m/n/10 ]

have become

\T1/cmr/m/n/10 [
\hbox(6.4151+0.0)x4.99878
.\pdfstartlink(*+*)x* attr{/Border[0 0 1]/H/I/C[0 .75 .25]} action goto name{cite.key}
.\T1/cmr/m/n/10 1
.\pdfendlink
\T1/cmr/m/n/10 ]

No extra box has been added: simply, the “1” has been enclosed between \pdfstartlink and \pdfendlink nodes. These nodes were generated by the hyperref package by executing code equivalent to the primitive commands

\pdfstartlink
    attr {/Border[0 0 1]/H/I/C[0 .75 .25]}
    goto name {cite.key}

for the start of the link, and

\pdfendlink

for the end of the link. The “rule specification” is omitted, so the link has “running” height, depth, and width (which show up as (*+*)x* in the transcript). The thickness and color of the border around the link are specified by the string (“general text”) that follows, enclosed in braces, the attr keyword: had that string contained /Border[0 0 0] instead of /Border[0 0 1], no border would have been drawn. So we see that not only the presence of the border does not take up any additional cell of memory inside the pdfTeX program, but also in the generated PDF file it merely requires that a 1 be written in place of a 0, resulting in a file of the same length!

Unfortunately, as @AlexG has already remarked, the hyperref package makes no provision for specifying different thicknesses for the borders of different kinds of link: you can only customize the thickness of the border drawn around all links. For this reason, short of making substantial patches to the macros of the hyperref package and/or the hpdftex driver, I see no solution essentially different from @AlexG’s.

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.