Consider the Cell
generated using the InputForm
wrapper:
a + b //InputForm
Cell["a + b", "Output",
CellChangeTimes->{{3.747528582367461*^9, 3.747528588598588*^9}, 3.747528677164525*^9},
CellLabel->"Out[186]//InputForm="]
versus the Cell
contents of the input "a+b"
Cell[BoxData[
RowBox[{"a", "+", "b"}]], "Input",
CellChangeTimes->{{3.74752883542157*^9, 3.747528837893704*^9}}]
The key difference is that the InputForm
output uses a string instead of a BoxData
as the first argument. Since the argument is a string, 2D editing is disallowed, and because it is a string, syntax coloring doesn't work.
Using InputForm
+ DisplayForm
as suggested by MrWizard produces a different kind of cell:
a + b //InputForm //DisplayForm
Cell[BoxData[
TagBox[
InterpretationBox[
StyleBox[
RowBox[{"a", " ", "+", " ", "b"}],
ShowStringCharacters->True,
NumberMarks->True],
InputForm[\$CellContext`a + $CellContext`b],
AutoDelete->True,
Editable->True],
DisplayForm]], "Output",
CellChangeTimes->{3.747529163398923*^9},
CellLabel->"Out[190]//DisplayForm="]
Notice how the cell now contains a BoxData
wrapper (as well as boxes, but that difference isn't relevant) with lots of wrappers. I don't like this approach because the boxes become rather unwieldy after 2D editing. For example, placing the cursor after the b
and using Ctrl+6 produces the following cell:
Cell[BoxData[
TagBox[
RowBox[{
StyleBox["a",
ShowStringCharacters->True,
NumberMarks->True],
StyleBox[" ",
ShowStringCharacters->True,
NumberMarks->True],
StyleBox["+",
ShowStringCharacters->True,
NumberMarks->True],
StyleBox[" ",
ShowStringCharacters->True,
NumberMarks->True],
SuperscriptBox[
StyleBox["b",
ShowStringCharacters->True,
NumberMarks->True], "\[Placeholder]"]}],
DisplayForm]], "Input",
CellChangeTimes->{{3.747529388438327*^9, 3.747529388455496*^9}}]
Notice the proliferation of StyleBox
wrappers that appears.
My suggestion is to create a new form that uses a TemplateBox
, as this will prevent the proliferation of StyleBox
wrappers:
MakeBoxes[myInputForm[expr_], StandardForm] ^:= With[
{arg = MakeBoxes[InputForm[expr]][[1,1]]},
TemplateBox[{arg},
"myInputForm",
DisplayFunction -> (StyleBox[#,ShowStringCharacters->True,NumberMarks->True]&),
InterpretationFunction->(#&)
]
]
myInputForm
produces the following cell:
Hold[f[x_] := a + b] //myInputForm
Cell[BoxData[
TemplateBox[{"Hold[f[x_] := a + b]"},
"myInputForm",
DisplayFunction->(StyleBox[#, ShowStringCharacters -> True, NumberMarks -> True]& ),
InterpretationFunction->(#& )]], "Output",
CellChangeTimes->{3.7475298985844793`*^9},
CellLabel->"Out[196]="]
If we place the cursor after the b
this time, and use Ctrl+6 we get:
Cell[BoxData[
TemplateBox[{RowBox[{"Hold", "[",
RowBox[{
RowBox[{"f", "[", "x_", "]"}], " ", ":=", " ",
RowBox[{"a", " ", "+", " ",
SuperscriptBox["b", "\[Placeholder]"]}]}], "]"}]},
"myInputForm",
DisplayFunction->(StyleBox[#, ShowStringCharacters -> True, NumberMarks -> True]& ),
InterpretationFunction->(#& )]], "Input",
CellChangeTimes->{{3.7475300096828337`*^9, 3.747530009698186*^9}}]
Notice that the argument of the TemplateBox
has changed from a string to boxes automatically, and the StyleBox
proliferation we saw earlier is absent. Also, since we have a BoxData
object, and the cell style has changed to "Input", the above cell has syntax coloring as desired. Here is a screen shot of the above rendered cell: