Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ensure that whitespace is preserved in Markdown?

Tags:

markdown

Currently, I have this line in a Markdown file detailing command output:

1\. Work (00:10:00)  
    1\. Mail letter (00:05:00, Est. 00:03:00)  
      Send letter to Foo Bar  
2\. Personal (00:02:00)

However, when I preview the Markdown file, all of the whitespace is disregarded.

1. Work (00:10:00)
1. Mail letter (00:05:00, Est. 00:03:00)
Send letter to Foo Bar
2. Personal (00:02:00)

How do I preserve this whitespace?

like image 520
cheese1756 Avatar asked Mar 30 '13 17:03

cheese1756


People also ask

What is &nbsp in Markdown?

Another option, if your Markdown processor supports HTML, is to use the HTML entity for non-breaking space (   ).

How do you keep an indent in Markdown?

As a workaround I would suggest inserting a vertical bar (|) followed by hard spaces (Alt-Code on Windows: Alt+0160). This preserves the indent after the bar resulting in a visually acceptable solution for raw and rendered Markdown. This is a normal line of text. | This is an indented line of text.

What does ``` do in Markdown?

Mostly, Markdown is just regular text with a few non-alphabetic characters thrown in, like # or *. Markdown coverts text with four leading spaces into a code block; with GFM you can wrap your code with ``` to create a code block without the leading spaces.


3 Answers

Markdown is used primarily to generate HTML, and HTML collapses white spaces by default. Use   instead of space characters.

like image 126
Matt Ball Avatar answered Oct 23 '22 07:10

Matt Ball


Use non-breaking spaces

To preserve spaces in a markdown document use a non-breaking space:
"a space character that prevents consecutive whitespace characters from collapsing into a single space, and also prevents an automatic line break at its position".

Example

See an online and editable example here.

This line uses            non-breaking            spaces in many places;       they are not collapsed.
                                   There is no need to use code blocks.

This line uses many consecutive spaces in many places; they are all collapsed.

Note:
Copy and paste the previous example could not work because sometimes the non-breaking spaces are changed to normal spaces in a copy-paste operation :‑(.

Or tray a table

However, if you intend to use non-breaking spaces to align text, prefer to use tables.

Example code:

| Country  | Capital |
| -------- | --------|
| Portugal | Lisbon  |
| Spain    | Madrid  |
| Cuba     | Havana  | 

But not all Markdown implementations recognize the previous syntax.

How to introduce a non-breaking space?

  • In macOS, you need to press ⌥ Opt+Space
  • In Windows, sometimes work Alt+0+1+6+0 or Alt+2+5+5
  • In many commercial software Ctrl+Space
  • In Linux, Compose key enabled Compose Space Space

The beauty of this solution is that you don't need to use any code in your Markdown document. For example, in HTML you must use  .

PS:
Reader, please, let us know in the comments is this method does not work in your particular markdown editor. I have tested this method in two apps and several online editors.

like image 29
ePi272314 Avatar answered Oct 23 '22 09:10

ePi272314


One alternative is to use

<pre></pre>

like:

<pre>

    1 
   / \ 
  2   2 
 / \ / \ 
3  4 4  3 
</pre>

the pyramid will be preserved.

Of cause, you can use &nbsp; . I use them both, depending on needs.

like image 39
Eric Chow Avatar answered Oct 23 '22 08:10

Eric Chow