# Including Mathematica code in a response

What is the best way to visually include Mathematica code in a response?

I am almost done writing one right now, and I'd like to include a small amount of code which shows a computation demonstrating a result I claim in the response.

Edit.

Here is art of what the code. I realized that of I right clicked on the code and chose Copy As > MathML then I would pretty much get what I wanted (see below). Is this the best I can do? I mean, I guess the only more I could want is to have the text set in Courier so it looks more "code-ish."

Rx [ θ_ ] := { { 1 , 0 , 0 } , { 0 , Cos [ θ ] , Sin [ θ ] } , { 0 , - Sin [ θ ] , Cos [ θ ] } }
• Unfortunately this site doesn't have prettyprint/prettify enabled, otherwise you would be able to get the code highlighted like on Mathematica. – Manishearth Aug 13 '13 at 7:22
• You could try writing it in a code block, start with three consecutive ticks "" and end it with three consecutive ticks. – Alex Nelson Mar 7 '14 at 17:16

The '{}' button at the top of the editor or pressing Control-k will indent a selected block by 4-spaces putting it in code block mode. Like this:

Rx[θ_] := {{1, 0, 0}, {0, Cos[θ], Sin[θ]}, {0, -Sin[θ], Cos[θ]}}

• Yeah the only thing that bothers me about this is that in my code, I have used greek characters which I'm not sure how to deal with in the block mode. – joshphysics Aug 12 '13 at 21:37
• Er...I'm unsure how to help with that. The formatting sandbox will let you try out everything except MathJax in a place where people won't be bothered. – dmckee Aug 12 '13 at 21:41
• Ok...I might just use an image with a screenshot I suppose. – joshphysics Aug 12 '13 at 21:43
• @josh I would suggest trying really hard to avoid including an image of text. (At least in a main-site question. If you're demonstrating a rendering problem on meta, it's fine.) Actually, I'm not sure I understand just what problem you're having. (In theory SE supports Unicode, including Greek letters) Maybe if you were to include a demonstration in your question, it'd be clearer? – David Z Aug 12 '13 at 22:59
• @DavidZ I added an edit; hopefully the question is clearer now. – joshphysics Aug 13 '13 at 2:22
• @josh hm, that should work. See the example I edited into dmckee's answer. (dmckee: hope you don't mind. I didn't think that one example would be appropriate material for a whole new answer.) – David Z Aug 13 '13 at 2:31
• @DavidZ I'm perhaps being a bit dense here, but how did you typeset the theta character? – joshphysics Aug 13 '13 at 2:57
• @josh I just copied and pasted from your question. – David Z Aug 13 '13 at 3:02
• @joshphysics I think he used unicode. How you do that depends on your OS. – dmckee Aug 13 '13 at 3:02
• Oh yeah, that too. You can get a theta using the Alt+digits shortcuts or the Character Map on Windows, or by using a suitable alternate keyboard layout on any OS. But in this case copy+paste was the easiest way. – David Z Aug 13 '13 at 3:04
• @DavidZ Ah ok thanks. – joshphysics Aug 13 '13 at 3:25
• @joshphysics One trick I use is to type &theta; (when not on Windows) or $\theta$ in the text box and copy the resultant character from the result. – Manishearth Aug 13 '13 at 7:25
• @Manishearth Cool thanks. Too bad we don't have prettification :( – joshphysics Aug 13 '13 at 7:27
• @joshphysics: Use this for reference: αβγδΔΔεζηθϑΘι∫κλΛμvoπ∏ρσςτυφϕΦχψΨωΩ√∂∇∴∠∞⇔ℝℂ∅°(who uses degrees?.)× – Abhimanyu Pallavi Sudhir Aug 13 '13 at 13:14

The best option is to use the {} button, or Ctrl + k, to create four indenting spaces which will format code like

this,


as dmckee pointed out.

I would generally recommend not altering the Mathematica code. When posting any code on the site, it should be in a form that it will run directly after being copied and pasted.

Now, as it happens, Mathematica will automatically turn Greek ASCII characters, like θ, directly to their InputForm representation (i.e. \[Theta]) immediately upon pasting. It is therefore OK to change those characters to their ASCII representation. This is, in fact, what the mathematica.se prettify userscript does.

More generally, though, you can't expect all your Mathematica code to be made completely clear. Take, for example, this piece of code,

Sound[{Play[Re[E^(-((10000 I)/(4 10^-6 I + 60 t)))/Sqrt[10^-6 - 15 I t]], {t, 0, 15}]}]


which I posted recently in this answer. It lost quite a bit of formatting (fraction, square root, exponential) in the transcription process, but I can't put that in and I definitely shouldn't do something that compromises the code upon copy-paste. Thus I left it like that; if a clearer exposition of what the code is saying is necessary then I'd recommend adding a LaTeX'd version of what's going on.

• If you don't like having stuff like \[Theta] floating around, it's quite easy to use the slash-dot operator and shove a different variable into the expression. Doesn't always work, though. – Manishearth Aug 13 '13 at 15:30
• Symbols inside \[ ... ] will convert to their prettier versions automatically. More complicated formatting like the subscripts, radicals and fractions in my answer can be prettified by sandwiching them inside StandardForm[Hold[ ... ]]` and copy-pasting the output. – Emilio Pisanty Aug 19 '13 at 20:34