1

I want to colorize (highlight) certain headings both in the document as well as in the table of contents. The following works fine for me, but the respective entries in the hyperref TOC (links shown in the side bar of the PDF viewer) start with the word blue now.

\documentclass{article}

\usepackage[
 colorlinks,
 pdfpagelabels,
 bookmarksnumbered, linkcolor = black,
 plainpages = false, hypertexnames = false, citecolor = black,
 urlcolor = blue,
 breaklinks]{hyperref}
\usepackage{color}

\newcommand{\SBlue}[1]{%
\begingroup\color{blue}{\textbf{#1}}\endgroup%
}

\begin{document}

\tableofcontents
\vspace{10em}

\section{Heading 1}
foo
\section{\SBlue{Heading 2}}
bar
\section{Heading 3}
foobar

\end{document}

Here's what appears in the sidebar:

Heading 1
blueHeading 2
Heading 3

Is there a way of modifying the hyperref link texts? Or a completely different approach?

--Vic

1

2 Answers 2

1

Contents for the PDF bookmark panel should not consist of TeX macros, so \color{blue} etc. is nothing that PDF understands.

Use \texorpdfstring{\color{blue}{\textbf{#1}}{#1} where the second usage of #1 will enter the PDF bookmarks.

The colour can be controlled with \bookmarksetup{color=blue,bold}, if bold is not requested, just omit it.

\documentclass{article}

\usepackage{xcolor}

\usepackage[
 colorlinks,
 pdfpagelabels,
 bookmarksnumbered, linkcolor = black,
 plainpages = false, hypertexnames = false, citecolor = black,
 urlcolor = blue,
 breaklinks]{hyperref}

\usepackage{bookmark}

\bookmarksetup{color=blue,bold}

\newcommand{\SBlue}[1]{%
%\begingroup
\texorpdfstring{\color{blue}\textbf{#1}}{#1}%
%\endgroup
}

\begin{document}

\tableofcontents
\vspace{10em}

\section{Heading 1}
foo
\section{\SBlue{Heading 2}}
bar
\section{Heading 3}
foobar

\end{document}
2
  • Es sind immer diese kleinen Dinge, die den fortgeschrittenen Anwender vom Guru unterscheiden ;-) Der fortgeschrittene Anwender weiß gar nicht, wo er den Hebel ansetzen soll ...
    – Victor
    Commented Dec 18, 2017 at 16:42
  • @Victor: Ein weiser Spruch ;-)
    – user31729
    Commented Dec 18, 2017 at 20:25
0

After fiddeling with Christian's solution around a bit more, here's my final code:

 \documentclass{article}

 \usepackage[
  colorlinks,
  pdfpagelabels,
  bookmarksnumbered, linkcolor = black,
  plainpages = false, hypertexnames = false, citecolor = black,
  urlcolor = blue,
  breaklinks]{hyperref}
 \usepackage{color}
 \usepackage{bookmark}

 \newcommand{\sectionB}[1]{
 \bookmarksetup{color=[rgb]{0,0,1}, bold}
 \section{\texorpdfstring{\color{blue}\textbf{#1}}{#1}}
 \bookmarksetup{color={}, bold=false}
 }

 \begin{document}

 \tableofcontents
 \vspace{10em}

 \section{Header 1}
 foo
 \sectionB{Header 2}
 bar
 \section{Header 3}
 foobar

 \end{document}

(I only changed from xcolor to color because I have it loaded anyway and don't want to test the impact on my existing documents.)

4
  • Aha... Die Lösung entakzeptieren und dann die eigene bevorzugen. .. Das ist nicht koscher. Vor allem weil man nie von alleine auf diese Lösung gekommen wäre
    – user31729
    Commented Dec 20, 2017 at 11:49
  • Okay sorry ... ich glaube, das geht Dir ans Charma - richtig? Bin nicht so oft hier (nur wenn ich Probleme habe ;-). Hab's rückgängig gemacht.
    – Victor
    Commented Dec 20, 2017 at 15:16
  • One word to xcolor vs. color: I have a LOT of documents (i.e. user manuals) which I would have to check for compatibility problems ... as long as color works for me, I'm hesitating to make the change.
    – Victor
    Commented Dec 20, 2017 at 15:17
  • Das hat mit Karma nichts zu tun...
    – user31729
    Commented Dec 20, 2017 at 16:56

You must log in to answer this question.

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