What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 130 Stack Exchange communities.

Oftentimes, I want to include code samples in a list, for example:

  1. Item One
  2. Item Two, for example:

    private bool ItemTwo()
    {
        return this.IFeelLucky;
    }
  3. Item Three

The problem is that there isn't a good and intuitive way to do this using Markdown. It's doable, but you have to do some formatting black-magic to get it that way, which is just not "nice" to deal with.

Source that produces this:

1. Item One
2. Item Two, for example:
<br/><br/><pre><code>private bool ItemTwo()
    {
        return this.IFeelLucky;
    }</code></pre>
3. Item Three

I would love to just be able to do it this way:

1. Item One
2. Item Two, for example:

    private bool Test()
    {
        return this.IFeelLucky;
    }

3. Item Three

Which results in, well, this:

  1. Item One
  2. Item Two, for example:

    private bool Test() { return this.IFeelLucky; }

  3. Item Three


Update: In order to use Markdown, you have to indent a minimum of eight spaces. I still stand my ground that this isn't 100% intuitive - you can indent only a single space to continue a list item (see below) - but at least it's good and easy.

1. List item

 Item continued

2. Another list item

Results in:

  1. List item

    Item continued

  2. Another list item

Return to FAQ index

share|improve this question
1  
    
Seems a little silly to close as a dupe a FAQ question, either close in the other direction, or move the FAQ tag. –  Lance Roberts Mar 26 at 23:07

4 Answers 4

up vote 70 down vote accepted

Code is possible in markdown (see here) - you just have to leave a blank line and then indent by 8 spaces as a minimum.

  • example

    this.isSomeCode = true;
    
share|improve this answer
3  
I see. Somehow I didn't try that, and I had missed it on the markdown editing help page as well. –  lc. Jul 9 '09 at 14:10
1  
That's because it seems very counter-intuitive. Thank you for posting this, I had been despairing of doing the same thing. –  fool4jesus Jan 8 at 14:53

If you want to add a block of code to a list item, you have to add an extra 4 spaces for every level of that list. You also have to make sure that you leave a blank line before the code block.

code block outside of a list
  • Main list

    code block
    
    • sub list

      code block
      
      second code block
      
      • sub2 list

        code block
        
        • sub3 list

          code block
          

      up two list levels
      
# 4↴
    code block outside of a list

- Main list
#     8↴
        code block
# 4↴
    - sub list

#        12↴
            code block
#     8↴
        <!-- --> # can also be used to set highlighting <!-- language: lang-none -->
#        12↴
            second code block
#     8↴
        - sub<sup>2</sup> list

#            16↴
                code block
#        12↴
            - sub<sup>3</sup> list
#                20↴
                    code block
#     8↴
        <!-- -->
#        12↴
            up two list levels

If you need to force the Markdown processor to start a new section, just add a <p/> or <!-- --> on it's own line at the appropriate level. That's how I managed to follow a list with a code block for this answer.
If you do use <!-- --> you can use it to set the highlighting mode for the following code block as well (<!-- language: lang-none -->).

share|improve this answer
    
nice example, thx. –  quack quixote Dec 10 '09 at 5:44
    
The <p/> trick is brilliant - thanks! –  fbrereto Feb 3 '10 at 17:45
    
I now use an HTML comment, instead of <p/> . –  Brad Gilbert Feb 3 '10 at 18:34
    
It would be nice if the code button on the format bar above the edit box did that. –  Jay Elston May 13 '10 at 17:23
    
+1 : You also have to make sure that you leave a blank line before the code block –  Aaron J Lang Sep 10 '13 at 9:40

If you want to start a list item with a block of code, rather than text, you need to add at least a space character after the bullet or number, then make two line breaks before starting to indent the code block with 8 spaces.

In the following examples, line breaks are denoted by the ↵ symbol.


  • unordered
    
 - ↵
↵
        example

  1. ordered
    
1. ↵
↵
        example

Also see this answer for a real-world example.

share|improve this answer
    
The line heights do look a little awkward though... –  BoltClock's a Unicorn Dec 25 '11 at 1:13

This is all documented in the editing help, that little [?] button above the editor.

It's also linked from the sidebar of every edit page.

http://meta.stackoverflow.com/editing-help

share|improve this answer
    
Ok. Now that I look again I see it. Somehow I missed it the first time. –  lc. Jul 9 '09 at 14:09
    
Wish I could accept multiple answers because this is really helpful too. –  lc. Jul 9 '09 at 14:11
    
You should edit that guide to include @BradGilbert 'You also have to make sure that you leave a blank line before the code block' –  Aaron J Lang Sep 10 '13 at 9:42
1  
Could you perhaps also add UI support for this? The {} button above the textbox only toggles the indentation on multiple clicks, doesn't increase the indentation. Having to do it manually (or to copy back and forth to a text editor) on long code blocks is pretty tiresome. –  sundar Sep 18 '13 at 17:34

You must log in to answer this question.

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