I have seen many answers where there is nicely parsed Mathematica code in the answer.

When I attempted to answer a question and pasted in the code, all the spaces and line breaks were removed so it became difficult to read.

How does one paste in the code and retain the indentations and line-breaks?

Related: How should I include code samples in posts?

  • 1
    If you type in code cells (not input cells), then only the line breaks and spaces you explicitly enter are present in the code and this copies fine. With input cells, the front end inserts spaces and breaks automatically. – rm -rf Mar 10 '14 at 18:39

When you type your post, there's a (?) button at the top right corner of the edit box.

Press this button for detailed formatting help.

It will tell you that to format code nicely, you need to select it, then press the {} button from above the edit box to format it. (You can press Ctrl+K, or Command+K on a Mac, to achieve the same.)

Alternatively you can manually indent each code line by four spaces to mark it as a code block.

There are formatting constructs for other things too, for example *word* will be formatted in italics: *word*. All the formatting constructs on StackExchange are designed to make the text of the post easily readable even when it wasn't processed by the formatting engine. For example, it's clear that *word* is emphasized even if you look at it in plain text.


Information about formatting can be found on the editing-help page. The relevant section is the first one.

I prefer Szabolcs answer though, as the Command + k trick seems to be missing in editing help.

You must log in to answer this question.

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