4

Whenever I ask a question, on the right side jumps out a pink block 'How to Format', which tells me 'indent code by 4 spaces'. I tried putting four spaces before my code, but it never makes any difference - there's still no indentation after my question was posted and my code was still in plain text. What's wrong?

13

Did you leave an empty line before the code? For example:

Test line
    line 1 indented by four spaces
    line 2 indented by four spaces

Test line 2

    line 3 indented by four spaces
    line 4 indented by four spaces

Shows up as:

Test line line 1 indented by four spaces line 2 indented by four spaces

Test line 2

line 3 indented by four spaces
line 4 indented by four spaces
7

There's another syntax for code blocks which is mentioned in the advanced formatting help. Surround the code by three backticks on a line of their own. The backticks have to be on their own line but don't need a blank line.

```
#!/bin/sh
echo Hello, world.
```

This is rendered as

#!/bin/sh
echo Hello, world.

This syntax also makes it easy to request syntax highlighting: add a syntax code after the opening backticks. For example:

```lang-sh
#!/bin/sh
echo Hello, world.
```

This is rendered as

#!/bin/sh
echo Hello, world.

(There's no syntax highlighting on meta, but there is on the main site.)

(For the old-timers: this was implemented in January 2019.)

  • See also the <pre>...</pre> which allow formatting within (like with <b>bold</b>) though you need to remember to write the <, > as &lt;, &gt; and you don't get syntax highlighting. – Stéphane Chazelas Mar 14 at 16:52
  • Is it just my browser or since a few days, the code blocks show as if there was an extra column on the left. That is pretty annoying and misleading. – Stéphane Chazelas Mar 14 at 16:55
  • (0) It seems to be good enough to say ```sh; you don’t need lang-. … (Cont’d) – G-Man Says 'Reinstate Monica' Mar 21 at 18:59
  • (Cont’d) …  (1) Apparently you get syntax highlighting if you use <pre><code>...</code></pre>.  (2) If you use <Pre>...</pre> (where any or all of the letters in the opening <pre> are capitalized), you can use Markdown (*italic* and **bold**).  (3) I hadn’t noticed the “extra column on the left” (although I guess I see it now that you mentioned it), but, just in the past few days, I have noticed that there seem to be fractional blank lines at the top and bottom of every code block — as if somebody added approximately 6 pt. padding on (at least) the top, bottom and left. – G-Man Says 'Reinstate Monica' Mar 21 at 18:59
3

Let me have a go

code    

yes it works! I left a blank line before it.
So the 'How to Format' should have told us about this!

  • 2
    Annoying, isn't it? I've been writing markdown pages for many years (my blog is written in it) and I keep forgetting that blank line is needed! Fortunately, once you know the trick it's easy to edit your post and fix it :-) – Stephen Harris Mar 19 at 0:12
1

The behavior you are seeing is a design feature: in Markdown you need a blank line to signal the and of a paragraph. Spaces or tabs at the beginning of a line are not enough to make it a different block from the preceding (non blank) line(s).

This is documented both in Help Center > Our Model > How do I format my posts using Markdown or HTML? (briefly, under "Linebreaks") and in the original Markup documentation, which the Help Center links to:

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines. (A blank line is any line that looks like a blank line — a line containing nothing but spaces or tabs is considered blank.)

I agree, though, that it is not intuitive to infer that a blank line is required to trigger proper formatting of code blocks (or, similarly, of lists). It would probably be useful to have a brief notice in the "How to format" help box on the right of the question/answer boxes and/or in the Help Center and/or in the advanced help page that we can reach by clicking the question mark in the top right corner of the question/answer boxes.

Note that a preceding blank line is not required if you use the "code fences" style of formatting for code blocks (whose introduction, as far as I can tell, to date has been reflected in the help pages only partly (Markdown help, expanding "Code and Preformatted Text")), which also removes the need for the 4-spaces indentation.

  • 1
    Indeed the 'How To Format' box is so very misleading. Even when we click into the Help Centre, we still don't know which part to see. You mentioned 'Markdown', but who would know we should look into that part. What does Markdown mean? Who knows! The instruction doesn't use any simple language and is very misleading in every way. – OhLook Mar 13 at 18:20
  • 2
    @OhLook I think we are supposed to have spent some time in the Help Center before posting, and that the "How to format" box is there mainly to remind us things we should at least roughly know. However, you are right in pointing out that "Markdown" is cited nowhere in the "Asking" section of the Help center (it's only in "Our Model"), and this makes the documentation not really clear in this regard. Furthermore, note that some advanced stuff is found only on Meta Stack Exchange. – fra-san Mar 13 at 19:15
0

But when you want to put that grey thingy onto something in the same line as your plain text, the blank line isn't applicable.
This website simply never tells you what you need to do to have this visual effect. Time to change!

  • 1
    @Olorin How do you solve this problem? – XYZ Mar 12 at 14:25
  • 4
    Could you explain what you mean by "put that grey thingy onto something in the same line as your plain text"? Are you just looking for backticks like this? If you add a ` on either side of the target text, it will appear as code inline. So `foo` is rendered as foo. – terdon Mar 13 at 11:05

You must log in to answer this question.

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