I want to make a table of elements such that when the cursor is over a target element, all other elements of the table sharing some specified common property with the target are highlighted (let's say change color and font).

I'm however having some problems in finding a simple way to achieve this. Here is a sample code that should highlight all the numbers differing from the one under the cursor for an even number:

mouseHighLightPermutationProperties[originalTable_List, rule_Symbol] :=
  With[{replacementRule = HoldForm[
     t_Integer -> EventHandler[
       {"MouseMoved" :> (table = (originalTable /. rule[t]))}
  DynamicModule[{table = originalTable},
   Dynamic[table /. ReleaseHold[replacementRule]]
highlightEvenTest[n_Integer] := 
 target_Integer?(EvenQ[# - n] &) -> Style[target, Bold, Green]
mouseHighLightPermutationProperties[Range@20, highlightEvenTest]

This almost works: the substitution when mouseovering is done, but the output is this:

enter image description here

Using FullForm reveals that the Style is printed that way because what appears to be a simple number in the output is actually an EventHandler object. The problem is thus that in my code the Style is applied to the EventHandler, instead of the EventHandler being applied to the Style. I think I see why this happens, but I'm not sure how to fix it.

Besides, this function seems rather convoluted to me. Is there a simpler, possibly flexible, way to achieve this?

rulf[x_] := Function[{y}, If[EvenQ[x - y], Style[y, Red, Bold], y]]

DynamicModule[{rul = Identity},
 Column[EventHandler[Dynamic[rul@#], {"MouseMoved" :> (rul = rulf@#)}] & /@ Range@5]

Mathematica graphics

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.