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 (Windows/Linux) or (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]=

View the result:

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

Use some special formatting markup:

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

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]=

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]=
In[6]:=
ResourceFunction["StringTemplateInput"][
  "Export[\"file\", expr, \"WXF\"]", ShowStringCharacters -> True] // DisplayForm
Out[6]=

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]=
In[8]:=
b2 = ResourceFunction["StringTemplateInput"][
  "Table[i+1,{i,i$min,i$max}]"]
Out[8]=

StringToBoxes formats as if the characters were typed literally:

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

StringTemplateInput applies heuristics to automatically format some expressions:

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

Possible Issues (2) 

Output cells do not typically show string characters:

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

Use ShowStringCharactersTrue to force quotes to appear:

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

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]=

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]=

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]=

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