Function Repository Resource:

StringTemplateInput

Source Notebook

Automatically format a string into boxes for documentation purposes

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["StringTemplateInput"]["string"]

converts "string" into boxes with automatic formatting intended for documentation notebooks.

Details and Options

ResourceFunction["StringTemplateInput"] generates boxes that are similar to what you would get by using the Template Input button on resource function definition notebooks.
You can see how box structures generated by ResourceFunction["StringTemplateInput"] would be displayed by using DisplayForm.
Use Image (Windows/Linux) or Image (macOS) to view the underlying boxes of a selected cell. »
ResourceFunction["StringTemplateInput"] accepts the same options as Style.

Examples

Basic Examples (3) 

Convert a string into formatted boxes:

In[1]:=
ResourceFunction["StringTemplateInput"]["f[x,y]"]
Out[1]=
Image

View the result:

In[2]:=
DisplayForm[%]
Out[2]=
Image

Use some special formatting markup:

In[3]:=
ResourceFunction["StringTemplateInput"][
  "f[x$1,x$2,$$]"] // DisplayForm
Out[3]=
Image

Automatically link to documentation pages when appropriate:

In[4]:=
ResourceFunction["StringTemplateInput"][
  "Table[expr, {i, i$min, i$max}, {j, j$min, j$max}, $$]"] // DisplayForm
Out[4]=
Image

Scope (1) 

Some strings will result in documentation links to non-symbol pages:

In[5]:=
ResourceFunction["StringTemplateInput"][
  "Entity[\"Country\", \"UnitedStates\"]", ShowStringCharacters -> True] // DisplayForm
Out[5]=
Image
In[6]:=
ResourceFunction["StringTemplateInput"][
  "Export[\"file\", expr, \"WXF\"]", ShowStringCharacters -> True] // DisplayForm
Out[6]=
Image

Properties and Relations (3) 

StringTemplateInput is similar to the resource function StringToBoxes:

In[7]:=
b1 = ResourceFunction["StringToBoxes"]["Table[i+1,{i,i$min,i$max}]"]
Out[7]=
Image
In[8]:=
b2 = ResourceFunction["StringTemplateInput"][
  "Table[i+1,{i,i$min,i$max}]"]
Out[8]=
Image

StringToBoxes formats as if the characters were typed literally:

In[9]:=
DisplayForm[b1]
Out[9]=
Image

StringTemplateInput applies heuristics to automatically format some expressions:

In[10]:=
DisplayForm[b2]
Out[10]=
Image

Possible Issues (2) 

Output cells do not typically show string characters:

In[11]:=
ResourceFunction["StringTemplateInput"][
  "ResourceFunction[\"BirdSay\"]"] // DisplayForm
Out[11]=
Image

Use ShowStringCharactersTrue to force quotes to appear:

In[12]:=
ResourceFunction["StringTemplateInput"][
  "ResourceFunction[\"BirdSay\"]", ShowStringCharacters -> True] // DisplayForm
Out[12]=
Image

When ambiguous, StringTemplateInput guesses which documentation page to target for links and is unaware of context:

In[13]:=
ResourceFunction["StringTemplateInput"]["Interpreter[\"Country\"]", ShowStringCharacters -> True] // DisplayForm
Out[13]=
Image

In this case, "Country" linked to the documentation page for the entity type, and not the interpreter type. In these cases, it may be better to manually construct the desired link:

In[14]:=
Hyperlink["\"Country\"", "paclet:ref/interpreter/Country", BaseStyle -> "Link"]
Out[14]=
Image

Neat Examples (1) 

Create your very own Template Input button that can be used in any notebook:

In[15]:=
Button["Template Input", With[{cell = Cell[BoxData[NotebookRead[InputNotebook[]]]]}, NotebookWrite[InputNotebook[], ResourceFunction["StringTemplateInput"][
    First[FrontEndExecute[ExportPacket[cell, "PlainText"]]]]]]]
Out[15]=
Image

Select the following text and click the button to test it: f[x$1,x$2,$$]

Version History

  • 1.0.0 – 26 May 2020

Related Resources

License Information