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

Mophotla
  • 183

2 Answers2

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}

Ulrike Fischer
  • 327,261
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

marquinho
  • 1,721