6

Is it possible to override the hyperref instruction of showing the references in a specific colour but only within the context of TikZ and pgfplots? The problem is that I am using legend to ref and \ref in a tikzpicture environment and the text of the legend becomes blue because of the hyperref global setting. I don't know how to force legend's text colour or even if that would work, but the solution I am looking for should be something that can be set globally in the preamble if possible.

MWE:

\documentclass{article}

\usepackage{tikz,pgfplots} \usepgfplotslibrary{groupplots} \usepackage[colorlinks,linkcolor={blue}]{hyperref}

\begin{document} \begin{figure}[] \centering \begin{tikzpicture} % \hypersetup{hidelinks} % jfbu's solution: Uncomment to get black text. \begin{groupplot}[ group style={ group size=2 by 1,},] \nextgroupplot[scale only axis,width=0.35\textwidth,height=0.35\textwidth,legend to name=grouplegend,] \addplot coordinates {(1,1) (2,2) (3,3)}; \addlegendentry{Legend 1} \nextgroupplot[scale only axis,width=0.35\textwidth,height=0.35\textwidth] \addplot coordinates {(1,1) (2,2) (3,3)}; \end{groupplot} \node (legend) at ($(group c1r1.south)!0.5!(group c2r1.south)$) [below, yshift=-3\pgfkeysvalueof{/pgfplots/every axis title shift}] {\ref{grouplegend}}; \end{tikzpicture}% \caption{The caption.} \end{figure} \end{document}

enter image description here

jfbu's solution of placing \hypersetup{hidelinks} in the tikzpicture environment works by maintaining the links (I'm not interested in that) but showing the text in black. I'm looking for a way to achieve this globally though.

sudosensei
  • 4,072
  • I have successfully used \hypersetup{hidelinks} inside a group (and environments create groups) in the past to address a similar issue. This cures the color problem by suppressing the links. I sort of remember having tried unsuccessfully to change the linkcolor using similarly other hyperref options locally. –  Jul 30 '13 at 15:01
  • Could you elaborate please? I am a beginner. Sorry. – sudosensei Jul 30 '13 at 15:04
  • 1
    Just insert \hypersetup{hidelinks} as the first thing inside your tikzpicture. Perhaps this will work. –  Jul 30 '13 at 15:12
  • @jfbu: Placing it inside the tikzpicture environment works. But I need something that can be set globally. @Jake: Yes, I'm editing my post now. – sudosensei Jul 30 '13 at 15:53
  • 2
    @sudosensei: You can set the option for all tikzpictures using \tikzset{ every picture/.style={ execute at begin picture=\hypersetup{hidelinks} } } – Jake Jul 30 '13 at 16:04
  • @Jake: It works great! Thank you so much. Could you please convert this into an answer so that I can accept it? – sudosensei Jul 30 '13 at 16:10
  • @jfbu: Could you write up an answer? – Jake Jul 30 '13 at 16:13
  • @Jake: please do; I so rarely did things with tikzpicture that I could not decently be credited with an answer related to its use... –  Jul 30 '13 at 16:22
  • @Jake, and additionnally I said this would suppress the links, but it seems it does not, it just suppresses the color. –  Jul 30 '13 at 16:24
  • @jfbu: It does because I externalize the tikzpicture. ;-) – sudosensei Jul 30 '13 at 16:29

2 Answers2

6

While scouring through the pgfplots manual, I happened to stumble upon a more elegant solution. Simply, instead of invoking a command that temporarily suspends hyperref before each picture, it is possible to just replace \ref{refname} with \pgfplotslegendfromname{refname}.

From the pgfplots manual on page 189:

\pgfplotslegendfromname{<name>}
This command poses an equivalent alternative for \ref{<name>}: it has essentially the same effect, but it does not create links when used with the hyperref package.

sudosensei
  • 4,072
  • 1
    Oh, that's indeed much more elegant! Good find! You should accept this instead of my answer, so others can find the proper approach more easily. – Jake Aug 16 '13 at 06:27
5

You can disable the hyperref links in tikzpictures by using Stefan Kottwitz's approach from Selectively turn off hyperref links and wrapping it in execute at begin picture:

\makeatletter
\tikzset{
    every picture/.style={
        execute at begin picture={
            \let\ref\@refstar
        }
    }
}
\makeatother

Jake
  • 232,450