I have a template notebook that I'd like to substitute a string, but the string could possibly contain a Hyperlink.

Here is an example template notebook:

template notebook

I create a hyperlink to a help page with:

Hyperlink["Association", "ref/Association"]

That creates the hyperlinked text which I then paste into the GenerateDocument command.

    "aString" -> "This is a string with a link to an Association"

The word "Association" is the hyperlink.

But when I generate the notebook from the template, it doesn't make the hyperlink. This is the text that is generated:

This is a string with a link to an \!\(\*TemplateBox[{"\"Association\"",{"\!\(\*Cell[TextData[ButtonBox[\"ref/Association\",BaseStyle->\"ExampleLink\",ButtonData->\"CellID\"]]]\)", None},"\!\(\*Cell[TextData[ButtonBox[\"ref/Association\",BaseStyle->\"ExampleLink\",ButtonData->\"CellID\"]]]\)"},"HyperlinkDefault"]\)

How do I put a link into a notebook generated from a template?

  • $\begingroup$ The least cumbersome way I found was to add a second slot and run GenerateDocument[nb, <|"aString" -> "text ", "link" -> Hyperlink["Associations", "ref/Associations"]|>]. $\endgroup$ – Kuba Feb 21 at 6:46
  • $\begingroup$ @Kuba That's a good idea but in my case the text can be arbitrary and may or may not contain a link anywhere in the string. So I'm looking for the more general solution. $\endgroup$ – Sean Lynch Feb 22 at 1:59

Hyperlinks in text cells are an inline cells with standard form. I don't see how to pass this via notebook template slot (together with regular text for that cell). TextData can be a list but as soon as you put something else than _String to the template slot, it will end up a standard form as a whole.

So here is another alternative:

Create new Input cell with:

enter image description here

Then set the behavior to "Evaluate and delete".

Now you can run:

printTextCell[textData : _List | _String, style_: "Text"] := NotebookWrite[
       textData // Replace[#, std : Except[_String] :> Cell@BoxData@ToBoxes@std, {1}] & 

     "aString" -> {
       "This is a string with a link to an ",  Hyperlink["Association", "ref/Association"]

enter image description here

enter image description here

| 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.