1
$\begingroup$

I want to convert the $10^i$ tick labels generated by LogTicks of the CustomTicks package to regular numbers. The first tick label is in element [[1,2]] of the object generated by LogTicks. FullForm tells me the format is as follows:

LogTicks[0, 2][[1, 2]] // FullForm
\!\(
TagBox[
StyleBox[
RowBox[{"DisplayForm", "[", 
RowBox[{"SuperscriptBox", "[", 
RowBox[{"10", ",", "0"}], "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

Actually, in the notebook it looks different:

enter image description here

Subsequently, I can use ToExpression to obtain the regular number 1:

ToExpression[%]
(* 1 *)

But it doesn't work in one step:

ToExpression[LogTicks[0, 2][[1, 2]]]

ToExpression::notstrbox: 10^0 is not a string or a box. ToExpression can only interpret strings or boxes as Wolfram Language input.

What would be the command to get

command[LogTicks[0, 2][[1, 2]]]
(* 1 *)

and what is the reason why my approach failed?

$\endgroup$
7
  • $\begingroup$ Does this work for you general use-case? command[expr_] := First@Cases[expr, SuperscriptBox[x_, y_] :> x^y] $\endgroup$
    – march
    Feb 7, 2017 at 18:55
  • $\begingroup$ Is it possible to extend this to return expr if it can't be converted that way (e.g., some elements of LogTicks[0, 2][[All, 2]] are just empty strings "" $\endgroup$
    – Felix
    Feb 7, 2017 at 19:10
  • $\begingroup$ I'm not entirely sure about the structure of these objects, so I'm not sure how to generalize it. You could use and If statement to check the structure of expr first, and return expr if it's an empty string or something. Otherwise, why doesn't ToExpression@FullForm@expr work? $\endgroup$
    – march
    Feb 7, 2017 at 19:14
  • 1
    $\begingroup$ What about ToExpression@ToBoxes@FullForm@expr? Or maybe just ToExpression@ToBoxes@expr? $\endgroup$
    – march
    Feb 7, 2017 at 19:25
  • 1
    $\begingroup$ Yes, perfect, that works! Thanks! Do you understand why it does? Can you make it an answer? $\endgroup$
    – Felix
    Feb 7, 2017 at 19:27

0

Your Answer

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