9

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
8
  • 3
    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.
    – Pollyanna
    Mar 17, 2011 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. Mar 17, 2011 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. Mar 17, 2011 at 15:08
  • Agree; in passages surrounded by `, whitespace should be preserved.
    – tenfour
    Mar 18, 2011 at 1:53
  • 1
    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, 2011 at 2:11
  • @Shadow Wizard: how did you do that? When I type HTML entities, they just show up as plain text (i.e. &#160;) Mar 18, 2011 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! Mar 18, 2011 at 11:22
  • 1
    @Cher doing it by holding ALT and pressing 255 in the keypad - the ♪♫ are there as well. ☻ Mar 18, 2011 at 16:48

3 Answers 3

2

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> 
    a 
    <code>code  span  with  double  space</code>
</span>

So the whitespace is indeed correctly preserved in the source HTML

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

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

<code>two&#160;&#160;spaces</code>

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.

3
  • 1
    ...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, 2011 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? ;) Mar 19, 2011 at 10:54
  • Good point, @Arjan. I hadn't thought about people copying and pasting. Mar 19, 2011 at 16:34
0

enter image description here

1
  • Nice, but that would also enable newlines. That has been declined for comments.
    – Arjan
    Mar 19, 2011 at 10:00

You must log in to answer this question.

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