5
$\begingroup$
expr = First@
  Level[Values@
    WolframLanguageData["Plot", 
     EntityProperty["WolframLanguageSymbol", 
      "DocumentationExampleInputs"]], {3}]

Plot[Sin[x], {x, 0, 6 Pi}]

But I find the Head of expr is RawBoxes

Head@expr
(* RawBoxes *)

And I cannot get the string like "Plot[Sin[x],{x,0,6 Pi}]"

ToString@expr
(* "RawBoxes[Cell[BoxData[RowBox[{Plot, [, RowBox[{RowBox[{Sin, 
[, x, ]}], ,, RowBox[{{, RowBox[{x, ,, 0, ,, RowBox[{6, Pi}]}], 
}}]}], ]}]], Input, ShowStringCharacters -> True]]" *)
$\endgroup$
2
  • $\begingroup$ Did you look at MakeExpression or ToExpression? $\endgroup$ Sep 2, 2016 at 17:31
  • $\begingroup$ @BrunoLeFloch Yes,I have.And fail to make it. $\endgroup$
    – yode
    Sep 2, 2016 at 17:32

3 Answers 3

4
$\begingroup$

Actually MakeExpression works. If you want to evaluate that expression,

expr//ToBoxes//MakeExpression//ReleaseHold

will work. If you want to get similar output as Wjx

(expr // ToBoxes // MakeExpression) /. ExpressionCell[a_, __] :> a
$\endgroup$
2
$\begingroup$

Will this help?

Cases[Hold[Plot[Sin[x], {x, 0, 6 Pi}]], ExpressionCell[cont_, ___] :> Hold@cont, Infinity]

{Hold[Plot[Sin[x], {x, 0, 6 [Pi]}]]}

A trick using Cases to extract stuffs in a Held Expression. Is this what you need? :)

Also, if your real application needs you to convert them into a string:

Cases[Hold[Plot[Sin[x], {x, 0, 6 Pi}]], 
  ExpressionCell[cont_, ___] :> ToString@Unevaluated@cont, 
  Infinity][[1]]

"Plot[Sin[x], {x, 0, 6 Pi}]"

$\endgroup$
1
$\begingroup$

A solution that is not very satisfactory is to grab the argument of BoxData and turn that into an expression.

expr = First@
  Level[Values@
    WolframLanguageData["Plot", 
     EntityProperty["WolframLanguageSymbol", 
      "DocumentationExampleInputs"]], {3}]
FirstCase[expr, BoxData[data_]:>data,,Infinity]//ToExpression

(one can also use MakeExpression[#,StandardForm]& instead of ToExpression to get a held expression).

$\endgroup$
2
  • $\begingroup$ I cannot very understand your FirstCase.Have you see this post? $\endgroup$
    – yode
    Sep 2, 2016 at 18:09
  • $\begingroup$ The goal is to extract the argument of BoxData in expr. The pattern BoxData[_] matches BoxData with any single argument; I've named that argument data. Then :> introduces a rule, whose right-hand side is data, meaning that I extract the argument I've named data. The empty argument (between commas) is the default value used in case BoxData[_] did not match anything in your expr, and the Infinity describes at which levels to operate. Contrarily to your other question here we are working with expressions and not just strings so no difficulty matching paired delimiters. $\endgroup$ Sep 2, 2016 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.