Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

My problem is: I would like to activate WholeCellGroupOpener on a cell containing hyperlinks. But when I do that, the hyperlinks are no longer active. Is there a way to change the order of the two, so that 'Hyperlink' is activated before 'WholeCellGroupOpener'.

More precisely: I want the cell group to collapse when I click anything but a hyperlink.

Does anyone know a way to achieve that?

share|improve this question
Could you provide the simple code that you tried? –  halmir Jul 31 '13 at 22:58
add comment

1 Answer

I could only get the kind of behavior you seek with an EventHandler and PassDownEvents -> True, in this case ClickPane.


Below is the ClickPane. We're going to replace "HL" manually by a Hyperlink later. First execute this and we'll paster the output later into the desired cell. I will use a "Section" cell for illustration; hence the style "Section". Alter to suit.

 Style["This is a link to HL but you can click anywhere else to open/close the cell group", 
 (SelectionMove[InputNotebook[], All, CellGroup]; 
   FrontEndExecute[FrontEndToken["OpenCloseGroup"]]) &, 
 PassEventsDown -> True]

Second, create a hyperlink. We'll paste this later, too. We wrap it in an EventHandler so that click on the hyperlink does not close the cell group (because of PassEventsUp -> False). Styling the text as "Hyperlink" makes be colored like a hyperlink; without this, it gets the standard cell styling ("Section" in our example).

 Hyperlink[Style["Google", "Hyperlink"], "http://www.google.com"],
 {"MouseClicked" :> (Null)},
 PassEventsDown -> True, PassEventsUp -> False]

Third, create the cell you want, say, a new section cell.

Fourth, select the cell bracket and copy the whole cell containing the ClickPane output.

Fifth, create an inline cell in the section (Control-9).

enter image description here

Sixth, paste the ClickPane output. You won't be able to edit it with the mouse. A mouse click will open or close the cell group. Edit by click next to the cell and use your arrow keys.

Mathematica graphics

Seventh, using your arrow keys and backspace, get rid of the extra quotation marks.

Eighth, copy the hyperlink.

Ninth, using your arrow and shift keys, select the "HL". Paste the hyperlink. It should automatically style to "Section".

Mathematica graphics

One drawback is that single letters are italicized, as though they are mathematical variables. You can manually select the "a" and unitalicize.


Below is a function to create a cell. The str follows a similar syntax as in StringForm: for each "``" in str Replace substitutes each link in turn from the list links. (Note links here is to be a list, which is different from StringForm.) ToBoxes is pretty aggressive about putting extra quotes in, and the replace code at the end strips the extra quotes from the appropriate place.

openerHLForm[str_, links_, style_] := 
          Module[{n = 0},
            StringCases[str, "``" | Whitespace | Except[WhitespaceCharacter] ..],
            "``" :> EventHandler[links[[++n]], {"MouseClicked" :> (Null)}, 
                      PassEventsDown -> True, PassEventsUp -> False],
          BaseStyle -> style],
        (SelectionMove[InputNotebook[], All, CellGroup]; 
          FrontEndExecute[FrontEndToken["OpenCloseGroup"]]) &,
        PassEventsDown -> True],
       StandardForm], style] /. 
    tb_TemplateBox :> 
     MapAt[Replace[#, s_String :> StringTrim[s, "\""]] & /@ # &, tb, 1])

The patterns are not thoroughly tested, so you may have to adjust things for overly complicated settings. Note that single letters are not italicized.

openerHLForm["This is a link to `` but you can click anywhere else to open/close the cell group",
 {Hyperlink[Style["Google", "Hyperlink"], "http://www.google.com"]},

Mathematica graphics

openerHLForm["This is a link to `` but you can click anywhere else, \
except ``, to open/close the cell group",
 {Hyperlink[Style["Google", "Hyperlink"], "http://www.google.com"], 
  Hyperlink[Style["Wolfram", "Hyperlink"], "http://www.wolfram.com"]},

Mathematica graphics

share|improve this answer
Thank you very much for your time and your useful and thorough explanation, but I am hoping for a simpler answer as I need to do this to a lot of cells. –  Sofic Aug 1 '13 at 12:21
@Sofic There are others with much more experience dealing with the front end. Let's hope there is a simpler way! :) –  Michael E2 Aug 1 '13 at 12:30
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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