Markdown Tables and other Blocks

Using Flexmark in Stata

Doug Hemken

May 2018

As block elements, tables behave much the way paragraphs do in how they are delimited from other block elements.

Tables and other tables

To separate one table from the next, a blank line is required. To separate the tables visually, some intervening text might add clarity (multiple blank lines are just collapsed). If the table cells just happened to be the same width, two stacked tables could appear to be a single table!

```
| abcde | fghij |
| ---   | ---   |
| 123   | 456   |


| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |
```
abcde fghij
123 456
Column One Column Two
data cell one data cell two

A run-on table would look like this (notice the dashes become text, not a theme break - tables are not containers):

```
| abcde | fghij |
| ---:  | ---:  |
| 123   | 456   |
| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |
```
abcde fghij
123 456
Column One Column Two
data cell one data cell two

Tables and paragraphs

Paragraphs have the similar precendence as tables - you could think of a table as a paragraph-like block with added internal formating. After a paragraph, a blank line is required to separate the table, otherwise it is interpreted as a continuation of the paragraph.

However, after a table no blank line is needed to mark a new paragraph - the absence of cell delimiters in a line of text indicates a new block has begun.

```
Paragraph one.

| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |
Paragraph two.
```

Paragraph one.

Column One Column Two
data cell one data cell two

Paragraph two.


Tables and headers

Headers take precedence over tables. ATX headers do not require trailing blank lines, so tables may follow immediately after them. And ATX headers may interrupt (end) a table.

```
### Header Before
| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |
### Header After
```

Header Before

Column One Column Two
data cell one data cell two

Header After

Setext headers also do not require trailing blank lines. However, they require a preceding blank line after a table, or the table text becomes part the the header.

```
Header Before
---
| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |

Header After
---
```

Header Before

Column One Column Two
data cell one data cell two

Header After


Tables and theme breaks

A theme break requires no trailing blank line, like a header, so a table may appear immediately after one. However, in order to be interpreted correctly after a table, a blank line is required between the table and the theme break.

```
- - - - -
| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |

- - - - -
```

Column One Column Two
data cell one data cell two


Tables and code blocks

Fenced code is clearly demarcated, so no blank line is needed either before or after a table.

```
generate y = x
```
| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |
```
summarize y
```
generate y = x
Column One Column Two
data cell one data cell two
summarize y

Indented code is demarcated by four or more leading spaces on each line. No blank line is needed before a table. However, after a table a blank line is needed, or the code block is interpreted as paragraph text.

```
    generate y = x
| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |

    summarize y
```
generate y = x
Column One Column Two
data cell one data cell two
summarize y

Tables and block quotes

Block quotes allow for "lazy" text wrapping - not very line of text in a block quote require the preceding ">" (greater than) symbol. So a blank line is required after a block quote or the table is interpreted as ordinary text.

No blank line is required after a table, before a block quote.

```
> Block quote before

| Column One    | Column Two    |
| ---           | ---           |
| data cell one | data cell two |
> Block quote after
```

Block quote before

Column One Column Two
data cell one data cell two

Block quote after

A table may also appear within a block quote (one of the two container block types). To be included as an element within a block quote, the table must be preceded by the block-quoting symbol.

Notice that within the block quote, a blank line is required to separate the table from the preceding text. (A blank line need not be quoted, but for clarity it is a good idea.)

```
> sometext
> 
> | Column One    | Column Two    |
> | ---           | ---           |
> | data cell one | data cell two |
```

sometext

Column One Column Two
data cell one data cell two

And notice that lazy blockquoting works for tables as well as for paragraphs.

```
> sometext
>
> | Column One | Column Two |
| --- | --- |
| data cell one | data cell two |
```

sometext

Column One Column Two
data cell one data cell two

Tables and lists

List items allow for lazy text continuation like block quotes and paragraphs. So a blank line is needed after a list item to demarcate a table.

After a table, a blank line is needed to prevent the trailing list item to be interpreted as ordinary paragraph text.

```
- list item

| Column One | Column Two |
| --- | --- |
| data cell one | data cell two |
- another list

```
Column One Column Two
data cell one data cell two

Lists are also container blocks, so a table may be within a list item. Annoyingly, leading spaces in the rows after the header turn all of the table into ordinary text, so the table cannot be written with the best clarity.

```
1. item 1
1. | Column One | Column Two |
| --- | --- |
| data cell one | data cell two |
```
  1. item 1
  2. Column One Column Two
    data cell one data cell two

A table may also appear as a continuation within a list item. Note there are four leading spaces before the table lines, and a blank line between the paragraph text and the table. This does allow the table to be written with visual clarity.

```
- some text

    | Column One | Column Two |
    | --- | --- |
    | data cell one | data cell two |
```