4

I know hypersetup from hyperref needs to be added before \begin{document} but after \usepackage{hyperref} and \usepackage{bookmark}.

For example, you can inject it like the following to ensure this:

\AtBeginDocument{\hypersetup{}}

Problem

I'd like to have links (particularly linkcolor) that appear in section headings inherit the color of the section heading (I am using titlesec to redefine all of my headings). This means I would need to redefine / override the original colors defined within \hypersetup{}.

Code

Note that I only kept bookmark in there because of its fastidious relationship with hyperref.

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
  colorlinks=true,
  linkcolor=red!80!black,
  urlcolor=green!50!black,
  hyperfootnotes=false,
  hypertexnames,
  bookmarks=true}% Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters
\renewcommand{\sectionautorefname}{Section}

\usepackage[compact,explicit,noindentafter]{titlesec}
\titleformat{\section}[hang]{\bookmarksetupnext{bold,color=blue!50!black,open=false}\color{blue!50!black}\large\bfseries}{\thesection}{1em}{\hyphenchar\font=-1 #1}[\hyphenchar\font=\defaulthyphenchar] % \hyphenchar\font=-1 disables hyphenation, which I reset back to 1 after typesetting the section. see https://tex.stackexchange.com/a/44362/13552


\begin{document}
\section{Always remember that you are absolutely unique. Just like everyone else.}
\label{sec:quote}
Margaret Mead

\section{Should not be colored: \nameref{sec:quote}}
This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one: \autoref{sec:quote}. And this one: \pageref{sec:quote}.
\end{document}

Ouput

enter image description here

2 Answers2

4

You can call \hypersetup{hidelinks} in the "before" part of \titleformat, as this will only affect the section title locally:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
  colorlinks=true,
  linkcolor=red!80!black,
  urlcolor=green!50!black,
  hyperfootnotes=false,
  hypertexnames,
  bookmarks=true}% Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters
\renewcommand{\sectionautorefname}{Section}

\usepackage[compact,explicit,noindentafter]{titlesec}
\titleformat{\section}[hang]{\bookmarksetupnext{bold,color=blue!50!black,open=false}\hypersetup{hidelinks}\color{blue!50!black}\large\bfseries}{\thesection}{1em}{\hyphenchar\font=-1         #1}[\hyphenchar\font=1]

\begin{document}
\section{Always remember that you are absolutely unique. Just like everyone else.}
\label{sec:quote}
Margaret Mead

\section{Should not be colored: \nameref{sec:quote}}
This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one: \autoref{sec:quote}. And this one: \pageref{sec:quote}.
\end{document}

Output

Edit: Regarding Henri Menke's comment about \hyphenchar\font=-1 I will let that be up to the original poster, as he/she seems to have some reason to be using this in order to turn off hyphenation.

sodd
  • 5,771
  • \hyphenchar\font=-1 is not good practice. You turn off hyphenation but text will still be forced to a justified layout. Prepare for underfull/overfull boxes. \raggedright is much better! – Henri Menke Apr 19 '16 at 07:30
  • @hooy Thanks! Note that your answer's hypersetup does not inherit section's color. It may be good design to create a macro containing the color for this purpose, unless there is a better way. – Jonathan Komar Apr 19 '16 at 07:30
  • @macmadness86 I don't really understand what you want here. Is the output at least what you wanted? – sodd Apr 19 '16 at 07:34
  • @HenriMenke I forgot the specific reason why, but \raggedright does not always work. I think it had something to do with the scope of \raggedright not affecting tikz's scope within my section definition (not in my minimal example). \hyphenchar\font=-1 has a much broader scope, which is why I needed to redefine it to \hyphenchar\font=1 just before finishing the heading code. This way it is not harmful to the rest of the body. A couple underfull/overfull boxes never hurt anybody :-p They actually look good in my setup (I have some fancy TikZ action going on with much whitespace) – Jonathan Komar Apr 19 '16 at 07:34
  • @hooy No, you answered the question fine. You did not meet all parameters of the question (inheritance of color in surrounding scope), and I thought it would be useful to future readers to mention that and provide a solution (because I know how I would deal with that). – Jonathan Komar Apr 19 '16 at 07:36
  • 1
    @macmadness86 See edit, you can achieve the inheritance with \hypersetup{hidelinks} instead. – sodd Apr 19 '16 at 07:44
  • 2
    @macmadness86 But you are aware, that the default hyphenchar is not 1? Please use at least \hyphenchar\font=\defaulthyphenchar or \hyphenchar\font=`-. – Henri Menke Apr 19 '16 at 07:52
  • @hooy It should be noted that hidelinks affects all types of links not just linkcolor (url color, link color, etc.). This makes the color macro idea still viable. Good tip though! – Jonathan Komar Apr 19 '16 at 07:59
  • @macmadness86 Yes, it will. Although, in my opinion, one should not use (colored) urls and links in section titles anyway, as it steals focus away from the title itself. – sodd Apr 19 '16 at 08:02
  • @macmadness86 I don't understand your need for a color macro. Do you use a lot of different colors in section titles, or do you stick with one color? If you only use one color, you can use \colorlet{sectioncolor}{blue!50!black} and specify sectioncolor as the textcolor and linkcolor in section titles. No need to make simple things complicated. Am I missing something? – sodd Apr 19 '16 at 08:10
  • @hooy This discussion is diverging from the question/solution into design schemes, but I can tell you that harmonious colors are often very attractive and help the eye to distinguish various sectioning levels (hence why inheritance within titlesec heading definitions would be nice). Note that we are talking about shades here, but nevertheless, they are different. Examples I that provide use striking, non-harmonious colors to ensure that the differences between them are obvious. – Jonathan Komar Apr 19 '16 at 08:13
3

You can use named colors and change them in the document:

\documentclass{article}
\usepackage{xcolor}
\colorlet{mylinkcolor}{red!80!black}
\colorlet{myurlcolor}{green!50!black}
\colorlet{mysectioncolor}{blue!50!black}

\usepackage{hyperref} \usepackage{bookmark} \hypersetup{ colorlinks=true, linkcolor=mylinkcolor, urlcolor=myurlcolor, hyperfootnotes=false, hypertexnames, bookmarks=true}% Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters \renewcommand{\sectionautorefname}{Section}

\usepackage[compact,explicit,noindentafter]{titlesec} \titleformat{\section}[hang]{% \bookmarksetupnext{bold,color=mysectioncolor,open=false}% \color{mysectioncolor}% \colorlet{mylinkcolor}{mysectioncolor}% \large\bfseries}{\thesection}{1em}{\hyphenchar\font=-1 #1}[\hyphenchar\font=\defaulthyphenchar] % \

\begin{document} \section{Always remember that you are absolutely unique. Just like everyone else.} \label{sec:quote} Margaret Mead

\section{Should not be colored: \nameref{sec:quote}} This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one:

\autoref{sec:quote}. And this one: \pageref{sec:quote} but this one is yellow {\colorlet{mylinkcolor}{yellow}\pageref{sec:quote}} \end{document}

enter image description here

Edit

As a side remark: Your claim that \hypersetup can be used only in the preamble is wrong. A number of options can not be changed in the document, but when it makes sense \hypersetup works fine. But as \hypersetup is fragile, some care is need when using it in places like the section title. (So I would still prefer to change only the color values):

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
  colorlinks=true,
  linkcolor=red,
  }%

\begin{document} \section{Always remember that you are absolutely unique. Just like everyone else.} \label{sec:quote} This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one:

{\hypersetup{linkcolor=green} This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one:}

This link should be colored: \nameref{sec:quote}. And this one: Section~\ref{sec:quote}. And this one:

\section[for toc]{Should not be colored: \hypersetup{linkcolor=black}\nameref{sec:quote}}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • Thanks for the alternative way of defining colors! It is nice to remember \colorlet. Personally, I will stick with the macro way, because then I can associate colors with semantically relevant macro names like \seccolor, \subseccolor, \appname, \helpnotice etc.) and later have a quick way to change ALL of the members of a group (macro name). – Jonathan Komar Apr 20 '16 at 06:12
  • I don't understand your comment. Your example doesn't show commands like \seccolor, you used fixed names like red!80!black. Also I'm only showing you that you can change the link color in middocument (which would work with \hypersetup too by the way). How you hide this in semantic commands is up-to-you. – Ulrike Fischer Apr 20 '16 at 07:06
  • My comment is not within the context of my question, rather your answer and some comments to hooy's answer. Sorry for the confusion. – Jonathan Komar Apr 20 '16 at 08:08