我知道需要添加hypersetup
from之前但在and之后。hyperref
\begin{document}
\usepackage{hyperref}
\usepackage{bookmark}
例如,您可以像下面这样注入以确保这一点:
\AtBeginDocument{\hypersetup{}}
问题
我希望出现linkcolor
在节标题中的链接(特别是)继承节标题的颜色(我用它来titlesec
重新定义我的所有标题)。这意味着我需要重新定义/覆盖在中定义的原始颜色\hypersetup{}
。
代码
请注意,我之所以将它保留bookmark
在那里只是因为它与 的关系很密切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}
输出
答案1
您可以\hypersetup{hidelinks}
在的“之前”部分调用\titleformat
,因为这只会在本地影响部分标题:
\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}
编辑:关于 Henri Menke 的评论,\hyphenchar\font=-1
我将让原始海报决定,因为他/她似乎有某种理由使用它来关闭连字符。
答案2
您可以使用命名的颜色并在文档中更改它们:
\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}
编辑
附言:您声称\hypersetup
只能在序言中使用是错误的。文档中的许多选项无法更改,但当它有意义时可以\hypersetup
正常工作。但由于它\hypersetup
很脆弱,在章节标题等地方使用它时需要小心。(所以我仍然希望只更改颜色值):
\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}