45

Many newcomers to this site have code that they entered into their notebooks using one of tje Mathematica palettes, e.g., Basic Math Assistant. They are delighted with mathematical look of their code, but when they copy such code from their notebook and paste into our editor, they often get a mess they don't know how to deal with. So they just leave the mess or they resort to making a PNG image of their code and posting that.

When they are chided by a community member for posting their code in a bad format, they are often given the advice to copy the code as plain text. My experience is that such advice is not optimal. What would constitute better advice?

  • 3
    I posted this because I think people might want to give a link to it when making a comment to newcomers about better formatting. – m_goldberg Jun 24 '15 at 15:14
  • I added the site-tools tag because I thought new users who are directed here would like to see other tools that have been developed to make the site easier to use. – Michael E2 Aug 2 '15 at 15:37
44

Here is the way I do it when I have fancy standard form code I want post on Mathematica.SE

Step-by-step

  1. Make a copy of the cell containing the code to be posted
  2. 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

  3. Select the code and do a normal copy (do not use Copy As)
  4. Paste form the clipboard in the Mathematica.SE editor pane
  5. Delete the raw input form cell from the notebook.

How it looks in a notebook

raw-input

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

  1. Select the text you want to block indent
  2. 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.

  • 2
    How does it handle Greek characters? – bbgodfrey Jun 25 '15 at 0:36
  • @bbgodfrey. Most are not affected, but π is converted to Pi. – m_goldberg Jun 25 '15 at 1:30
  • Er… what's the difference between raw input form and input form? BTW I always use Ctrl+Shift+I (this is the shortcut for input form, right?) to convert the mess and I think I never see this fails. – xzczd Jul 2 '15 at 5:59
  • Time to Accept this answer, don't you think? – Mr.Wizard Jul 6 '15 at 17:53
  • @Mr.Wizard. Really? Is their any significance to accepting one's own answer in Meta? – m_goldberg Jul 6 '15 at 17:59
  • 2
    Not a great deal I suppose, but it does show up as having an Accepted answer in the questions list, and I find that beneficial. No other methods were posted and this one is popular, so I think we can reasonably conclude that this is the "canonical way" and therefore Accept seems appropriate. – Mr.Wizard Jul 6 '15 at 18:29
  • To give a complete protocol for newbies, you might add that clicking the {} button (or Control-K) will format the code. – Michael E2 Jul 26 '15 at 19:38
  • @MichaelE2. I think that's a good idea. Will update my answer soon -- too busy right now to do it immediately. – m_goldberg Jul 26 '15 at 19:46
  • @m_goldberg Steps 1 thru 5 do not work for me. All the greek letters are written out (e.g., \[Theta]). I am on Mathematica 10.1 and using Internet Explorer 11. Do you have any tips on a work-a-round? – Jack LaVigne Aug 15 '15 at 18:15
  • @JackLaVigne. That's really a different problem from the one I address here. You will find a solution for it in this meta-post – m_goldberg Aug 15 '15 at 21:24
  • Response to 2nd update: though codes in raw input form and input form are different inside the notebook, I think they result in the same text when being copy and paste to somewhere outside of notebook e.g. this site? – xzczd Feb 8 '17 at 11:03
  • @xzczd. Sorry, but that is not so. – m_goldberg Feb 8 '17 at 11:05
  • Show me an example :) ? – xzczd Mar 31 '17 at 3:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .