What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

I was trying to answer a coding question in java, I had some code and used <pre><code>, put my code in, then typed </pre></code>. I then came across this error when trying to post:

Oops! Your edit couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

It is properly formatted (at least by the preview it is), so how can I fix this? I'm new to stack overflow formatting.

share|improve this question

migrated from stackoverflow.com Jul 12 '12 at 23:52

This question came from our site for professional and enthusiast programmers.

    
possible duplicate of How do I format my code blocks? –  Time Traveling Bobby Jul 13 '12 at 6:50
    
So why not just click those little {} in the format toolbar, which do nothing more than put 4 spaces in front of each line, instead of those weird XML tag thingies? –  Christian Rau Jul 13 '12 at 6:50
    
@UristMcBobby What are you talking about? –  balpha Jul 13 '12 at 6:52
1  
@ChristianRau Creating the HTML yourself is a supported way of formatting code. The validator should accept that. –  balpha Jul 13 '12 at 6:54
    
@balpha: What? Since when?! I always thought that it was "yak, yeah, you can do that too" type, and not something which should be encouraged (as with all HTML markup)...mea culpa big time... –  Time Traveling Bobby Jul 13 '12 at 6:58
1  
@UristMcBobby Well, for one thing, your possible dupe lists this as a valid alternative :) Yes, you're correct we'd rather have people use Markdown, but that doesn't mean we should (as happened in this case; the code was formatted just fine) prohibit the supported use of <pre><code>ing yourself, I think. But even if we eventually decline this, it should still be explicit. –  balpha Jul 13 '12 at 7:00
7  
If you really did it like you described here, your closing tags were switched. –  Ral Zarek Jul 13 '12 at 7:08
    
@balpha: Yes, that's what I meant, I can remember reading that post some time back, but could not remember that the pre-code-stuff was in there, and also not that it was encouraged (in the sense of "You can do this too"). Reading this question, I just thought you guys killed it off. –  Time Traveling Bobby Jul 13 '12 at 7:56

2 Answers 2

up vote 4 down vote accepted

Markdown automatically formats such lines (those that start with four spaces) as code. When pasting code, you can have it automatically formatted for you by clicking on the Code Icon button. Alternatively, you can put the four spaces in front of the code lines yourself. As a result, your code looks like this:

    class Something{
        int i;
        public void DoSomething(){
            //some code here
        }
    }

You also have to make sure that you leave at least one blank line between your statements/paragraphs and your code to ensure the code gets detected and formatted properly.

The alternative is to wrap the code in <pre><code> tags. Formatting of raw HTML can be sometimes terse so you'll have to make sure you close the tags in reverse to the order you opened them. In other words, open and close tags like so:

<pre><code>
class Something{
    int i;
    public void DoSomething(){
        //some code here
    }
}
</code></pre>

If you interchange the position of the closing tags, it is possible that markdown will not format your code properly or sometimes it will not be formatted as code at all!

share|improve this answer
    
With <pre><code> one does not need the four spaces. Also, one would need to manually encode HTML entities, such as &lt; or &#60; for <, and &amp; or &#38; for &. –  Arjan Jul 13 '12 at 11:02
    
@Arjan: Sorry for the oversight. I've updated the answer now. –  Alex Essilfie Jul 13 '12 at 13:56

Excuse me for repeating message you've just cited, but didn't system tell you plain and clear:

Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut.

If you do just that, you'll get your code in and it will even get automatically highlighted.

share|improve this answer
    
I did, it didn't work. But Alex said that I need to have the tags in reverse, I'll try that. –  John Schneider Jul 13 '12 at 17:55

You must log in to answer this question.

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