27

This is so bloody irritating - I finally just managed to format my answer, so code appears inside a quotation using <pre>, and I get "Your post appears to contain code that is not properly formatted as code":

so-error

Ok, so how do I properly format multiline code inside a quote? Obviously this:

> This is my quote line

    Code Line 1
    Code Line 2

... does not work, because it does not render the code inside the quote - the result is simply this:

This is my quote line

Code Line 1
Code Line 2
3
  • 1
    Asides from how to do that, don't ever just post "I found $article $quote the whole article" as answer.
    – Magisch
    Commented Apr 28, 2016 at 11:30
  • Thanks @Magisch - I didn't quote the whole article there, just the most important code lines, which should answer the OP's question. At least, I didn't just post a link :)
    – sdbbs
    Commented Apr 28, 2016 at 11:32
  • There is trailing white space in the source for the example posted here. Is that intentional? Commented Mar 16, 2022 at 12:41

1 Answer 1

31

5 spaces are needed after > (1 for the quotation and 4 for the code). So:

This class prints what is the meaning of life.

class MeaningOfLife {
 
    private static final String MEANING_OF_LIFE = "I love Java";
     
    public static void main(String[] args) {
        System.out.println(MEANING_OF_LIFE);
    }
  
}

Yes, you read right.


Is accomplished by:

> This class prints what is the meaning of life.
> 
>     class MeaningOfLife {
>     
>         private static final String MEANING_OF_LIFE = "I love Java";
>         
>         public static void main(String[] args) {
>             System.out.println(MEANING_OF_LIFE);
>         }
>     
>     }
> 
> Yes, you read right.
10
  • 1
    Fantastic, many thanks for the edit with the code - am accepting this as soon as the 8 minutes expire... Cheers! ALSO: note that > and four spaces is not enough to get this preview - you need > and FIVE spaces at least...
    – sdbbs
    Commented Apr 28, 2016 at 11:35
  • 5
    @sdbbs Yep, that's the trick. 5 spaces are needed :)
    – Tunaki
    Commented Apr 28, 2016 at 11:39
  • 19
    Side-note: the code formatting looks messed up a bit though. The blank lines aren't in "code" format. The same snippet on Main is fine =/.
    – Tunaki
    Commented Apr 28, 2016 at 11:41
  • 6
    Weird that meta and main render it differently. I see a bug report in the future. Commented Apr 28, 2016 at 14:13
  • 7
    @sdbbs: No, it's four spaces for the code ... but there is already required a space after > for the quote. Commented Apr 28, 2016 at 18:43
  • 1
    @NathanOliver: The author of this answer hadn't formatting it properly. They dropped the indent back to 0 for the empty lines, rather than maintaining the code-style indent. However, having fixed it, this weird rendering persists! Commented Apr 28, 2016 at 18:44
  • 4
    @LightnessRacesinOrbit I also wrote up a bug report: meta.stackoverflow.com/questions/321990/… Commented Apr 28, 2016 at 18:46
  • Interesting. I tried adding ` ` (NBSP) to the end of the lines to force code formatting. It did not work; what more, the edit history looks strange, in that it shows me editing a character in right next to the > character in some cases Commented Apr 29, 2016 at 13:07
  • 1
  • @Tunaki What to do if a code block is not desired? I mean can I preserve indented text in a quoting?
    – hek2mgl
    Commented Aug 18, 2017 at 9:25

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