Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 158 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

Since I don't think we can use the <pre> tag, is there a way to preserve white space in comments? Backticks by themselves don't seem to do it:

Using backticks:
testing extra spaces

Using <code> tag:
testing extra spaces

Using <pre> tag:

testing     extra spaces

Using "Code Sample" toolbar button (Ctrl+K) (I guess this is the same as <pre><code>):

testing     extra spaces
share|improve this question
Comments only have a few features because they are meant to be secondary to the answers, and not meant to contain important information. Can you give an example of, or explain why, comments need this feature? It may take quite a bit of convincing to get it accepted. – Adam Davis Mar 17 '11 at 6:34
@Adam: This is the comment that prompted me to ask this question. It doesn't seem like enough to post a separate answer. I retagged this question to "support", since I don't really expect anything to be changed. I just wanted to know if there was a way. – Cheran Shunmugavel Mar 17 '11 at 6:41
@Cheran: You can't use <code> in comments anyway, can you? Let me try: <code>testing extra spaces</code>. So no, not possible. What you can do in questions and answers: <code>testing &nbsp; extra spaces</code>. In comments there still might be a way with crazy unicode spaces. – Hendrik Vogt Mar 17 '11 at 15:08
Agree; in passages surrounded by `, whitespace should be preserved. – tenfour Mar 18 '11 at 1:53
You use to be able to do the ol' backtick hackery trick: two spaces: ` ` ` ` -- but I think that broke a while ago. &nbsp; – Earlz Mar 18 '11 at 2:11
Hmm.. what about this? – Shadow Wizard Mar 18 '11 at 10:11
@Shadow Wizard: how did you do that? When I type HTML entities, they just show up as plain text (i.e. &#160;) – Cheran Shunmugavel Mar 18 '11 at 11:20
try this out....ah, okay, you're just typing in the actual non-breaking space character. I think that's a workable solution! Thanks! – Cheran Shunmugavel Mar 18 '11 at 11:22
@Cher doing it by holding ALT and pressing 255 in the keypad - the ♪♫ are there as well. ☻ – Shadow Wizard Mar 18 '11 at 16:48

There's something odd about this.

When I leave the comment

this is a `code span with single space` a `code  span  with  double  space`

The rendered HTML is

<span class="comment-copy">
    this is a 
    <code>code span with single space</code> 
    <code>code  span  with  double  space</code>

So the whitespace is indeed correctly preserved in the source HTML

share|improve this answer
this is a code span with single space a code span with double space – Jeff Atwood Mar 18 '11 at 7:25
I'm encouraged by reading that you find that odd, Jeff :-) – Hendrik Vogt Mar 18 '11 at 7:48
@hendrik meh, I don't think <code> does anything except switch the font as balpha pointed out to me. – Jeff Atwood Mar 18 '11 at 7:53
Indeed, @Jeff: <code> should not render excessive whitespace. (That would then also include newlines et al. <code> is simply not <pre>.) – Arjan Mar 18 '11 at 7:54
Thanks, that's really good to keep in mind. – Hendrik Vogt Mar 18 '11 at 7:59
up vote 2 down vote accepted

Per @Shadow Wizard's comment, you can type a non-breaking space character by pressing ALT+255 on the keypad (or alternatively, ALT+0160). For example

`two  spaces`

is rendered as


and the spaces are preserved.

EDIT: As Arjan pointed out, this should not be used when posting code, since someone could copy and paste it into their editor, and it possibly won't compile.

share|improve this answer
...even if this is never removed, then the fragments are no longer true code. Copy & paste might then fail with odd compile messages? – Arjan Mar 19 '11 at 10:11
@Arjan correct as usual, this should not be used to post code.. though never tried to compile code with those characters - anyone care to pick the glove? ;) – Shadow Wizard Mar 19 '11 at 10:54
Good point, @Arjan. I hadn't thought about people copying and pasting. – Cheran Shunmugavel Mar 19 '11 at 16:34

enter image description here

share|improve this answer
Nice, but that would also enable newlines. That has been declined for comments. – Arjan Mar 19 '11 at 10:00

You must log in to answer this question.

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