I could only get the kind of behavior you seek with an EventHandler
and PassDownEvents -> True
, in this case ClickPane
.
Manually
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.
ClickPane[
Style["This is a link to HL but you can click anywhere else to open/close the cell group",
"Section"],
(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).
EventHandler[
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).
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.
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"
.
One drawback is that single letters are italicized, as though they are mathematical variables. You can manually select the "a"
and unitalicize.
Programatically
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_] :=
CellPrint@(Cell[BoxData@ToBoxes[
ClickPane[
Row[
Module[{n = 0},
Replace[
StringCases[str, "``" | Whitespace | Except[WhitespaceCharacter] ..],
"``" :> EventHandler[links[[++n]], {"MouseClicked" :> (Null)},
PassEventsDown -> True, PassEventsUp -> False],
1]],
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"]},
"Section"]
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"]},
"Subsection"]