# How to convert input expression to FullForm String

Is there a way to convert an input expression to FullForm String?

Here is my attempt, which is almost there.

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := ToString[FullForm[Unevaluated[expr]], OutputForm]


But Unevaluated appears in the string, which I don't want.

fullFormString[Integrate[α^2, {α, -3, 3}]]


"Unevaluated[Integrate[Power[\\[Alpha], 2], List[\\[Alpha], -3, 3]]]"

The escaped \ is correct. I just need the canonical way to make a FullForm string without the Unevaluated.

• I suppose I could postprocess the output using StringReplace, but is there a better way? – QuantumDot Jul 27 '17 at 15:05
• Proposed duplicate: (70022) – Mr.Wizard Jul 28 '17 at 11:11

You just put Unevaluated in the wrong spot:

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := ToString[Unevaluated @ FullForm[expr]]


fullFormString[Integrate[\[Alpha]^2, {\[Alpha], -3, 3}]]


"Integrate[Power[\[Alpha], 2], List[\[Alpha], -3, 3]]"

• Oh that's very good. Do you know why Unevaluated is not stripped in FullForm? I thought Unevaluated simply sets the attributes of the outer function, rather than actually being processed by it. Also, would you have any recommendations about using your method vs @HenrikSchumacker's method (involving HoldForm)? – QuantumDot Jul 27 '17 at 19:27
• FullForm doesn't have DownValues, so the Unevaluated never gets stripped. I think Unevaluated is slightly better as it avoids evaluating anything, while the HoldForm approach evaluates the HoldForm object, which is inert because of it's Hold attributes, and the HoldForm approach relies on correct formatting of HoldForm when converting to OutputForm. For example, HoldForm doesn't disappear when converting to InputForm. – Carl Woll Jul 27 '17 at 19:57
• Ah I see; that makes sense. I also notice FullForm doesn't disappear when converting to InputForm either. It's stripped only when converting to OutputForm. I wish I understood this part of the evaluation process better. – QuantumDot Jul 27 '17 at 20:05

Maybe this way?

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := ToString[HoldForm[FullForm[expr]], OutputForm]


Or maybe this way?

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] :=
StringReplace[
ToString[FullForm[Unevaluated[expr]], OutputForm],
"Unevaluated[" ~~ Longest[u__] ~~ "]" -> u]

fullFormString[Integrate[α^2, {α, -3, 3}]]


"Integrate[Power[\\[Alpha], 2], List[\\[Alpha], -3, 3]]"

• This works, but it's very similar to my "postprocessing" method mentioned in my comments. @Henrik's method seems to be working for all my cases. Is there really no built-in function to achieve my task? – QuantumDot Jul 27 '17 at 16:25
• @QuantumDot. Not that I know of. – m_goldberg Jul 27 '17 at 16:33