[Tex/LaTex] highlights path sequentially BUT in order of clicking

highlightinghyperrefpathstikz-pgf

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?

colored squares

\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}

Best Answer

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
  },
  hyperlink node/.style={
    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}

\hypersetup{hidelinks}

\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}