Message Boards Message Boards

2
|
7859 Views
|
5 Replies
|
6 Total Likes
View groups...
Share
Share this post:

RawBoxes to expression?

Posted 7 years ago

Does anyone know how to turn RawBoxes into an expression?

In particular, I am looking at the first part of

WolframLanguageData["List", "DocumentationBasicExamples"](*[[1,1]] looks like List[a,b,c,d]*)

I was hoping for a held expression or something that is Inactive.

POSTED BY: Todd Rowland
5 Replies

Perhaps you could do:

Cases[WolframLanguageData["List","DocumentationBasicExamples"],cell_Cell:>CellPrint@cell,Infinity];

List[a,b,c,d]
{a,b,c,d}

FullForm[{a,b,c,d}]
List[a,b,c,d]
POSTED BY: Carl Woll
Posted 7 years ago
In[]:= Cases[WolframLanguageData["List","DocumentationBasicExamples"],c_Cell :>MakeExpression[c,StandardForm],Infinity]
Out[]= {HoldComplete[{a,b,c,d}],HoldComplete[{a,b,c,d}],HoldComplete[List[a,b,c,d]],HoldComplete[{a,b,c,d}]}
POSTED BY: Data Computist

Thanks Carl. I was looking for something that could be done automatically, and maybe one way is to use CellPrint and then a NotebookImport to get the Input cells.

Also thanks Data Computist. Apparently MakeExpression is making an ExpressionCell object, which presumably is something that can be ignored.

I guess that MakeExpression is an old function, while AllCases is a new one (which could replace Cases[...,Infinity])

POSTED BY: Todd Rowland

Todd I will also post an answer that I got from some folks, so just being the messenger here.


There's no guarantee that a particular RawBoxes or Cell represents a valid WL expression. But if it did, then yes, MakeExpression would be the low-level way to extract it. (See also: StripBoxes.) For example:

WolframLanguageData["Array", "DocumentationBasicExamples"][[1, 1]] // 
 MakeExpression@*First@*First

The higher-level NotebookImport can also be very convenient for processing multiple cells at once. Riffing on Carl's posted answer:

Cases[WolframLanguageData["Array", 
    "DocumentationBasicExamples"], _Cell, Infinity] // Notebook // NotebookImport
POSTED BY: Vitaliy Kaurov

Thanks everyone. I woke up in the middle of the night realizing that NotebookImport[Notebook[AllCases[expr,_Cell]] would work. Only learned about NotebookImport a few days ago although it's been around for a couple years.

POSTED BY: Todd Rowland
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract