# highlights path sequentially BUT in order of clicking

I have been looking for a possibly difficult answer to a simple question. I would like to be able to highlight paths on a TikZ picture. I would like to highlight these paths by clicking on them and at each step retain the previous steps.

For instance, in the MWE below, I would like to start with the 4 empty squares, and have the color appear on each of them by clicking on it. And considering that any path can be chosen. I guess I can do it by using {hyperref} and identify all the possible ways, but there must be some easier way I am sure.

Any idea?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{box1/.style={draw=black, thick, rectangle,rounded corners, minimum height=2cm, minimum width=2cm}}

\begin{document}
\begin{tikzpicture}

\node[box1, fill=white] (c1) {1};
\node[box1, fill=white, right=1cm of c1] (c2) {2};
\node[box1, fill=white, below=1cm of c2] (c3) {3};
\node[box1, fill=white, left=1cm of c3] (c3) {4};

\node[box1, fill=red, right=5cm of c1] (c21) {1};
\node[box1, fill=blue, right=1cm of c21] (c22) {2};
\node[box1, fill=orange, below=1cm of c22] (c23) {3};
\node[box1, fill=green, left=1cm of c23] (c23) {4};
\end{tikzpicture}
\end{document}

-

This could be automated even further, and it would be nice if the hyperlinks were the same size as the nodes, but it shows - I think - the main idea.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/61020/86}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{box1/.style={draw=black, thick, rectangle,rounded corners, minimum height=2cm, minimum width=2cm}}
\usepackage{hyperref}

\colorlet{picture-1-1}{red}
\colorlet{picture-2-1}{blue}
\colorlet{picture-3-1}{orange}
\colorlet{picture-4-1}{green}
\colorlet{picture-1-0}{white}
\colorlet{picture-2-0}{white}
\colorlet{picture-3-0}{white}
\colorlet{picture-4-0}{white}

\begin{document}
\foreach \n in {0,...,15} {
\pgfmathtruncatemacro\i{mod(\n,2)}
\pgfmathtruncatemacro\j{mod(int(\n/2),2)}
\pgfmathtruncatemacro\k{mod(int(\n/4),2)}
\pgfmathtruncatemacro\l{mod(int(\n/8),2)}
\pgfmathparse{\i == 0 ? "\noexpand\hyperlink{picture-1-\j-\k-\l}{1}" : 1}
\let\pictexti=\pgfmathresult
\pgfmathparse{\j == 0 ? "\noexpand\hyperlink{picture-\i-1-\k-\l}{2}" : 2}
\let\pictextj=\pgfmathresult
\pgfmathparse{\k == 0 ? "\noexpand\hyperlink{picture-\i-\j-1-\l}{3}" : 3}
\let\pictextk=\pgfmathresult
\pgfmathparse{\l == 0 ? "\noexpand\hyperlink{picture-\i-\j-\k-1}{4}" : 4}
\let\pictextl=\pgfmathresult
\hypertarget{picture-\i-\j-\k-\l}{%
\begin{tikzpicture}
\node[box1, fill=picture-1-\i] (c1) {\pictexti};
\node[box1, fill=picture-2-\j, right=1cm of c1] (c2) {\pictextj};
\node[box1, fill=picture-3-\k, below=1cm of c2] (c3) {\pictextk};
\node[box1, fill=picture-4-\l, left=1cm of c3] (c3) {\pictextl};
\end{tikzpicture}}
\newpage
}
\end{document}


Each possible configuration is generated, and each unfilled square is linked to the next appropriate configuration. By labelling the configurations according to which squares are filled, it is very easy to sort out the paths in the graph linking the configurations.

Update 2013-08-31 This version allows for two-way clicking, so a coloured node is a link to the uncoloured one and vice versa. It actually makes the code a smidgeon simpler. To offset that, I've included Jake's excellent code for making the entire node clickable (and used hidelinks to hide the red boxes).

\documentclass{article}
%\url{http://tex.stackexchange.com/q/61020/86}
\usepackage{tikz}
\usepackage{hyperref}
\usetikzlibrary{positioning,calc}

\tikzset{
box1/.style={
draw=black,
thick,
rectangle,
rounded corners,
minimum height=2cm,
minimum width=2cm
},
alias=sourcenode,
append after command={
let \p1 = (sourcenode.north west),
\p2=(sourcenode.south east),
\n1={\x2-\x1},
\n2={\y1-\y2} in
node [inner sep=0pt, outer sep=0pt,anchor=north west,at=(\p1)] {\hyperlink{#1}{\phantom{\rule{\n1}{\n2}}}}
}
}
}

\colorlet{picture-1-1}{red}
\colorlet{picture-2-1}{blue}
\colorlet{picture-3-1}{orange}
\colorlet{picture-4-1}{green}
\colorlet{picture-1-0}{white}
\colorlet{picture-2-0}{white}
\colorlet{picture-3-0}{white}
\colorlet{picture-4-0}{white}

\begin{document}
\foreach \n in {0,...,15} {
\pgfmathtruncatemacro\i{mod(\n,2)}
\pgfmathtruncatemacro\j{mod(int(\n/2),2)}
\pgfmathtruncatemacro\k{mod(int(\n/4),2)}
\pgfmathtruncatemacro\l{mod(int(\n/8),2)}
\pgfmathparse{int(1-\i)}
\edef\pictexti{picture-\pgfmathresult-\j-\k-\l}
\pgfmathparse{int(1-\j)}
\edef\pictextj{picture-\i-\pgfmathresult-\k-\l}
\pgfmathparse{int(1-\k)}
\edef\pictextk{picture-\i-\j-\pgfmathresult-\l}
\pgfmathparse{int(1-\l)}
\edef\pictextl{picture-\i-\j-\k-\pgfmathresult}
\edef\picname{picture-\i-\j-\k-\l}
\hypertarget{picture-\i-\j-\k-\l}{%
\begin{tikzpicture}
\node[box1, fill=picture-1-\i, hyperlink node=\pictexti] (c1) {1};
\node[box1, fill=picture-2-\j, hyperlink node=\pictextj, right=1cm of c1] (c2) {2};
\node[box1, fill=picture-3-\k, hyperlink node=\pictextk, below=1cm of c2] (c3) {3};
\node[box1, fill=picture-4-\l, hyperlink node=\pictextl, left=1cm of c3] (c3) {4};
\end{tikzpicture}}
\newpage
}
\end{document}

-
Very nice! Although I wonder if the OP wanted the colours to always be assigned in the same order (first red, then orange, etc.)? For making the whole nodes clickable, there are possible approaches here: tex.stackexchange.com/questions/36109/… and tex.stackexchange.com/questions/60329/… –  Jake Jun 24 '12 at 20:26
@Jake Then the graph would be a tree as you could reconstruct the path taken by the colours. Same principle, but more pages: I make it 64. –  Loop Space Jun 24 '12 at 20:58
Thanks Stacey. Although I get the following error when I run the code: package pgf math error: unknown function `int'}. It looks like I am going to have to reinstall TeXlive from tug.org/texlive. Will come back ASAP. –  jejuba Jun 24 '12 at 21:54
I meant thanks Andrew. –  jejuba Jun 24 '12 at 22:05
Hi Andrew, well it appears I won't be able to test your solution in the near future. I haven't been able to correct the error I get. I tried different things, but it doesn't work. I leave for 2 months at the end of the week, with no access to computer, and I have too much work to do before to sort this out. In case, there was some doubt, I do not wish the square to be highlighted in any specific order; i wish that anybody can choose a different route to get to the 4 colored squares, starting from the 4 empty ones. Hope this helps: see you in a few months to accept the answer. Best, –  jejuba Jun 25 '12 at 22:55