
我希望創建一個與滑鼠互動的圖形,假設滑鼠懸停在某個文字標籤上,然後突出顯示相關區域。下面是一個範例圖:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[yscale=-1,>=latex]
\foreach \a/\b/\c/\d/\desc [count=\j] in {
0/0/5/8/box1,
0/0/5/8/box2,
1/1/4/7/box3,
0/0/3/6/box4
}{
\path (-2,0) ++(0,\j) coordinate (A);
\draw (\a,\b) rectangle (\c,\d);
\draw[<-,red] (\a,\b) -- (A) node[anchor=east,pos=1] {\desc};
}
\end{tikzpicture}
\end{document}
如果滑鼠懸停在 box1 上,它應該突出顯示區域 (0,0) - (5,8)。我知道動畫支持,但它不是互動的(透過滑鼠位置控制)。
答案1
這是使用 ocgx2 套件的範例。檔案必須編譯兩次,需要PDF閱讀器Acrobat Reader、Foxit Reader或evince。若要啟動區域,需要按一下該方塊的名稱。
非常感謝ocgx2
套件的維護者 @AlexG,在評論中指定如何確保使用選項 啟動一層以停用其他層opts={radiobtngrp=myBoxes}}
。
\documentclass[border=2mm]{standalone}
\usepackage[tikz]{ocgx2}
\begin{document}
\begin{tikzpicture}[yscale=-1,>=latex]
\foreach \a/\b/\c/\d/\desc [count=\j] in {
0/0/5/8/box1,
0/0/2/4/box2,
1/1/4/7/box3,
0/0/3/6/box4
}{
\path (-2,0) ++(0,\j) coordinate (A);
\draw (\a,\b) rectangle (\c,\d);
\begin{scope}[ocg={name=box\j,ref=box\j,status=invisible,opts={radiobtngrp=myBoxes}}]
\fill[red!30] (\a,\b) rectangle (\c,\d);
\end{scope}
\draw[<-,red] (\a,\b) -- (A) node[anchor=east,pos=1,switch ocg=box\j] {\desc};
}
\end{tikzpicture}
\end{document}