Steps to reproduce:
1. Note that instructions at right of screen say "indent code by 4 spaces." My interpretation of this is that lines that are indented by four spaces will be recognized as code.
2. Ask a new question on stackoverflow.
3. In the message text area, type the following:
This is some code[enter][space][space][space][space]++n;
4. Note that the code is indented four spaces but the preview does not format it as code.
5. Post the message. Note that the code is not properly formatted.

Is this a bug or am I not understanding the instructions correctly?

I am aware of this post Code is indented by 4 spaces but isn't interpreted as code and its apparent duplicate at Code block is not properly formatted when placed immediately after a list item. Both of those deal with code that is part of a list. My question concerns code that is not part of a list so I have started a separate discussion.

Edit: changed the title to make clear that this question has nothing to do with lists or and is unrelated to the several other posts on meta that are related to code indentation problems in the context of lists.

  • Dupe: meta.stackexchange.com/questions/51338/… – kennytm Jul 25 '10 at 18:49
  • 1
    As I mentioned, I started this post because the code in question has nothing to do with bulleted lists. Or have I implicitly created a bulleted list somehow? – strongopinions Jul 25 '10 at 19:03
  • If I understand you correctly, your question actually has nothing to do with lists at all and it might have been a better idea not to mention them. – David Z Jul 25 '10 at 19:07
  • @David Zaslavsky: I mentioned it specifically because I wanted to avoid getting marked as a duplicate. It seems to have backfired. – strongopinions Jul 25 '10 at 19:11
  • 2
    @strongopinions: yeah, I'd say so. I think if you hadn't mentioned lists, people's thoughts might not have jumped to the questions about code blocks in lists. – David Z Jul 25 '10 at 19:15
  • I think it was a combination of the fact that list questions were brought up at the start, and the actual demonstration of the issue consisted of a list followed by a code block. When I posted, I understood it as "I want the code formatting to not be part of the preceding list", rather than "The part before the code formatting isn't part of a list either". Because there's separate approaches to code formatting after a list based on whether the code formatting is intended to be in the list or not. – Grace Note Jul 25 '10 at 19:28

You need to leave a blank line before an indented block to have it formatted as code.

example 1
    this will not be formatted as code


example 2

    this will
  • 2
    Thanks. I really wish this were in the instructions. – strongopinions Jul 25 '10 at 19:10

The latter.

Normal code block
  • Indented code needs extra spacing. Heed this rule:

    One blank line before, 4 chars + 4 per indentation level.
    • Like this.


Here's the source of the above with annotated spaces (replace ¹²³⁴ with four spaces).

Not really.

¹²³⁴Normal code block

* Indented code needs extra spacing. Heed this rule:

¹²³⁴¹²³⁴One blank line before, 4 chars + 4 per indentation level.

 * Like this.

  • Alternatively, you can use <pre></pre>, which does forgo code highlighting. – badp Jul 25 '10 at 18:53
  • I'm sorry, I don't understand your response. It looks like you have given an example of a normal code block, which is what I want, but I don't understand how to create one. As I showed in my repro steps, I am indenting 4 spaces but nothing special happens as a result. Is it because you have a linebreak before the code? I'm also not trying to create indented code, but just to get my code to be highlighted, which I understand should be triggered if the line is indented by 4 characters. – strongopinions Jul 25 '10 at 19:08
  • @strongopinions: One blank line before, 4 chars + 4 per indentation level. All parts are important, both the extra linebreak and the extra four spaces (if you want the codebox indented). Give me in the question body something that fails for you. – badp Jul 25 '10 at 21:08

You must log in to answer this question.

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