Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Convert a string into boxes as if it were typed into a notebook
ResourceFunction["StringToBoxes"][string] converts string into boxes as if it were typed into a notebook. |
Convert a string into boxes:
In[1]:= | ![]() |
Out[1]= | ![]() |
View the DisplayForm:
In[2]:= | ![]() |
Out[2]= | ![]() |
View as an input cell:
In[3]:= | ![]() |
The string does not need to be valid syntax:
In[4]:= | ![]() |
Out[4]= | ![]() |
In[5]:= | ![]() |
Out[5]= | ![]() |
Convert a linear syntax string into boxes:
In[6]:= | ![]() |
Out[6]= | ![]() |
In[7]:= | ![]() |
Out[7]= | ![]() |
In[8]:= | ![]() |
Out[8]= | ![]() |
In[9]:= | ![]() |
Out[9]= | ![]() |
The boxes are interpreted as if the string were typed into a notebook:
In[10]:= | ![]() |
Out[10]= | ![]() |
In[11]:= | ![]() |
Out[11]= | ![]() |
Compare to using ToExpression and MakeBoxes:
In[12]:= | ![]() |
Out[12]= | ![]() |
This results in an error:
In[13]:= | ![]() |
Out[13]= | ![]() |
StringToBoxes can handle invalid syntax:
In[14]:= | ![]() |
Out[14]= | ![]() |
This means the resulting boxes will not be valid syntax either:
In[15]:= | ![]() |
Out[15]= | ![]() |
The boxes are valid, however, and can be displayed by the front end:
In[16]:= | ![]() |
By default, whitespace will be preserved in the output:
In[17]:= | ![]() |
Out[17]= | ![]() |
In[18]:= | ![]() |
Out[18]= | ![]() |
Ignore whitespace:
In[19]:= | ![]() |
Out[19]= | ![]() |
In[20]:= | ![]() |
Out[20]= | ![]() |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License