# Cross-reference and tikzpicture conflict?

When I Include this diagram to my memoir document

% Requires \usetikzlibrary{shadows,positioning,backgrounds}

\centering
\begin{tikzpicture}[x=11.8mm,y=8mm]
% some colors
\colorlet{even}{cyan!60!black}
\colorlet{odd}{orange!100!black}
\colorlet{back}{yellow!20!white}
% some styles
\tikzset{
box/.style={
minimum height=5mm,
inner sep=.7mm,
outer sep=0mm,
text width=10mm,
text centered,
font=\small\bfseries\sffamily,
text=#1!50!black,
draw=#1,
line width=.25mm,
top color=#1!5,
bottom color=#1!40,
rounded corners=2.3mm,
rotate=0,
},
}
% Pascal's triangle
% row #0 => value is 1
\node[box=odd] (p-0-0) at (0,0) {1};
\foreach \row in {1,...,14} {
% col #0 =&gt; value is 1
\node[box=odd] (p-\row-0) at (-\row/2,-\row) {1};
\pgfmathsetmacro{\value}{1};
\foreach \col in {1,...,\row} {
% iterative formula : val = precval * (row-col+1)/col
% (+ 0.5 to bypass rounding errors)
\pgfmathtruncatemacro{\value}{\value*((\row-\col+1)/\col)+0.5};
\global\let\value=\value
% position of each value
\coordinate (pos) at (-\row/2+\col,-\row);
% odd color for odd value and even color for even value
\pgfmathtruncatemacro{\rest}{mod(\value,2)}
\ifnum \rest=0
\node[box=even] (p-\row-\col) at (pos) {\value};
\else
\node[box=odd] (p-\row-\col) at (pos) {\value};
\fi
% for arrows and plus sign
\ifnum \col<\row
\node[plus,above=0mm of p-\row-\col]{+};
\pgfmathtruncatemacro{\prow}{\row-1}
\pgfmathtruncatemacro{\pcol}{\col-1}
\draw[link] (p-\prow-\pcol) -- (p-\row-\col);
\draw[link] ( p-\prow-\col) -- (p-\row-\col);
\fi
}
}
\begin{pgfonlayer}{background}
% filling and drawing with the same color to enlarge background
\path[draw=back,fill=back,line width=5mm,rounded corners=2.5mm]
(  p-0-0.north west) -- (  p-0-0.north east) --
(p-14-14.north east) -- (p-14-14.south east) --
( p-14-0.south west) -- ( p-14-0.north west) --
cycle;
\end{pgfonlayer}
\end{tikzpicture}


This package doesn't work

\usepackage{hyperref}


and when I remove the package the diagram looks fine. How could I resolve this and get both of them(the reference and the diagram at once)?

• Please, provide Minimal Working Example (MWE) which we can copy to our computers and test it to see, where is your problem. So far from your code snippets doesn't follow, that they contain some error. – Zarko Jun 20 '16 at 12:03
• Also, what does 'doesn't work' mean? Do you get a compilation error? What is it? Does it give undesirable output? What's wrong with it? When you add your MWE, the answers may be obvious, but it is always better not to assume that. In the case of an error, it matters we know which error you get so we can tell if it is reproducing. In the case of undesired output, it matters a lot that we know what you find undesirable. – cfr Jun 20 '16 at 12:12
• can you change the names of those some colors and try again? – percusse Jun 20 '16 at 12:17

Do not say

\global\let\value=\value


You need to pick something other than \value and other than any other existing macro which you wish to later use, either implicitly or explicitly.

When you use \pgfmathsetmacro, TeX defines the macro to whatever you say, even if the macro already exists. I'm not sure what

\global\let\value=\value


is intended to do, but what it is doing is assigning the current value to the macro globally. This breaks all subsequent use of \value in the document. Since all cross-references depend on this, it naturally breaks all later cross-references.

When choosing macro names - especially when making them global - it is essential to either define them in a way which will include a check or check yourself that your choice of name is a safe one.

LaTeX's \newcommand and \newenvironment include checks, so do xparse's \NewDocumentCommand and \NewDocumentEnvironment. \def, \edef, \xdef, \gdef etc. do not and, hence, neither to the PGF commands which depend on them. These therefore require particular care and should be used only when the LaTeX alternatives are not an option.

For further explanation see my answer to a question about a different symptom of the same problem.