3
$\begingroup$

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 [ θ ] } }
$\endgroup$
  • 1
    $\begingroup$ Unfortunately this site doesn't have prettyprint/prettify enabled, otherwise you would be able to get the code highlighted like on Mathematica. $\endgroup$ – Manishearth Aug 13 '13 at 7:22
  • $\begingroup$ You could try writing it in a code block, start with three consecutive ticks "`" and end it with three consecutive ticks. $\endgroup$ – Alex Nelson Mar 7 '14 at 17:16
5
$\begingroup$

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[θ]}}
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – joshphysics Aug 12 '13 at 21:37
  • $\begingroup$ 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. $\endgroup$ – dmckee Aug 12 '13 at 21:41
  • $\begingroup$ Ok...I might just use an image with a screenshot I suppose. $\endgroup$ – joshphysics Aug 12 '13 at 21:43
  • $\begingroup$ @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? $\endgroup$ – David Z Aug 12 '13 at 22:59
  • $\begingroup$ @DavidZ I added an edit; hopefully the question is clearer now. $\endgroup$ – joshphysics Aug 13 '13 at 2:22
  • $\begingroup$ @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.) $\endgroup$ – David Z Aug 13 '13 at 2:31
  • $\begingroup$ @DavidZ I'm perhaps being a bit dense here, but how did you typeset the theta character? $\endgroup$ – joshphysics Aug 13 '13 at 2:57
  • $\begingroup$ @josh I just copied and pasted from your question. $\endgroup$ – David Z Aug 13 '13 at 3:02
  • 1
    $\begingroup$ @joshphysics I think he used unicode. How you do that depends on your OS. $\endgroup$ – dmckee Aug 13 '13 at 3:02
  • $\begingroup$ 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. $\endgroup$ – David Z Aug 13 '13 at 3:04
  • $\begingroup$ @DavidZ Ah ok thanks. $\endgroup$ – joshphysics Aug 13 '13 at 3:25
  • $\begingroup$ @joshphysics One trick I use is to type θ (when not on Windows) or $\theta$ in the text box and copy the resultant character from the result. $\endgroup$ – Manishearth Aug 13 '13 at 7:25
  • $\begingroup$ @Manishearth Cool thanks. Too bad we don't have prettification :( $\endgroup$ – joshphysics Aug 13 '13 at 7:27
  • $\begingroup$ @joshphysics: Use this for reference: αβγδΔΔεζηθϑΘι∫κλΛμvoπ∏ρσςτυφϕΦχψΨωΩ√∂∇∴∠∞⇔ℝℂ∅°(who uses degrees?.)× $\endgroup$ – Abhimanyu Pallavi Sudhir Aug 13 '13 at 13:14
5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Manishearth Aug 13 '13 at 15:30
  • $\begingroup$ 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. $\endgroup$ – Emilio Pisanty Aug 19 '13 at 20:34

You must log in to answer this question.

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