1

I am trying to have the two \href in the code below behave with different options without having to invoke macros or global defaults.

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\href{www.overleaf.com}{link1}
\href{www.overleaf.com}{link2}

\end{document}

The idea is that one of them will continue with default and to pass the second one, I believe, the hidelinks = true option: keep clickable property but remove default blue box. I've seen many examples showing how to do this with macros but since I only want to have the non-default behavior very few times, I prefer to have the option scoped to the \href directly.

This is very similar in spirit to the answer given by @CephBirk in How to use different colors for different \href commands? but I can't figure out how to make it work to remove the box instead of changing the color.

1

The options you pass to \href are passed to a href option group, but the options that handle the link appearance belong to the Hyp option group. If you don't want to keep the interface to the href option group then this is enough:

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand*{\Myhref}[1][]{% definition copied form hyperref.sty
  \begingroup
  \setkeys{Hyp}{#1}% Changed href to Hyp
  \@ifnextchar\bgroup\Hy@href{\hyper@normalise\href@}%
}
\makeatother

\begin{document}

\Myhref{www.overleaf.com}{link1}

\Myhref[hidelinks]{www.overleaf.com}{link2}

\Myhref{www.overleaf.com}{link3}

\end{document}

which prints:

enter image description here

If you are still going to use the standard options to \href then you have to do a little more:

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand*{\Myhref}[1][]{%
  \begingroup
  \hypersetup{#1}%
  \inner@href % Call an "inner href"
}
\DeclareRobustCommand*{\inner@href}[1][]{%
  \setkeys{href}{#1}%
  \@ifnextchar\bgroup\Hy@href{\hyper@normalise\href@}%
}
\makeatother

\begin{document}

\Myhref{www.overleaf.com}{link1}

\Myhref[hidelinks]{www.overleaf.com}{link2}

\Myhref{www.overleaf.com}{link3}

\end{document}

Then the \Myhref command has two optional arguments:

\Myhref[<Hyp-options>][<href-options>]{<link>}{<text>}
  • What is the difference between the 2 options? – ranlot Sep 17 '18 at 19:08
  • Would a mix of \Myhref and \href work or should I replace all the \href's? – ranlot Sep 17 '18 at 19:13
  • Sorry, final question... Is there an option that hover would reveal the text in the Myhref in addition to hidelinks? – ranlot Sep 17 '18 at 19:17
  • @ranlot Sorry, I had to leave for a while. 1) If you used the \href command with options (\href[<something>]{}{}), then you need the second approach, otherwise the first will do. 2) You can still use \href normally. The \Myhref just creates a wrapper to change options. 3) That is a completely different question (that I really have no idea about the answer, sorry), so I suggest you ask a new question. Hope this helps :) – Phelype Oleinik Sep 17 '18 at 19:51

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.