Here is the way I do it when I have fancy standard form code I want post on Mathematica.SE
Step-by-step
- Make a copy of the cell containing the code to be posted
Convert the copied cell to raw input form
Choose Convert To > Raw InputForm from the Cell menu or from the contextual (right mouse clidk) menu
- Select the code and do a normal copy (do not use Copy As)
- Paste form the clipboard in the Mathematica.SE editor pane
- Delete the raw input form cell from the notebook.
How it looks in a notebook
Update
This is aimed at newcomers. After you have pasted your code into an editor input pane, you are not done. The code must be block indented by a minimum of four spaces for the editor to recognize that it is code.
Here is some properly indented code without a block indent. It looks terrible because it is displayed without its line breaks and with all the leading indents are compressed to one space.
With[{n = 524},
xpts = Table[{i h, x[i]}, {i, 0, n}];
ypts = Table[{i h, y[i]}, {i, 0, n}];]
After carrying out the simple instructions given below, it looks good.
With[{n = 524},
xpts = Table[{i h, x[i]}, {i, 0, n}];
ypts = Table[{i h, y[i]}, {i, 0, n}];]
One way to make a block indent is to use the { } tool from the editor tool bar
- Select the text you want to block indent
- Click on the { } icon in the tool bar at the top of the editor input pane
The keyboard shortcut for clicking on the { } tool is Ctrl+K.
2nd update
Response to xzczd's comment
The cells produced are quite different.
InputForm
Cell[BoxData[
RowBox[{"Sum", "[",
RowBox[{
RowBox[{
RowBox[{"Log", "[", "i", "]"}], "/",
RowBox[{"i", "^", "2"}]}], ",", " ",
RowBox[{"{",
RowBox[{"i", ",", " ", "1", ",", " ", "Infinity"}], "}"}]}], "]"}]], "Input",
FormatType->"InputForm"]
Raw InputForm
Cell["Sum[Log[i]/i^2, {i, 1, Infinity}]", "Input"]
InputForm allows code coloring and other code editor formatting, but Raw InputForm is cleaner and to my mind better for copy and paste.