33

I want to write multiple spaces in code, say

many       spaces

but I want to do this in a code block, ``. But in this case, spaces are merged into one. I found this question, but there isn't any solution... How can I do this?

0

5 Answers 5

25

Looks like the hack suggested in the answer to the question you referring to no longer works, neither in post body nor in comments. (with `` multiple `` `` spaces)

The backticks are not exactly code blocks. They won't highlight the code inside them. They're used more to "escape" tags so they appear in the post or to distinguish code parts written as part of the sentence.

You can write multiple           spaces as part of a paragraph using   - it will be parsed as a single space.

However when part of backtick      code it's not parsed.

You also have the <pre></pre> tags, and whatever written between them will be indeed preserved as-is, for example:

many       spaces

However this can't be inline, even

if it's part of existing paragraph
(see the source) it will be parsed into its own line.

5
  • Cheers, you decided to use the &nbsp; approach then? Commented Feb 16, 2012 at 8:16
  • Actually, if you refer to the original question, it gives a workaround in the question body itself. None of the workarounds mentioned here work in comments. But if you use Alt+0160 (on Windows this is the Unicode NBSP), it works in comments: sed /    / /.
    – Abel
    Commented Mar 21, 2012 at 20:50
  • @Abel works with ALT+0160 and ALT+255 as well - but I don't like this hack as the team might "bust" those characters at some point and treat them exactly like blank space thus rendering all those efforts meaningless. Might be good and dandy for now, but we can't depend it will stay in this format for long time. Commented Mar 22, 2012 at 7:00
  • It's nothing more than a workaround. I doubt they will revert the ten-or-so Unicode spaces as collapsible spaces at some point, but you never know. However, that counts for any workaround. Since nothing else works in comments, this is the only way as far as I know, that works. Until the SO-team fixes this permanently.
    – Abel
    Commented Mar 22, 2012 at 9:21
  • For regex comments, you could just rewrite it. Don't you think it's more clear to have /[ ]{4}/? And you know that copying and pasting it will work.
    – Laurel
    Commented Dec 30, 2021 at 17:17
10

Although you can use &nbsp; at no. of times you wish and most of users do it. But it results in only one space at a time and for more spacing we've to use it no. of times i.e &nbsp;&nbsp;..... (example facing such condition while posting this answer).

So,I am here discussing about three more entity reference that can be used.

  1. &nbsp; = &thinsp; Results in one character space; e.g. A B
  2. &ensp; Results in two character space; e.g. A B
  3. &emsp; Results in four character space; e.g. A B

In other words, the character width of them are:

  • &thinsp; = 1 x &nbsp;
  • &ensp; = 2 x &nbsp;
  • &emsp; = 2 x &ensp; = 4 x &nbsp;

Let's check:

  1. A B (using &emsp; once)
  2. A  B (using &ensp; twice)
  3. A    B (using &nbsp; four times)

So, as per requirement, we can use &nbsp; (U+00A0), &ensp;(U+2002) or &emsp;(U+2003).


For more information and character entity, visit Character entity references in HTML.

4

To type spaces at a random place, I use two different methods:

  • &nbsp; &nbsp; &nbsp; ...
    Every space between each entity is also rendered. Instead of typing &nbsp; for each space, use a &nbsp;<space> combination.
  • +U2003 Unicode character. This is called an "Em space", and is bigger than a normal space. It effectively adds a long space at any point in your post. Copy-paste the gap between the following words for a demo:
    Start:          End. Between it, I used 10 em spaces.
1
  • Unicode world is full with gaps and spaces, lol! Commented Mar 22, 2012 at 7:02
4

You can use the real unbreakable space : ALT+0160 like a boss.

Note that the the block itself can't start or end with a space:
. .
. .
. .

is in fact

`    .   .`
`.   .    `
`.       .`

Screenshot from 2015, in case it gets changed in the future:

screenshot: In case it gets changed in the future.

1
  • 1
    So apparently, the block itself can now start and end with spaces :), however, the change was only visible after making a change to the answer.
    – Qwerty
    Commented Dec 30, 2021 at 16:08
-1

You need to use:

&nbsp;  non-breaking space: ' '
&emsp;  em space: ' '
&ensp;  en space: ' '
&thinsp;    thin space: ' '

SIX-PER-EM SPACE

<p>I will display &#8198;</p>
<p>I will display &#x2006;</p>

EN QUAD space example

<p>I will display &#8192;tt</p>
<p>I will display &#x2000;tt</p>

EM QUAD

<p>I will display &#8193;tt</p>
<p>I will display &#x2001;tt</p>

Table

Char    Dec Hex Entity  Name
    8192    2000        EN QUAD
    8193    2001        EM QUAD
    8194    2002    &ensp;  EN SPACE
    8195    2003    &emsp;  EM SPACE
    8196    2004        THREE-PER-EM SPACE
    8197    2005        FOUR-PER-EM SPACE
    8198    2006        SIX-PER-EM SPACE
    8199    2007        FIGURE SPACE
    8200    2008        PUNCTUATION SPACE
    8201    2009    &thinsp;    THIN SPACE
    8202    200A        HAIR SPACE
​   8203    200B        ZERO WIDTH SPACE

Basically you can replace 4 space with  

&emsp; == &nbsp;&nbsp;&nbsp;&nbsp;
1

You must log in to answer this question.

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