20

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.

10
  • possible duplicate of How do I format my code blocks? Commented Jul 13, 2012 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? Commented Jul 13, 2012 at 6:50
  • @UristMcBobby What are you talking about?
    – balpha StaffMod
    Commented Jul 13, 2012 at 6:52
  • 1
    @ChristianRau Creating the HTML yourself is a supported way of formatting code. The validator should accept that.
    – balpha StaffMod
    Commented Jul 13, 2012 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... Commented Jul 13, 2012 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 StaffMod
    Commented Jul 13, 2012 at 7:00
  • 8
    If you really did it like you described here, your closing tags were switched.
    – Ral Zarek
    Commented Jul 13, 2012 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. Commented Jul 13, 2012 at 7:56
  • I cannot answer for some reason... :/ However, it seems like you may have added some pseudo-code as text which SO thinks is actual code and hasn't been formatted. For example, I had psuedo-code in the form of a list like: this_list = [] that_list = [] And so it wasn't happy that wasn't given as code.
    – DuDoff
    Commented Oct 2, 2019 at 10:48
  • I am getting exactly the same while trying to include mathematica code. I am not new to StackExchange and I can not possible figure out what is wrong as I am doing precisely what I do with every other single embed. It is extremely frustrating. All the solutions described here do not work.
    – M.E.
    Commented Aug 13, 2022 at 8:56

3 Answers 3

10

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!

4
  • 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
    Commented Jul 13, 2012 at 11:02
  • @Arjan: Sorry for the oversight. I've updated the answer now. Commented Jul 13, 2012 at 13:56
  • Option 2 is exactly how my code block is formatted (so I can highlight changes in the code) and I'm still getting the error. (And don't get me started on why a heuristic that can fail blocking submission. I never would have guessed that message meaning I couldn't submit. I spent a lot of time on my answer to have it all wasted.)
    – trlkly
    Commented May 2, 2017 at 22:56
  • @trlkly: If you are absolutely sure that that's how you've formatted your response, then I cannot determine what may be causing the error. You probably might have to get in touch with the site's moderators to see if there's anything peculiar about your input. Commented May 8, 2017 at 5:56
0

If you have any headings in your post, this could also be the case the warning is (wrongly) shown.

As proposed in this answer, you should make sure there is an empty line after each heading.

-4

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.

1
  • 2
    I did, it didn't work. But Alex said that I need to have the tags in reverse, I'll try that. Commented Jul 13, 2012 at 17:55

You must log in to answer this question.

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