5

I'm using the hyperref package with the hidelinks option, but there is one link I would like to be coloured and I'm trying to do this with an overriding hypersetup command in an environment which only contains that particular link. Why doesn't this work?

\documentclass{minimal}
\usepackage[hidelinks]{hyperref}

\begin{document}

\href{stackexchange.com}{hide this link}\\
{\hypersetup{colorlinks=true}{\href{stackexchange.com}{do not hide this link}}}\\
\href{stackexchange.com}{hide this link}

\end{document}

Result:

enter image description here

The method works just as intended the other way round:

\documentclass{minimal}
\usepackage{hyperref}

\begin{document}
    
\href{stackexchange.com}{do not hide this link}\\
{\hypersetup{hidelinks}{\href{stackexchange.com}{hide this link}}}\\
\href{stackexchange.com}{do not hide this link}
    
\end{document}

enter image description here

2
  • please consider accepting one of the answers (most likely Ulrike's straightforward answer tex.stackexchange.com/a/636731) if it solved your problem.
    – marquinho
    Mar 16, 2022 at 15:31
  • 1
    @marquinho Done.
    – Mophotla
    Mar 17, 2022 at 16:09

2 Answers 2

5

colorlinks is currently a preamble only option (this changes if you use the new pdfmanagement). If you want only one link in color I suggest to use simply \color:

\documentclass{article}
\usepackage[hidelinks]{hyperref}
\usepackage{xcolor}
\begin{document}

\href{stackexchange.com}{hide this link}\\
{\color{red}{\href{stackexchange.com}{do not hide this link}}}\\
\href{stackexchange.com}{hide this link}

\end{document}
2
  • Thank you, that makes sense of course. What if I want no different font colour, but just a coloured box around the link, as in my above example?
    – Mophotla
    Mar 11, 2022 at 16:28
  • 1
    \hypersetup{pdfborder={1 1 1}} Mar 11, 2022 at 16:32
3

I believe that \hypersetup is actually intended only for the preamble, and I'm somewhat surprised that your first example (where you invoked \hypersetup in the document body) worked at all.

[EDIT: As Ulrike Fischer points out, \hypersetup does work in the document body, but not for all options; f.i., colorlinks works only in the preamble.]

Under my assumption that \hypersetup{colorlinks} is not expected to work in the document body, I propose the following workaround:

(1) Save the current text color using pkg xcolor under a custom name (saved), in order to retrieve it later. Normally, the text color will be black, but we are going for a flexible enough solution;

(2) In the preamble, set the color of all links to said default text color (allcolors=saved), effectively mimicking the option hidelinks of hyperref;

(3) In the document, set locally (within a group) the color for all links to the desired color (similarly to your own approach).

\documentclass{article}

\usepackage{xcolor}

\colorlet{saved}{.}  % saves the current text color as "saved"

\usepackage[colorlinks]{hyperref}
\hypersetup{allcolors=saved}

\begin{document}

\href{stackexchange.com}{hide this link}

{\hypersetup{allcolors=magenta}{\href{stackexchange.com}{do not hide this link}}}

\href{stackexchange.com}{hide this link}

\end{document}

colored link among black text

And this is with a default text color other than black (setting \color{blue} in the preamble):

colored link among blue text

2
  • 1
    \hypersetup can be used in the document, but not all options work, colorlinks is one that currently works only when set in the preamble. Mar 11, 2022 at 16:17
  • @UlrikeFischer Thanks, I have edited in the information!
    – marquinho
    Mar 11, 2022 at 16:21

You must log in to answer this question.

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